mirror of
https://github.com/graphql-python/graphene.git
synced 2024-12-01 22:14:02 +03:00
Updated docs adding resolver page with @with_context instructions. Fixed #164
This commit is contained in:
parent
c1c4af8fc9
commit
981a7f665a
|
@ -12,6 +12,7 @@ ga = "UA-12613282-7"
|
||||||
pages = [
|
pages = [
|
||||||
"/docs/interfaces/",
|
"/docs/interfaces/",
|
||||||
"/docs/objecttypes/",
|
"/docs/objecttypes/",
|
||||||
|
"/docs/resolvers/",
|
||||||
"/docs/mutations/",
|
"/docs/mutations/",
|
||||||
"/docs/basic-types/",
|
"/docs/basic-types/",
|
||||||
"/docs/enums/",
|
"/docs/enums/",
|
||||||
|
|
|
@ -58,7 +58,7 @@ class Query(ObjectType):
|
||||||
## User-based Queryset Filtering
|
## User-based Queryset Filtering
|
||||||
|
|
||||||
If you are using `graphql-django-view` you can access Django's request object
|
If you are using `graphql-django-view` you can access Django's request object
|
||||||
via `info.request_context`.
|
via `with_context` decorator.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from graphene import ObjectType
|
from graphene import ObjectType
|
||||||
|
@ -71,18 +71,20 @@ class Query(ObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def resolve_my_posts(self, args, info):
|
@with_context
|
||||||
if not info.request_context.user.is_authenticated():
|
def resolve_my_posts(self, args, context, info):
|
||||||
|
# context will reference to the Django request
|
||||||
|
if not context.user.is_authenticated():
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
return Post.objects.filter(owner=info.request_context.user)
|
return Post.objects.filter(owner=context.user)
|
||||||
```
|
```
|
||||||
|
|
||||||
If you're using your own view, passing the request context into the schema is
|
If you're using your own view, passing the request context into the schema is
|
||||||
simple.
|
simple.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
result = schema.execute(query, request_context=request)
|
result = schema.execute(query, context_value=request)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Filtering ID-based node access
|
## Filtering ID-based node access
|
||||||
|
@ -100,13 +102,14 @@ class PostNode(DjangoNode):
|
||||||
only_fields = ('title', 'content')
|
only_fields = ('title', 'content')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_node(Cls, id, info):
|
@with_context
|
||||||
|
def get_node(Cls, id, context, info):
|
||||||
try:
|
try:
|
||||||
post = Cls._meta.model.objects.get(id=id)
|
post = Cls._meta.model.objects.get(id=id)
|
||||||
except Cls._meta.model.DoesNotExist:
|
except Cls._meta.model.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if post.published or info.request_context.user is post.owner:
|
if post.published or context.user is post.owner:
|
||||||
return Cls(instance)
|
return Cls(instance)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
62
docs/pages/docs/resolvers.md
Normal file
62
docs/pages/docs/resolvers.md
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
---
|
||||||
|
title: Resolvers
|
||||||
|
description: Walkthrough Resolvers
|
||||||
|
---
|
||||||
|
|
||||||
|
# Resolvers
|
||||||
|
|
||||||
|
A resolver is a method that resolves certain field within a `ObjectType`.
|
||||||
|
The resolver of a field will be, if not specified otherwise, the `resolve_{field_name}` within the `ObjectType`.
|
||||||
|
|
||||||
|
By default a resolver will take the `args`, and `info` arguments.
|
||||||
|
*This is likely to be simplified in the future*.
|
||||||
|
|
||||||
|
|
||||||
|
## Quick example
|
||||||
|
|
||||||
|
This example model defines a `Query` type, which has a reverse field that reverses the given `word`
|
||||||
|
argument using the `resolve_reverse` method in the class.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
reverse = graphene.String(word=graphene.String())
|
||||||
|
|
||||||
|
def resolve_reverse(self, args, info):
|
||||||
|
word = args.get('word')
|
||||||
|
return word[::-1]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Resolvers outside the class
|
||||||
|
|
||||||
|
A field could also specify a custom resolver outside the class:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
def reverse(root, args, info):
|
||||||
|
word = args.get('word')
|
||||||
|
return word[::-1]
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
reverse = graphene.String(word=graphene.String(), resolver=reverse)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
A query in a GraphQL schema could have some context that we can use in any resolver.
|
||||||
|
In this case we need to decorate the resolver function with `with_context`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
name = graphene.String()
|
||||||
|
|
||||||
|
@with_context
|
||||||
|
def resolve_name(self, args, context, info):
|
||||||
|
return context['name']
|
||||||
|
|
||||||
|
|
||||||
|
result = schema.execute(query, context_value={'name': 'Peter'})
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user