I looked through the docs and it's not mentioned anywhere that graphene supports query batching? I do however find it in the source code:
https://github.com/graphql-python/graphene-django/blob/master/graphene_django/views.py#L65
am I missing on some docs?
I took a look at the implementation
# blob/master/graphene_django/views.py
L103
data = self.parse_body(request)
L107
responses = [self.get_response(request, entry) for entry in data]
and it looks like if batch is set to something that evaluates to true, the data is iterated over for entries and send out to get a response, those entries could be different top level queries or mutations in one request: I would imagine that they should be named since those responses are aggregated together into one response.
@hyusetiawan we had the same issue (using apollo client on the front end side) which doesn't expect a collection when using batched queries, they should just be sent as a batch. We ended up with a custom view for that. Here's our version if you're interested:
from graphene_django.views import GraphQLView
class BatchEnabledGraphQLView(GraphQLView):
"""
Modified graphql view that enables batched queries
"""
def __init__(self, **kwargs):
kwargs.update({'batch':True})
super(BatchEnabledGraphQLView, self).__init__(**kwargs)
def get_response(self, request, data, show_graphiql=False):
query, variables, operation_name, id = self.get_graphql_params(request, data)
execution_result = self.execute_graphql_request(
request,
data,
query,
variables,
operation_name,
show_graphiql
)
status_code = 200
if execution_result:
response = {}
if execution_result.errors:
response['errors'] = [self.format_error(e) for e in execution_result.errors]
if execution_result.invalid:
status_code = 400
else:
# this is basically what we needed to change!!
response['data'] = execution_result.data
result = self.json_encode(request, response, pretty=show_graphiql)
else:
result = None
return result, status_code
it's been weeks since I asked and I found that batch query is actually supported via batch=True during the view initialization, for ex, in my urls.py:
urlpatterns = [
url(r'^graphql$', csrf_exempt(PrivateGraphQLView.as_view(schema=schema, batch=True))),
]
@hyusetiawan yes it's supported but as I pointed out, we had the requirement that the response should be sent back as a "normal" response, e.g. all results within "data" ....It might help someone though ;)
Closing this down since it seems we've reached a resolution.
Most helpful comment
it's been weeks since I asked and I found that batch query is actually supported via batch=True during the view initialization, for ex, in my urls.py: