evenchange4 / graphql.macro

Compile GraphQL AST at build-time with babel-plugin-macros.
MIT License
239 stars 21 forks source link

Breaks Apollo query deduplication #106

Open martdavidson opened 4 years ago

martdavidson commented 4 years ago

Issue Using either loader or gql from this package breaks apollo-client's query deduplication.

Steps to Recreate In two different components, fire the exact same query by either importing the same .graphql file with loader, or defining the same query with gql from this package. Render both components together, and you will see two network requests fire.

Expectation If you do the same as the above but with gql from the graphql-tag package, Apollo recognizes that the two queries are the same, and only fires one network request. Could be related to https://github.com/apollographql/apollo-client/issues/4930

Queries are deduplicated only according to the object reference of the DocumentNode.

I imagine this package is returning a different DocumentNode every time the query is imported / defined rather than building up a cache of already-compiled queries and check against that.

Is caching something that's in scope for this package?

martdavidson commented 4 years ago

For anyone else that comes by, my solution here was to configure Rescripts and use graphql-tag instead of this package.

You'll also require jest-transform-graphql if you're running tests against code that imports graphql files. This issue helped me correctly configure it for Jest.

Once you've done the above, you can simply import query from 'somewhere.graphql', and because graphql-tag caches correctly, Apollo Client's deduplication works as expected, and only one of the identical queries fires at a time.

mschipperheyn commented 4 years ago

When you have the same fragment multiple times in a query through nesting, it throws an error:

[GraphQL error]: Message: There can be only one fragment named "wallCommentFragment"., Location: [object Object],[object Object], Path: undefined