Skip to content

Commit 229c716

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#976 from matsimitsu/apollo_multiplex_docs
Add Apollo Query Batching example to Multiplexing docs
2 parents e5b9e94 + 314b1f6 commit 229c716

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

guides/queries/multiplex.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ results = MySchema.multiplex(queries)
4646

4747
`results` will contain the result for each query in `queries`.
4848

49+
## Apollo Query Batching
50+
51+
Apollo sends the batch variables in a `_json` param, you also need to ensure that your schema can handle both batched and non-batched queries, below is an example of the default GraphqlController rewritten to handle Apollo batches:
52+
53+
```ruby
54+
def execute
55+
context = {}
56+
57+
# Apollo sends the params in a _json variable when batching is enabled
58+
# see the Apollo Documentation about query batching: http://dev.apollodata.com/core/network.html#query-batching
59+
result = if params[:_json]
60+
queries = params[:_json].map do |param|
61+
{
62+
query: param[:query],
63+
operation_name: param[:operationName],
64+
variables: ensure_hash(param[:variables]),
65+
context: context
66+
}
67+
end
68+
MySchema.multiplex(queries)
69+
else
70+
MySchema.execute(
71+
params[:query],
72+
operation_name: params[:operationName],
73+
variables: ensure_hash(params[:variables]),
74+
context: context
75+
)
76+
end
77+
78+
render json: result
79+
end
80+
```
81+
4982
## Validation and Error Handling
5083

5184
Each query is validated and {% internal_link "analyzed","/queries/analysis" %} independently. The `results` array may include a mix of successful results and failed results

0 commit comments

Comments
 (0)