-
Notifications
You must be signed in to change notification settings - Fork 11
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 execution context class to graphql kwargs #65
Open
jeroenmuller
wants to merge
4
commits into
mirumee:main
Choose a base branch
from
jeroenmuller:add-execution-context-class-to-graphql-kwargs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import json | ||
from typing import Any, Callable, Optional, Union | ||
from typing import Any, Callable, Optional, Type, Union | ||
|
||
from django.conf import settings | ||
from django.http import HttpRequest | ||
|
@@ -14,8 +14,7 @@ | |
from ariadne.types import ContextValue, ErrorFormatter, ExtensionList, RootValue, ValidationRules | ||
|
||
from graphql import GraphQLSchema | ||
from graphql.execution import MiddlewareManager | ||
|
||
from graphql.execution import MiddlewareManager, ExecutionContext | ||
|
||
Extensions = Union[Callable[[Any, Optional[ContextValue]], ExtensionList], ExtensionList] | ||
|
||
|
@@ -44,6 +43,7 @@ class BaseGraphQLView(TemplateResponseMixin, ContextMixin, View): | |
error_formatter: Optional[ErrorFormatter] = None | ||
extensions: Optional[Extensions] = None | ||
middleware: Optional[MiddlewareManager] = None | ||
execution_context_class: Optional[Type[ExecutionContext]] = None | ||
|
||
def _get(self, request: HttpRequest, *args, **kwargs): # pylint: disable=unused-argument | ||
options = DEFAULT_PLAYGROUND_OPTIONS.copy() | ||
|
@@ -99,9 +99,10 @@ def get_kwargs_graphql(self, request: HttpRequest) -> dict: | |
"error_formatter": self.error_formatter or format_error, | ||
"extensions": extensions, | ||
"middleware": self.middleware, | ||
"execution_context_class": self.execution_context_class or ExecutionContext, | ||
} | ||
|
||
def get_context_for_request(self, request: HttpRequest) -> Optional[ContextValue]: | ||
def get_context_for_request(self, request: HttpRequest, data=None) -> Optional[ContextValue]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if callable(self.context_value): | ||
return self.context_value(request) # pylint: disable=not-callable | ||
return self.context_value or {"request": request} | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
-r requirements.txt | ||
black==22.6.0 | ||
codecov==2.1.11 | ||
codecov>=2.1.11 | ||
django-stubs==1.7.0 | ||
isort==5.7.0 | ||
mypy==0.812 | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
ariadne>=0.13.0 | ||
ariadne>=0.18.0 | ||
django>=2.2 | ||
python-dateutil==2.8.1 |
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
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 | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,18 @@ | ||||||
import json | ||||||
from unittest.mock import ANY, Mock | ||||||
from typing import List | ||||||
from unittest.mock import ANY, Mock, call | ||||||
|
||||||
from django.test import override_settings | ||||||
|
||||||
from ariadne.types import ExtensionSync | ||||||
|
||||||
from graphql.language import FieldNode | ||||||
from graphql.pyutils import Path | ||||||
|
||||||
try: | ||||||
from ariadne.types import ExtensionSync | ||||||
except ImportError: | ||||||
# From ariadne 0.20 Extension supports both sync and async contexts | ||||||
# https://github.com/mirumee/ariadne/blob/main/CHANGELOG.md#020-2023-06-21 | ||||||
from ariadne.types import Extension as ExtensionSync | ||||||
from graphql import ExecutionContext, GraphQLBoolean, GraphQLResolveInfo, GraphQLScalarType | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please format code with black. |
||||||
import pytest | ||||||
|
||||||
from ariadne_django.views import GraphQLView | ||||||
|
@@ -56,6 +64,18 @@ def test_custom_context_value_function_result_is_passed_to_resolvers(request_fac | |||||
assert data == {"data": {"testContext": "TEST-CONTEXT"}} | ||||||
|
||||||
|
||||||
def test_custom_execution_context_is_used_to_execute_operation(mocker, request_factory, schema, execution_context_class): | ||||||
spy_execution_context_execute_operation = mocker.spy(execution_context_class, 'execute_operation') | ||||||
|
||||||
execute_query( | ||||||
request_factory, | ||||||
schema, | ||||||
{"query": "{ status }"}, | ||||||
execution_context_class=execution_context_class, | ||||||
) | ||||||
spy_execution_context_execute_operation.assert_called_once() | ||||||
|
||||||
|
||||||
def test_custom_root_value_is_passed_to_resolvers(request_factory, schema): | ||||||
data = execute_query( | ||||||
request_factory, | ||||||
|
@@ -81,7 +101,7 @@ def test_custom_root_value_function_is_called_with_context_value(request_factory | |||||
context_value={"test": "TEST-CONTEXT"}, | ||||||
root_value=get_root_value, | ||||||
) | ||||||
get_root_value.assert_called_once_with({"test": "TEST-CONTEXT"}, ANY) | ||||||
get_root_value.assert_called_once_with({"test": "TEST-CONTEXT"}, ANY, ANY, ANY) | ||||||
|
||||||
|
||||||
def test_custom_validation_rule_is_called_by_query_validation(mocker, request_factory, schema, validation_rule): | ||||||
|
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.
Please order imports alphabetically.