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,
)
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too