-
Notifications
You must be signed in to change notification settings - Fork 60
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
Adds Variable Batching Proposal #307
Open
michaelstaib
wants to merge
4
commits into
main
Choose a base branch
from
mst/variable-battching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
## B. Appendix: Variable Batching | ||
|
||
This appendix defines an optional extension to the GraphQL-over-HTTP protocol that allows for the batching of multiple sets of variables in a single GraphQL request. | ||
|
||
:: **Variable batching** enables a client to execute the same GraphQL operation multiple times with different sets of variables, all within a single HTTP request. This can significantly reduce the number of HTTP requests required when performing multiple similar operations, thus improving network efficiency and reducing overhead. | ||
|
||
Field batching, such as querying multiple products by ID in a single request, can work in some cases. | ||
|
||
```graphql | ||
query($ids: [ID!]!) { | ||
productsByIds(ids: $ids) { | ||
id | ||
name | ||
} | ||
} | ||
``` | ||
|
||
However, this approach doesn’t work when variables are used within nested selections. | ||
|
||
```graphql | ||
query($ids: [ID!]! $first: Int!) { | ||
productsByIds(ids: $ids) { | ||
id | ||
name | ||
reviews(first: $first) { | ||
id | ||
body | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Variable batching allows a single request to include multiple sets of variables. This approach is particularly useful for GraphQL gateways to fetch multiple entities by their keys and bulk operations, reducing network overhead and improving performance by consolidating multiple operations into a single request. | ||
|
||
### Variable Batching Request | ||
|
||
A server MAY accept a **variable batching request** via `POST`. | ||
|
||
#### Request Parameters | ||
|
||
A _variable batching request_ follows the same structure as a standard GraphQL request, with the key difference being in the `variables` parameter. All other parameters are consistent with the standard GraphQL-over-HTTP protocol: | ||
|
||
- {query} - (_Required_, string): The string representation of the Source Text | ||
of a GraphQL Document as specified in | ||
[the Language section of the GraphQL specification](https://spec.graphql.org/draft/#sec-Language). | ||
- {operationName} - (_Optional_, string): The name of the Operation in the | ||
Document to execute. | ||
- **{variables}** (Required, array of maps): | ||
Instead of a single map, the `variables` parameter is an array of maps. Each map in the array represents a different set of variables to be used in executing the operation. | ||
- {extensions} - (_Optional_, map): This entry is reserved for implementors to | ||
extend the protocol however they see fit. | ||
|
||
### Accept | ||
|
||
A client SHOULD indicate the media types that it supports in responses using the `Accept` HTTP header as specified in [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231). | ||
|
||
For **variable batching requests**, the client SHOULD include the media type `application/graphql+jsonl` in the `Accept` header to indicate that it expects a batched response in JSON Lines format. | ||
|
||
If the client supplies an Accept header, the client SHOULD include the media type `application/graphql+jsonl` in the Accept header. | ||
|
||
If the client does not supply an `Accept` header, the server MAY respond with a default content type of `application/graphql+jsonl`, or it may use any other media type it supports. However, to ensure compatibility and clarity, it is RECOMMENDED that the client explicitly states its preferred media types using the `Accept` header. | ||
|
||
### POST | ||
|
||
A **variable batching request** instructs the server to perform a query or mutation operation multiple times, once for each set of variables provided. The request MUST have a body that contains the values of the _variable batching request_ parameters encoded in the `application/graphql+jsonl` media type, or another media type supported by the server. | ||
|
||
A client MUST indicate the media type of a request body using the `Content-Type` header as specified in [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231). | ||
|
||
A server MUST support POST requests encoded with the `application/json` media type (as outlined in the GraphQL-over-HTTP specification). | ||
|
||
If the client does not supply a `Content-Type` header with a POST request, the server SHOULD reject the request using the appropriate `4xx` status code. | ||
|
||
### JSON Encoding | ||
|
||
When encoded in JSON, a _GraphQL-over-HTTP request_ is encoded as a JSON object | ||
(map), with the properties specified by the GraphQL-over-HTTP request: | ||
|
||
- {query} - the string representation of the Source Text of the Document as | ||
specified in | ||
[the Language section of the GraphQL specification](https://spec.graphql.org/draft/#sec-Language). | ||
- {operationName} - an optional string | ||
- **{variables}** - An array of JSON objects (maps), where each map corresponds to a set of variables to be used in the query. | ||
names and the values of which are the variable values | ||
- {extensions} - an optional object (map) | ||
|
||
#### Example | ||
|
||
If we wanted to execute the following GraphQL query: | ||
|
||
```raw graphql example | ||
query ($id: ID!) { | ||
user(id: $id) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
With the following query variable sets: | ||
|
||
```json example | ||
[ | ||
{ | ||
"id": "QVBJcy5ndXJ1" | ||
}, | ||
{ | ||
"id": "QVBJcy5ndXJ2" | ||
}, | ||
{ | ||
"id": "QVBJcy5ndXJ3" | ||
} | ||
] | ||
``` | ||
|
||
This request could be sent via an HTTP POST to the relevant URL using the JSON | ||
encoding with the headers: | ||
|
||
```headers example | ||
Content-Type: application/json | ||
Accept: application/graphql-response+jsonl | ||
``` | ||
|
||
And the body: | ||
|
||
```json example | ||
{ | ||
"query": "query ($id: ID!) {\n user(id: $id) {\n name\n }\n}", | ||
"variables": [ | ||
{ | ||
"id": "QVBJcy5ndXJ1" | ||
}, | ||
{ | ||
"id": "QVBJcy5ndXJ2" | ||
}, | ||
{ | ||
"id": "QVBJcy5ndXJ3" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### Response | ||
|
||
When a server receives a well-formed _variable batching request_, it MUST return a well‐formed stream of _GraphQL responses_. Each response in the stream corresponds to the result of validating and executing the requested operation with one set of variables. The server's response stream describes the outcome of each operation, including any errors encountered during the execution of the request. | ||
|
||
A server must comply with [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231). | ||
|
||
Each response in the stream follows the standard GraphQL response format, with the addition of a required `variableIndex` field at the top level of each response. The `variableIndex` indicates the index of the variables map from the original request, allowing the client to associate each response with the correct set of variables. | ||
|
||
**Note:** The server MAY respond with results in any order. The `variableIndex` field ensures that clients can correctly match each response to its corresponding set of variables, regardless of the order in which the responses are returned. | ||
|
||
#### Response Structure | ||
|
||
Each line in the JSON Lines (JSONL) response MUST include the following fields: | ||
|
||
- **`variableIndex`** (Required, integer): The index of the corresponding variables map from the original request's `variables` array. This allows clients to match each response with the correct set of variables. | ||
- **`data`** (Optional, map): The data resulting from the execution of the GraphQL operation with the corresponding set of variables. | ||
- **`errors`** (Optional, array): An array of errors, if any were encountered during the execution of the operation. | ||
- **`extensions`** (Optional, map): A map that MAY include additional information about the request and execution, as needed by implementors. | ||
|
||
## Body | ||
|
||
A server MUST indicate the media type of the response with a `Content-Type` header, and SHOULD indicate the encoding (e.g. `application/graphql-response+jsonl; charset=utf-8`). | ||
|
||
TODO benjie: Should we repeat this or should we take this as a given? | ||
|
||
If an `Accept` header is provided, the server MUST respect the given `Accept` | ||
header and attempt to encode the response in the highest priority media type | ||
listed that is supported by the server. | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion in the composite schema working group we will have a single appendix about batching as this would allow for better example that show also both request batching and variable batching in combination.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjie shall we do also some example in here with defer and stream ... I asked that because defer and stream is not yet handled in the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now: no; but would be good to have the text handy anyway.