Skip to content

Commit df82b0a

Browse files
committed
Storing request object in dict
1 parent e55a9a7 commit df82b0a

4 files changed

Lines changed: 7 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ app.run(before_start=before_start)
3939
### Supported options
4040

4141
- `schema`: The `GraphQLSchema` object that you want the view to execute when it gets a valid request.
42-
- `context`: A value to pass as the `context` to the `graphql()` function.
42+
- `context`: A value to pass as the `context` to the `graphql()` function. By default is set to `dict` with request object at key `request`.
4343
- `root_value`: The `root_value` you want to provide to `executor.execute`.
4444
- `pretty`: Whether or not you want the response to be pretty printed JSON.
4545
- `executor`: The `Executor` that you want to use to execute queries. If an `AsyncExecutor` instance is provided, performs queries asynchronously within executor’s loop.

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Supported options
4242
- ``schema``: The ``GraphQLSchema`` object that you want the view to
4343
execute when it gets a valid request.
4444
- ``context``: A value to pass as the ``context`` to the ``graphql()``
45-
function.
45+
function. By default is set to ``dict`` with request object at key ``request``.
4646
- ``root_value``: The ``root_value`` you want to provide to
4747
``executor.execute``.
4848
- ``pretty``: Whether or not you want the response to be pretty printed

sanic_graphql/graphqlview.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ def get_root_value(self, request):
5959
return self.root_value
6060

6161
def get_context(self, request):
62-
if self.context is not None:
63-
return self.context
64-
return request
62+
context = self.context or {}
63+
if isinstance(context, dict) and 'request' not in context:
64+
context.update({'request': request})
65+
return context
6566

6667
def get_middleware(self, request):
6768
return self.middleware

tests/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def resolve_raises(*_):
1515
fields={
1616
'thrower': GraphQLField(GraphQLNonNull(GraphQLString), resolver=resolve_raises),
1717
'request': GraphQLField(GraphQLNonNull(GraphQLString),
18-
resolver=lambda obj, args, context, info: context.args.get('q')),
18+
resolver=lambda obj, args, context, info: context['request'].args.get('q')),
1919
'context': GraphQLField(GraphQLNonNull(GraphQLString),
2020
resolver=lambda obj, args, context, info: context),
2121
'test': GraphQLField(

0 commit comments

Comments
 (0)