Skip to content

Commit

Permalink
Modify documentation to be more clear about resolvers being static
Browse files Browse the repository at this point in the history
  • Loading branch information
allardhoeve committed Apr 25, 2019
1 parent abff3d7 commit 6b4d9f2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
14 changes: 8 additions & 6 deletions docs/execution/dataloader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
name = graphene.String()
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)
def resolve_friends(self, info):
return user_loader.load_many(self.friend_ids)
@staticmethod
def resolve_best_friend(root, info):
return user_loader.load(root.best_friend_id)
@staticmethod
def resolve_friends(root, info):
return user_loader.load_many(root.friend_ids)
2 changes: 2 additions & 0 deletions docs/execution/execute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/execution/middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion docs/relay/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
1 change: 1 addition & 0 deletions docs/types/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions docs/types/mutations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions docs/types/objecttypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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'
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 6b4d9f2

Please sign in to comment.