diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..5a890d3447 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,5 @@ +Release type: minor + +This release adds an extensions field to the `GraphQLHTTPResponse` type and also exposes it in the view's response. + +This field gets populated by Strawberry extensions: https://strawberry.rocks/docs/guides/extensions#get-results diff --git a/strawberry/http.py b/strawberry/http.py index 9beb070d7b..e8862a3f04 100644 --- a/strawberry/http.py +++ b/strawberry/http.py @@ -10,6 +10,7 @@ class GraphQLHTTPResponse(TypedDict, total=False): data: Optional[Dict[str, Any]] errors: Optional[List[Any]] + extensions: Optional[Dict[str, Any]] def process_result(result: ExecutionResult) -> GraphQLHTTPResponse: @@ -17,5 +18,7 @@ def process_result(result: ExecutionResult) -> GraphQLHTTPResponse: if result.errors: data["errors"] = [format_graphql_error(err) for err in result.errors] + if result.extensions: + data["extensions"] = result.extensions return data diff --git a/tests/django/test_extensions.py b/tests/django/test_extensions.py new file mode 100644 index 0000000000..3cbb3a390a --- /dev/null +++ b/tests/django/test_extensions.py @@ -0,0 +1,35 @@ +import json + +from django.test.client import RequestFactory + +import strawberry +from strawberry.django.views import GraphQLView +from strawberry.extensions import Extension + + +class MyExtension(Extension): + def get_results(self): + return {"example": "example"} + + +@strawberry.type +class Query: + hello: str = "strawberry" + + +schema = strawberry.Schema(query=Query, extensions=[MyExtension]) + + +def test_extensions(): + query = "{ hello }" + + factory = RequestFactory() + request = factory.post( + "/graphql/", {"query": query}, content_type="application/json" + ) + + response = GraphQLView.as_view(schema=schema)(request) + data = json.loads(response.content.decode()) + + assert response["content-type"] == "application/json" + assert data["extensions"]["example"] == "example"