Graphene-django: batch query?

Created on 14 Jul 2017  路  5Comments  路  Source: graphql-python/graphene-django

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?

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:

urlpatterns = [
    url(r'^graphql$', csrf_exempt(PrivateGraphQLView.as_view(schema=schema, batch=True))),
]

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khankuan picture khankuan  路  4Comments

dan-klasson picture dan-klasson  路  4Comments

x9sheikh picture x9sheikh  路  4Comments

licx picture licx  路  3Comments

Northshoot picture Northshoot  路  4Comments