2

I have a GraphQL query that returns an array of items. I can run the query in GraphiQL and get the result I expected.

I have a react application that uses react-apollo and the @graphql decorator to resolve the very same query to component props. When running the query via react-apollo, every item in the returned array is the same - each item has been 'overwritten' by the last item.

I can see in the dev-tools network tab that the correct array items were sent over the network, so the issue seems to relate to react-apollo. What could be causing react-apollo to overwrite array elements?

3
  • Can you post the code? Commented Jul 7, 2017 at 11:35
  • It's not really practical to post code that involves a GraphQL schema, a server, a react application and a react-redux implementation. In this case though I'm not sure it's necessary - I'm asking if there are any common reasons for misconfigurations that might cause this. Commented Jul 7, 2017 at 11:37
  • Since you said GraphiQL gives you the expected result, I think your server code does not have issues. The problem could be on the client side. I recollect any common reasons. i.e reducers? Commented Jul 7, 2017 at 11:50

1 Answer 1

5

The issue was that my qgl fragment for the query did not include the id property for the items in the Array.

If you have a similar issue, ensure your schema includes an id or _id property on each item; that each id value is unique, and that you are requesting the id property in your query. Alternatively you can supply a dataIdFromObject function in your client to dynamically generate IDs for values.

You can read more in the Apollo docs for Normalization with dataIdFromObject

Sign up to request clarification or add additional context in comments.

1 Comment

It appears you also get this strange behaviour if id is present but is an object. My query was returning a JSON structure like [{ "id": { "value": 1 } }, { "id": { "value": 2 } }], and apparently that doesn't work. I've flattened my data out and now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.