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

Collapse and Batch queries #520

Closed
schweller opened this issue Oct 27, 2015 · 10 comments
Closed

Collapse and Batch queries #520

schweller opened this issue Oct 27, 2015 · 10 comments

Comments

@schweller
Copy link

I have a Relay.Route that defines multiple queries and Relay is making one call for graphql for each query.

  static queries = {
    firstQuery: (Component) => Relay.QL`
      query {
        fieldOne
      }
    `,
    secondQuery: (Component) => Relay.QL`
      query  {
        fieldTwo
      }
    `
  }

How can I make Relay aggregate those queries in just one request? How this aggregation works?
Thanks in advance!

@steveluscher
Copy link
Contributor

We just had a conversation about what role core can play here. Broadly:

  1. You could create a new batching network layer (and an associated server implementation) that batches those queries together into a single fetch, streams back the results as they're available, and calls request.resolve(…) on each RelayQueryRequest. This is what we do at Facebook.
  2. Relay core could combine each query into one so you get batching by default. Would have to auto-alias each root field (eg. user to user as user0, in case there are multiple user root calls in the batch).

We should carefully trade off against:

a. New demands we place on the implementation of GraphQL servers
b. The ability to maintain choice with respect to how queries are parallelized (client-side? server-side?)
c. The ability to stream responses into the system as they become ready

Looping in @josephsavona and @wincent.

Are you interested in helping with this?

@RavenHursT
Copy link

👍

@schweller
Copy link
Author

Thanks for you response @steveluscher
Currently, I'm going with a solution similar to the first option you pointed out. I feel better for now, since I'm not the only one doing this way, but yes, would be awesome to see this in the core.

@nodkz
Copy link
Contributor

nodkz commented Apr 21, 2016

Can you provide here proper example of merging several RelayQueryRoot into one another?
Right now I wrote own network layer with JWT, last piece is reducing 7 requests into one in sendQueries method.

@josephsavona
Copy link
Contributor

Relay doesn't support "merging" queries together. Instead, we recommend creating a custom network layer that takes the list of queries passed to sendQueries and sends them to the server in a single http request. This is what we do in our internal network layer implementation.

I wrote a bit more about how to do this here, but you can use the getID() method on each query request as a key to identify which data goes with which query.

@nodkz
Copy link
Contributor

nodkz commented Apr 21, 2016

@josephsavona thanks for clarification.

If somebody needs example of BatchNetworkLayer, and wrapping express-graphql you can found it here https://gist.github.com/nodkz/d9a6380d55067192295382e8e490f39f

@josephsavona
Copy link
Contributor

@nodkz would be cool to publish that as an npm module :-)

@KyleAMathews
Copy link
Contributor

+1 to that. I was going to ask the same :-) I'd help maintain it!

On Thu, Apr 21, 2016 at 12:15 PM Joseph Savona [email protected]
wrote:

@nodkz https://github.com/nodkz would be cool to publish that as an npm
module :-)


You are receiving this because you are subscribed to this thread.

Reply to this email directly or view it on GitHub
#520 (comment)

@nodkz
Copy link
Contributor

nodkz commented Apr 22, 2016

@josephsavona @KyleAMathews
I can not publish my gist as npm module. It is too dumb.

So, I spent all current day and made this great thing: https://github.com/nodkz/react-relay-network-layer
With some simple middlewares. Open for review and PRs.

@josephsavona
Copy link
Contributor

@nodkz awesome! Thanks for publishing this! I'm going to go ahead and close this issue :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants