Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you send multiple operations in a graphql query document when one can only run at a time?

Tags:

graphql

The graphql spec mentions that you can send multiple operations in request document as long as you select the operation to run:

http://facebook.github.io/graphql/October2016/#ExecuteRequest()

To execute a request, the executor must have a parsed Document (as defined in the “Query Language” part of this spec) and a selected operation name to run if the document defines multiple operations, otherwise the document is expected to only contain a single operation.

In what situation, would you send such a document with multiple operations? I can only assume this is to allow for switching between operations without having to create a separate document for each one?

like image 800
Peter Nguyen Avatar asked Apr 08 '18 04:04

Peter Nguyen


1 Answers

I found this answer from the graphql team:

The use is threefold:

  1. It's often useful to store GraphQL documents in a client side file; we use .graphql files on iOS and Android for this. We wanted the parser to work on these files as well (so the parser is universal), but you can (and we often do) include multiple queries in a single document.
  2. One optimization that can be made (and that we'll discuss in more detail in the future, I'm sure) is "persisting" documents; you send the document to the server who stores it for you and gives you an identifier for that, that way you don't have to send the whole document string up every time. If you do that, you can send a document up with all your queries, and then pass the document ID + operation name over the wire. This would optimize bytes being sent from the client to the server.
  3. It's possible to write a "batch" API for GraphQL, where you use the results from one query as the parameters to another. We're still working out the exact details there, but if you do that, it's useful to specify multiple queries in a single document; you'd specify which one you wanted to run at the start and the relationships between them.

Reference: https://github.com/facebook/graphql/issues/29#issuecomment-118994619

like image 115
William Feng Avatar answered Jan 04 '23 06:01

William Feng