-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated docs adding resolver page with @with_context instructions. Fixed
- Loading branch information
1 parent
c1c4af8
commit 981a7f6
Showing
3 changed files
with
99 additions
and
33 deletions.
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
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,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'}) | ||
``` |