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 execution context class to graphql kwargs #65

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions ariadne_django/views/base.py
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
Expand All @@ -14,8 +14,7 @@
from ariadne.types import ContextValue, ErrorFormatter, ExtensionList, RootValue, ValidationRules

from graphql import GraphQLSchema
from graphql.execution import MiddlewareManager
Copy link

Choose a reason for hiding this comment

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

Please order imports alphabetically.


from graphql.execution import MiddlewareManager, ExecutionContext

Extensions = Union[Callable[[Any, Optional[ContextValue]], ExtensionList], ExtensionList]

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]:
Copy link

Choose a reason for hiding this comment

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

data needs type

if callable(self.context_value):
return self.context_value(request) # pylint: disable=not-callable
return self.context_value or {"request": request}
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Software Development :: Libraries :: Python Modules",
]

Expand All @@ -34,7 +35,7 @@
include_package_data=True,
install_requires=[
"django>=2.2.0",
"ariadne>=0.13.0",
"ariadne>=0.18.0",
],
classifiers=CLASSIFIERS,
platforms=["any"],
Expand Down
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ariadne import MutationType, QueryType, SubscriptionType, make_executable_schema, upload_scalar

import pytest
from graphql import ValidationRule
from graphql import ExecutionContext, ValidationRule


def pytest_configure():
Expand Down Expand Up @@ -190,3 +190,11 @@ class NoopRule(ValidationRule):
pass

return NoopRule


@pytest.fixture
def execution_context_class():
class CustomExecutionContext(ExecutionContext):
pass

return CustomExecutionContext
30 changes: 25 additions & 5 deletions tests/test_configuration.py
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
Copy link

Choose a reason for hiding this comment

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

Suggested change
from graphql import ExecutionContext, GraphQLBoolean, GraphQLResolveInfo, GraphQLScalarType
from graphql import ExecutionContext, GraphQLBoolean, GraphQLResolveInfo, GraphQLScalarType

Please format code with black.

import pytest

from ariadne_django.views import GraphQLView
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down