-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Is there a way to use the Query component like Promise.all? #3433
Comments
Have you considered putting all your queries into a single query document? Foo.graphql query Foo {
queryOne { ... }
queryTwo { ... }
queryThree { ... }
} Then you can use a single Alternatively you can achieve similar behavior with 7 queries using the |
Thank you for the quick response!
That's how we currently have our code. It works, but ideally we'd love to use something like the The other benefit I'm trying to achieve is for the requests to be sent in parallel.
I looked into that, but I had trouble because 6 of the queries are the same query, just with different variables (I know it sounds weird, but it makes sense for our project). I couldn't figure out how to ensure that the variable names didn't collide when the identically-named queries were returned. Another downside I can see is less useful caching. It's more useful to me to separately cache each of the requests since there can be many combinations of them. Thanks for your help! Looks like I'll end up making my own |
@oliverodaa Have you given this a try? https://github.com/jamesplease/react-composer |
I ended up doing as @TimMikeladze recommended. I put all the queries into one long query document and it worked very nicely. I used aliases to deal with the fact that some of the queries were the same, just with different variables. Because of server-side caching, I realized my caching concerns were not a big deal. Hopefully this thread helps someone in future! |
For any one else who ends up trying to do something similar—you can always create custom query functionality by directly calling the eg: export default withApollo(MyComponent);
function MyComponent({ client }) {
console.log(client.query({
query: GET_DOG_PHOTO,
variables: { breed: "bulldog" }
}));
// Promise {<pending>}
} |
@TimMikeladze but how to pass different query params to each one query ? query Foo {
queryOne { ... }
queryTwo { ... }
queryThree { ... }
}
graphql<IProps, Query>(Foo, {
options: ({ queryParams }) => ({
fetchPolicy: 'network-only',
variables: {
params: queryParams,
},
}),
})(Comp); |
Intended outcome:
I have a React component that needs 7 GraphQL queries to finish firing before I can render the page. I need an
error
state, aloading
state, and of course a final state generated by combining responses from all of the queries.I want to do this concisely and cleanly.
I would like to do something similar to the behavior in
Promise.all()
:How can I achieve this? I could definitely roll my own fairly easily but if there is a built-in way to achieve this I would love to use it.
Actual outcome:
The best solution I can come up with is something like:
But that is:
Promise.all
)How to reproduce the issue:
Not applicable.
Version
The text was updated successfully, but these errors were encountered: