Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional extensions to GraphQLHTTPResponse #903

Merged
merged 4 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions strawberry/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
class GraphQLHTTPResponse(TypedDict, total=False):
data: Optional[Dict[str, Any]]
errors: Optional[List[Any]]
extensions: Optional[Dict[str, Any]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is marked Optional but can never be None. Should we be setting data["extensions"] = None if there aren't any? (this would apply to data and errors too.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data and errors already work like that, for extensions we always return a dict:

def get_extensions_results(self) -> Dict[str, Any]:
data = {}
for extension in self.extensions:
data.update(extension.get_results())
return data

I think we can keep them as optional, I'm not sure if we will change the dict to None at somepoint (for example if there's no extension). So I'd say this is fine :)

We can always update this in another PR :)



def process_result(result: ExecutionResult) -> GraphQLHTTPResponse:
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
35 changes: 35 additions & 0 deletions tests/django/test_extensions.py
Original file line number Diff line number Diff line change
@@ -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"