-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 beNone
. Should we be settingdata["extensions"] = None
if there aren't any? (this would apply todata
anderrors
too.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data
anderrors
already work like that, for extensions we always return a dict:strawberry/strawberry/extensions/runner.py
Lines 50 to 56 in 98b4e67
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 :)