Im using Apollo, React and Graphcool. I have a query to get the logged in users ID:
const LoginServerQuery = gql`
query LoginServerQuery {
loggedInUser {
id
}
}
`;
I need to use the returned ID in another query which is called from the same React component. Here Ive hardcoded "xxxxxxxxxxxxx" which is where the dynamic user ID needs to go:
const LocationQuery = gql`
query LocationsQuery {
User(id: "xxxxxxxxxxxxx") {
location {
name
}
}
}
`;
This can be done as above, ensure you use skip property as your first query will be undefined initially:
See here: How to chain two GraphQL queries in sequence using Apollo Client
If you import compose like so:
Then you can pass the props to the second query with this:
UPDATE - Actually this isn't working well. If Im on a different page, I refresh, then navigate to this page it errors. However If I then refresh when on this page it works fine.
UPDATE - I think it was a Graphcool issue. The other page that I was refreshing on was also returning the User. I needed to return the ID for the User in both React components, otherwise the caching got confused. After adding the ID field it now works.
https://www.graph.cool/forum/t/the-store-already-contains-an-id-of/218