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

Erroneous warning when using imperative API #1437

Closed
bensalilijames opened this issue Mar 17, 2017 · 6 comments
Closed

Erroneous warning when using imperative API #1437

bensalilijames opened this issue Mar 17, 2017 · 6 comments

Comments

@bensalilijames
Copy link
Contributor

When running commands from the new imperative API, I get this warning:

You're using fragments in your queries, but don't have the addTypename:
true option set in Apollo Client. Please turn on that option so that we can accurately
match fragments.

fragmentMatcher	@	readFromStore.js?25f66d2:29
(anonymous)	@	graphql.js?7a56643:49
executeSelectionSet	@	graphql.js?7a56643:26
(anonymous)	@	graphql.js?7a56643:88
executeSubSelectedArray	@	graphql.js?7a56643:81
...

However, I have ensured that addTypename: true in the client setup:

const client = new ApolloClient({
  ssrMode: IS_SERVER,
  addTypename: true,
  dataIdFromObject: (result) => {
    if (result.id && result.__typename) {
      return result.__typename + result.id;
    }
    return null;
  },
  networkInterface,
});

The specific command I'm running is:

update: (proxy, mutationResult) => {
  const data = proxy.readQuery({
    query: graphQuery,
  });

  data.nodes.push(mutationResult.data.createNode);

  proxy.writeQuery({
    query: graphQuery,
    data,
  });
},

Intended outcome:

Not to display the warning, or perhaps I'm doing something wrong!

@helfer
Copy link
Contributor

helfer commented Mar 17, 2017

Hi @benhjames. I'm not sure what's going on in your case. Could you try to reproduce the error with https://github.com/apollographql/react-apollo-error-template please?

@bensalilijames
Copy link
Contributor Author

Sure - one repo coming up.

@bensalilijames
Copy link
Contributor Author

Behold, in all its glory:

https://github.com/benhjames/react-apollo-error-template

Hopefully the fact that I didn't write the mutations in the backend doesn't matter - this was sufficient to still repro the warning appearing!

Just start the app, and click the button "Click me to make error", and observe the warning in the console, despite using addTypename: true.

@helfer
Copy link
Contributor

helfer commented Mar 17, 2017

Thanks a lot @benhjames, this was extremely helpful. I now know what's going on.

First, there was a small problem in your code: you forgot to add __typename in the optimistic result. Adding that alone didn't fix the issue because of the second problem:

proxy.writeQuery doesn't check addTypename, which means the result would be written to the store without the __typename field, even if it was present on the result.

I think the proper thing to do here is to make writeQuery and writeFragment (and also the read functions, actually) respect addTypename. Once we have non-heuristic fragment matcher (hopefully next week), we can also start throwing errors if fields are missing during a write.

@calebmer do you have time to update the direct store API so it respect addTypename?

@bensalilijames
Copy link
Contributor Author

Nice one, glad it was useful! Thanks for spotting the missing __typename in the optimistic result too, will save me some head-scratching down the line. 😃

@helfer
Copy link
Contributor

helfer commented May 3, 2017

This is fixed.

@helfer helfer closed this as completed May 3, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants