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

Data of the fields added by Link cannot be retrieved from merge #8384

Closed
kazekyo opened this issue Jun 13, 2021 · 6 comments
Closed

Data of the fields added by Link cannot be retrieved from merge #8384

kazekyo opened this issue Jun 13, 2021 · 6 comments

Comments

@kazekyo
Copy link
Contributor

kazekyo commented Jun 13, 2021

Intended outcome:
For example, I have a query like the following:

const APP_QUERY = gql`
  query AppQuery {
    viewer {
      id
      robots(first: 2) {
        edges {
          node {
            id
          }
        }
      }
    }
  }
`;

A Link I created will add cursor and pageInfo, and the following query will be requested:

const APP_QUERY = gql`
  query AppQuery {
    viewer {
      id
      robots(first: 2) {
        edges {
          node {
            id
          }
          cursor
        }
        pageInfo {
          hasNextPage
          hasPreviousPage
          startCursor
          endCursor
        }
      }
    }
  }
`;

As a result, I expect to have pageInfo and cursor data in incoming of merge.

const client = new ApolloClient({
  cache: new InMemoryCache({
    typePolicies: {
      User: {
        fields: {
          robots: {
            merge(existing, incoming, { mergeObjects }) {
              console.log("---incoming---");
              console.log(incoming);
              return mergeObjects(existing, incoming);
            },
          },
        },
      },
    },
  }),
  link: from([
    createMyLink(),
    new HttpLink({ uri: "http://localhost:4000/graphql" }),
  ]),
});

Actual outcome:
The response to that request has data for cursor and pageInfo, but incoming does not have the data for pageInfo and cursor.

How to reproduce the issue:
https://github.com/kazekyo/apollo-link-test

Versions

System:
    OS: macOS 11.3.1
  Binaries:
    Node: 16.0.0 - ~/.nodenv/versions/16.0.0/bin/node
    Yarn: 1.22.10 - ~/.nodenv/versions/16.0.0/bin/yarn
    npm: 7.10.0 - ~/.nodenv/versions/16.0.0/bin/npm
  Browsers:
    Chrome: 91.0.4472.77
    Firefox: 76.0.1
    Safari: 14.1
  npmPackages:
    @apollo/client: 3.3.20 => 3.3.20
@benjamn
Copy link
Member

benjamn commented Jun 14, 2021

@kazekyo The ApolloLink abstraction provides flexibility in terms of how data gets fetched, but one thing links are not able/allowed to do is change the shape of the operations they receive, including adding or removing fields.

Thanks to your reproduction, I can tell you this line doesn't actually change/replace the query from the perspective of the client, so the client/cache won't acknowledge or process the extra fields, since those fields were not in the original query as provided to the link.

All things considered, I would not recommend trying to solve this problem with an ApolloLink. Instead, I believe you should trust the original APP_QUERY to ask for the fields it needs. If the cursor and pageInfo fields are essential to pagination, it's the query's responsibility to ask for them.

Also, since it looks like you're using Relay-style pagination, and mergeObjects is just a shallow merge, unaware of edges or cursors or pageInfo and how they relate to each other, you may be interested in our relayStylePagination field policy helper function.

@kazekyo
Copy link
Contributor Author

kazekyo commented Jun 15, 2021

@benjamn Thanks for the quick reply!
I understood that it is a specification 👍

I'm a third-party library developer and I'm working on a library to make Apollo Client more useful with Relay Specification. The library contains the improved relayStylePagination and some directives.

In some links, I have changed the operation. For example, the implementation of @arguments and @argumentDefinitions.

I understood that this way is not recommended, but would you please create a recommended way to modify operation.query and operation.variables for Apollo Client users? 😭

If we can agree on some kind of design, I will be happy to help you 🙌
I will submit a request to apollo-feature-requests, please check it out.

@kazekyo
Copy link
Contributor Author

kazekyo commented Jun 15, 2021

I posted a request to apollo-feature-requests. I hope we can discuss it there.
apollographql/apollo-feature-requests#300

@kazekyo
Copy link
Contributor Author

kazekyo commented Jul 31, 2021

@benjamn I'm still interested in this issue.
If third-party libraries can modify Query and Variables, we will see more exciting libraries. On the other hand, I don't think anyone wants to see links created in a non-recommended way.

There doesn't seem to be much discussion on apollo-feature-requests, including my post.
If there is no input from you, I will submit a PR with some ideas.

Let me know if you have any input.

@jerelmiller
Copy link
Member

Hey @kazekyo 👋

Thanks for your patience! We believe this can now be solved with document transforms released in 3.8.0 which will allow you to transform a query document that will be used by the cache as well. Please try this out and see if it helps here. Thanks!

Copy link
Contributor

github-actions bot commented Jan 9, 2025

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

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

No branches or pull requests

3 participants