strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

support batched queries

Open Speedy1991 opened this issue 2 years ago • 7 comments

It would be awesome if strawberry would support batched queries

I already spend some time into this and this is my very first working example The error handling is garbage but I hope you get the idea:

# strawberry/http.py
def process_result(result: ExecutionResult) -> Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]:
    if isinstance(result, list):
        data: List[GraphQLHTTPResponse] = {"data": [r.data for r in result]}
        data["errors"] = [[format_graphql_error(err) for err in (getattr(r, 'errors', []) or [])] for r in result]
        if not data["errors"]:
            del data["errors"]
        data["extensions"] = [getattr(r, 'extensions', None) for r in result]
        if not data["extensions"]:
            del data["extensions"]
    else:
        data: GraphQLHTTPResponse = {"data": result.data}
        if result.errors:
            data["errors"] = [format_graphql_error(err) for err in result.errors]
        if result.extensions:
            data["extensions"] = result.extensions

    return data

def parse_request_data(data: Union[Dict, List[Dict]]) -> Union[GraphQLRequestData, List[GraphQLRequestData]]:
    if isinstance(data, list):
        if not all([ d.get('query', None) for d in data]):
            raise MissingQueryError()
        results = [
            GraphQLRequestData(query=d["query"], variables=d.get("variables"), operation_name=d.get("operationName"))
            for d in data
        ]
        return results

    if "query" not in data:
        raise MissingQueryError()

    result = GraphQLRequestData(
        query=data["query"],
        variables=data.get("variables"),
        operation_name=data.get("operationName"),
    )

    return result
# strawberry/django.views.py
if isinstance(request_data, list):
    result = [self.schema.execute_sync(
        rd.query,
        root_value=self.get_root_value(request),
        variable_values=rd.variables,
        context_value=context,
        operation_name=rd.operation_name,
    ) for rd in request_data]

else:
    result = self.schema.execute_sync(
        request_data.query,
        root_value=self.get_root_value(request),
        variable_values=request_data.variables,
        context_value=context,
        operation_name=request_data.operation_name,
    )

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Speedy1991 avatar Oct 28 '21 10:10 Speedy1991