Skip to content

client.readFragment incorrectly handles optimistic option #13276

@jerelmiller

Description

@jerelmiller

Issue Description

client.readFragment is implemented as such:

  public readFragment<
    TData = unknown,
    TVariables extends OperationVariables = OperationVariables,
  >(
    options: ApolloClient.ReadFragmentOptions<TData, TVariables>,
    optimistic: boolean = false
  ): Unmasked<TData> | null {
    return this.cache.readFragment<TData, TVariables>(
      { ...options, fragment: this.transform(options.fragment) },
      optimistic
    );
  }

We have 2 function overrides for this API to allow optimistic to be passed in either options or as the 2nd argument (this form is currently deprecated). However, due to the implementation, client.readFragment({ optimistic: true }) does not correctly return the optimistic fragment since the default false value overrides options.optimistic.

The current workaround is to call client.cache.readFragment(...) or client.readFragment(options, true) to get the optimistic response, but we should fix this so that options.optimistic is properly passed to cache.readFragment.

Link to Reproduction

n/a

Reproduction Steps

See description.

@apollo/client version

4.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions