Modify documentation to be more clear about resolvers being static

Fixes #951
This commit is contained in:
Allard Hoeve 2019-04-25 13:40:43 +02:00
parent abff3d75a3
commit eb0c2d8e25
8 changed files with 28 additions and 9 deletions

View File

@ -111,8 +111,10 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
best_friend = graphene.Field(lambda: User) best_friend = graphene.Field(lambda: User)
friends = graphene.List(lambda: User) friends = graphene.List(lambda: User)
def resolve_best_friend(self, info): @staticmethod
return user_loader.load(self.best_friend_id) def resolve_best_friend(root, info):
return user_loader.load(root.best_friend_id)
def resolve_friends(self, info): @staticmethod
return user_loader.load_many(self.friend_ids) def resolve_friends(root, info):
return user_loader.load_many(root.friend_ids)

View File

@ -24,6 +24,7 @@ You can pass context to a query via ``context``.
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
name = graphene.String() name = graphene.String()
@staticmethod
def resolve_name(root, info): def resolve_name(root, info):
return info.context.get('name') return info.context.get('name')
@ -43,6 +44,7 @@ You can pass variables to a query via ``variables``.
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
user = graphene.Field(User, id=graphene.ID(required=True)) user = graphene.Field(User, id=graphene.ID(required=True))
@staticmethod
def resolve_user(root, info, id): def resolve_user(root, info, id):
return get_user_by_id(id) return get_user_by_id(id)

View File

@ -29,6 +29,7 @@ This middleware only continues evaluation if the ``field_name`` is not ``'user'`
.. code:: python .. code:: python
class AuthorizationMiddleware(object): class AuthorizationMiddleware(object):
@staticmethod
def resolve(next, root, info, **args): def resolve(next, root, info, **args):
if info.field_name == 'user': if info.field_name == 'user':
return None return None

View File

@ -40,7 +40,8 @@ one field: ``hello`` and an input name. And when we query it, it should return `
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.String(argument=graphene.String(default_value="stranger")) hello = graphene.String(argument=graphene.String(default_value="stranger"))
def resolve_hello(self, info, argument): @staticmethod
def resolve_hello(root, info, argument):
return 'Hello ' + argument return 'Hello ' + argument
schema = graphene.Schema(query=Query) schema = graphene.Schema(query=Query)

View File

@ -41,5 +41,6 @@ that implements ``Node`` will have a default Connection.
name = graphene.String() name = graphene.String()
ships = relay.ConnectionField(ShipConnection) ships = relay.ConnectionField(ShipConnection)
def resolve_ships(self, info): @staticmethod
def resolve_ships(root, info):
return [] return []

View File

@ -82,6 +82,7 @@ For example, you can define a field ``hero`` that resolves to any
episode=graphene.Int(required=True) episode=graphene.Int(required=True)
) )
@staticmethod
def resolve_hero(_, info, episode): def resolve_hero(_, info, episode):
# Luke is the hero of Episode V # Luke is the hero of Episode V
if episode == 5: if episode == 5:

View File

@ -19,7 +19,8 @@ This example defines a Mutation:
ok = graphene.Boolean() ok = graphene.Boolean()
person = graphene.Field(lambda: Person) person = graphene.Field(lambda: Person)
def mutate(self, info, name): @staticmethod
def mutate(root, info, name):
person = Person(name=name) person = Person(name=name)
ok = True ok = True
return CreatePerson(person=person, ok=ok) return CreatePerson(person=person, ok=ok)
@ -32,7 +33,8 @@ resolved.
only argument for the mutation. only argument for the mutation.
**mutate** is the function that will be applied once the mutation is **mutate** is the function that will be applied once the mutation is
called. called. This function acts as a resolver function and as such is always
called as a static method.
So, we can finish our schema like this: So, we can finish our schema like this:
@ -157,7 +159,8 @@ To return an existing ObjectType instead of a mutation-specific type, set the **
Output = Person Output = Person
def mutate(self, info, name): @staticmethod
def mutate(root, info, name):
return Person(name=name) return Person(name=name)
Then, if we query (``schema.execute(query_str)``) the following: Then, if we query (``schema.execute(query_str)``) the following:

View File

@ -25,6 +25,7 @@ This example model defines a Person, with a first and a last name:
last_name = graphene.String() last_name = graphene.String()
full_name = graphene.String() full_name = graphene.String()
@staticmethod
def resolve_full_name(root, info): def resolve_full_name(root, info):
return '{} {}'.format(root.first_name, root.last_name) return '{} {}'.format(root.first_name, root.last_name)
@ -72,10 +73,12 @@ passed to the ``ObjectType``.
me = graphene.Field(Person) me = graphene.Field(Person)
best_friend = graphene.Field(Person) best_friend = graphene.Field(Person)
@staticmethod
def resolve_me(_, info): def resolve_me(_, info):
# returns an object that represents a Person # returns an object that represents a Person
return get_human(name='Luke Skywalker') return get_human(name='Luke Skywalker')
@staticmethod
def resolve_best_friend(_, info): def resolve_best_friend(_, info):
return { return {
"first_name": "R2", "first_name": "R2",
@ -96,6 +99,7 @@ kwargs. For example:
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
human_by_name = graphene.Field(Human, name=graphene.String(required=True)) human_by_name = graphene.Field(Human, name=graphene.String(required=True))
@staticmethod
def resolve_human_by_name(_, info, name): def resolve_human_by_name(_, info, name):
return get_human(name=name) return get_human(name=name)
@ -124,6 +128,7 @@ For example, given this schema:
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.String(required=True, name=graphene.String()) hello = graphene.String(required=True, name=graphene.String())
@staticmethod
def resolve_hello(_, info, name): def resolve_hello(_, info, name):
return name if name else 'World' return name if name else 'World'
@ -149,6 +154,7 @@ into a dict:
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.String(required=True, name=graphene.String()) hello = graphene.String(required=True, name=graphene.String())
@staticmethod
def resolve_hello(_, info, **args): def resolve_hello(_, info, **args):
return args.get('name', 'World') return args.get('name', 'World')
@ -159,6 +165,7 @@ Or by setting a default value for the keyword argument:
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.String(required=True, name=graphene.String()) hello = graphene.String(required=True, name=graphene.String())
@staticmethod
def resolve_hello(_, info, name='World'): def resolve_hello(_, info, name='World'):
return name return name
@ -172,6 +179,7 @@ A field can use a custom resolver from outside the class:
import graphene import graphene
@staticmethod
def resolve_full_name(person, info): def resolve_full_name(person, info):
return '{} {}'.format(person.first_name, person.last_name) return '{} {}'.format(person.first_name, person.last_name)