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

Implement a relayStylePagination field policy helper function. #6465

Merged
merged 2 commits into from
Jun 22, 2020

Commits on Jun 22, 2020

  1. Implement a relayStylePagination field policy helper function.

    This helper function makes it very easy to consume paginated lists from
    Relay-friendly GraphQL servers, such as the Artsy search API
    (https://metaphysics-production.artsy.net):
    
      import { InMemoryCache } from "@apollo/client"
      import { relayStylePagination } from "@apollo/client/utilities"
    
      const cache = new InMemoryCache({
        typePolicies: {
          Query: {
            fields: {
              // Keep searches separated by args.query (but not by any other
              // arguments, since the field policy will handle them).
              search: relayStylePagination(["query"]),
            },
          },
        },
      });
    
    With this field policy in place, you never need to provide an updateQuery
    function when calling fetchMore for the Query.search field, since the
    merge function handles that logic far more reliably than any updateQuery
    function can. Also, you do not need to use any `@connection` directives in
    your queries for the field, because that information is expressed by the
    ["query"] keyArgs passed to the relayStylePagination function (above).
    
    I made this helper function an export of the the @apollo/client/utilities
    entry point (along with concatPagination and offsetLimitPagination) so
    those functions will not be included in your application bundle unless you
    import and use them. We hope you will find them useful, but we also
    encourage using them merely for guidance/inspiration when writing your own
    custom field policy helper functions.
    
    Note that Apollo Client has no hard-coded logic for Relay connections,
    edges, nodes, pageInfo, cursors, or forwards and backwards pagination,
    because it doesn't need to know anything about these concepts. With enough
    care, anyone could write the field policy generated by this helper
    function, which is a considerable testament to the power and flexibility
    of field policies.
    benjamn committed Jun 22, 2020
    Configuration menu
    Copy the full SHA
    fbdb446 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    e228535 View commit details
    Browse the repository at this point in the history