mirror of
https://github.com/graphql-python/graphene.git
synced 2025-09-21 11:22:33 +03:00
Modify documentation to be more clear about resolvers being static
Fixes #951
This commit is contained in:
parent
abff3d75a3
commit
eb0c2d8e25
|
@ -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)
|
||||
friends = graphene.List(lambda: User)
|
||||
|
||||
def resolve_best_friend(self, info):
|
||||
return user_loader.load(self.best_friend_id)
|
||||
@staticmethod
|
||||
def resolve_best_friend(root, info):
|
||||
return user_loader.load(root.best_friend_id)
|
||||
|
||||
def resolve_friends(self, info):
|
||||
return user_loader.load_many(self.friend_ids)
|
||||
@staticmethod
|
||||
def resolve_friends(root, info):
|
||||
return user_loader.load_many(root.friend_ids)
|
||||
|
|
|
@ -24,6 +24,7 @@ You can pass context to a query via ``context``.
|
|||
class Query(graphene.ObjectType):
|
||||
name = graphene.String()
|
||||
|
||||
@staticmethod
|
||||
def resolve_name(root, info):
|
||||
return info.context.get('name')
|
||||
|
||||
|
@ -43,6 +44,7 @@ You can pass variables to a query via ``variables``.
|
|||
class Query(graphene.ObjectType):
|
||||
user = graphene.Field(User, id=graphene.ID(required=True))
|
||||
|
||||
@staticmethod
|
||||
def resolve_user(root, info, id):
|
||||
return get_user_by_id(id)
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ This middleware only continues evaluation if the ``field_name`` is not ``'user'`
|
|||
.. code:: python
|
||||
|
||||
class AuthorizationMiddleware(object):
|
||||
@staticmethod
|
||||
def resolve(next, root, info, **args):
|
||||
if info.field_name == 'user':
|
||||
return None
|
||||
|
|
|
@ -40,7 +40,8 @@ one field: ``hello`` and an input name. And when we query it, it should return `
|
|||
class Query(graphene.ObjectType):
|
||||
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
|
||||
|
||||
schema = graphene.Schema(query=Query)
|
||||
|
|
|
@ -41,5 +41,6 @@ that implements ``Node`` will have a default Connection.
|
|||
name = graphene.String()
|
||||
ships = relay.ConnectionField(ShipConnection)
|
||||
|
||||
def resolve_ships(self, info):
|
||||
@staticmethod
|
||||
def resolve_ships(root, info):
|
||||
return []
|
||||
|
|
|
@ -82,6 +82,7 @@ For example, you can define a field ``hero`` that resolves to any
|
|||
episode=graphene.Int(required=True)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def resolve_hero(_, info, episode):
|
||||
# Luke is the hero of Episode V
|
||||
if episode == 5:
|
||||
|
|
|
@ -19,7 +19,8 @@ This example defines a Mutation:
|
|||
ok = graphene.Boolean()
|
||||
person = graphene.Field(lambda: Person)
|
||||
|
||||
def mutate(self, info, name):
|
||||
@staticmethod
|
||||
def mutate(root, info, name):
|
||||
person = Person(name=name)
|
||||
ok = True
|
||||
return CreatePerson(person=person, ok=ok)
|
||||
|
@ -32,7 +33,8 @@ resolved.
|
|||
only argument for the mutation.
|
||||
|
||||
**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:
|
||||
|
||||
|
@ -157,7 +159,8 @@ To return an existing ObjectType instead of a mutation-specific type, set the **
|
|||
|
||||
Output = Person
|
||||
|
||||
def mutate(self, info, name):
|
||||
@staticmethod
|
||||
def mutate(root, info, name):
|
||||
return Person(name=name)
|
||||
|
||||
Then, if we query (``schema.execute(query_str)``) the following:
|
||||
|
|
|
@ -25,6 +25,7 @@ This example model defines a Person, with a first and a last name:
|
|||
last_name = graphene.String()
|
||||
full_name = graphene.String()
|
||||
|
||||
@staticmethod
|
||||
def resolve_full_name(root, info):
|
||||
return '{} {}'.format(root.first_name, root.last_name)
|
||||
|
||||
|
@ -72,10 +73,12 @@ passed to the ``ObjectType``.
|
|||
me = graphene.Field(Person)
|
||||
best_friend = graphene.Field(Person)
|
||||
|
||||
@staticmethod
|
||||
def resolve_me(_, info):
|
||||
# returns an object that represents a Person
|
||||
return get_human(name='Luke Skywalker')
|
||||
|
||||
@staticmethod
|
||||
def resolve_best_friend(_, info):
|
||||
return {
|
||||
"first_name": "R2",
|
||||
|
@ -96,6 +99,7 @@ kwargs. For example:
|
|||
class Query(graphene.ObjectType):
|
||||
human_by_name = graphene.Field(Human, name=graphene.String(required=True))
|
||||
|
||||
@staticmethod
|
||||
def resolve_human_by_name(_, info, name):
|
||||
return get_human(name=name)
|
||||
|
||||
|
@ -124,6 +128,7 @@ For example, given this schema:
|
|||
class Query(graphene.ObjectType):
|
||||
hello = graphene.String(required=True, name=graphene.String())
|
||||
|
||||
@staticmethod
|
||||
def resolve_hello(_, info, name):
|
||||
return name if name else 'World'
|
||||
|
||||
|
@ -149,6 +154,7 @@ into a dict:
|
|||
class Query(graphene.ObjectType):
|
||||
hello = graphene.String(required=True, name=graphene.String())
|
||||
|
||||
@staticmethod
|
||||
def resolve_hello(_, info, **args):
|
||||
return args.get('name', 'World')
|
||||
|
||||
|
@ -159,6 +165,7 @@ Or by setting a default value for the keyword argument:
|
|||
class Query(graphene.ObjectType):
|
||||
hello = graphene.String(required=True, name=graphene.String())
|
||||
|
||||
@staticmethod
|
||||
def resolve_hello(_, info, name='World'):
|
||||
return name
|
||||
|
||||
|
@ -172,6 +179,7 @@ A field can use a custom resolver from outside the class:
|
|||
|
||||
import graphene
|
||||
|
||||
@staticmethod
|
||||
def resolve_full_name(person, info):
|
||||
return '{} {}'.format(person.first_name, person.last_name)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user