Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow calling readField with ReadFieldOptions. #6306

Merged
merged 1 commit into from
May 18, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented May 18, 2020

Note: I decided to split these changes out from PR #6288 after all, per my
comment #6288 (comment).

As with Cache.EvictOptions in #6288, named options also make sense for the options.readField function that we pass to read and merge functions.

While it was/is technically possible to specify field arguments by passing a FieldNode instead of a string as the first argument to readField (as I was recently reminded while investigating #6278), I think it makes a whole lot more sense to let readField accept named options, so that we can more easily add fields like args (and other optional fields, in the future):

new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        messages(_, { args, toReference, readField }) {
          return readField({
            from: toReference({
              __typename: "Chat",
              id: args.id,
            }),
            fieldName: "messages",
            args: { offset: 0, limit: 10 },
          });
        },
      },
    },
  },
})

As with cache.evict, options.readField can still be called with just a string, or a string and a foreign StoreObject | Reference object, in cases when that information is sufficient. Once you get into more advanced territory, passing named options is a great way to limit code complexity.

I decided to split these changes out from PR #6288 after all, per my
comment #6288 (comment).
Comment on lines -439 to -445
public getStoreFieldName(
typename: string | undefined,
nameOrField: string | FieldNode,
// If nameOrField is a string, argsOrVars should be an object of
// arguments. If nameOrField is a FieldNode, argsOrVars should be the
// variables to use when computing the arguments of the field.
argsOrVars: Record<string, any>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new FieldSpecifier type was important to clean up this awkward/subtle dependency between nameOrField and argsOrVars.

@benjamn benjamn merged commit 7429d9d into master May 18, 2020
@benjamn benjamn deleted the make-readField-accept-options branch May 18, 2020 21:30
@benjamn
Copy link
Member Author

benjamn commented May 18, 2020

Merged despite the bundlesize near-miss, because #6289 should bring the bundle size down again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant