diff --git a/docs/execution/dataloader.rst b/docs/execution/dataloader.rst index 3cd36fccc..c88ddb5e6 100644 --- a/docs/execution/dataloader.rst +++ b/docs/execution/dataloader.rst @@ -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) diff --git a/docs/execution/execute.rst b/docs/execution/execute.rst index f4a1e7596..52101507c 100644 --- a/docs/execution/execute.rst +++ b/docs/execution/execute.rst @@ -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) diff --git a/docs/execution/middleware.rst b/docs/execution/middleware.rst index ad109e440..3591fa1df 100644 --- a/docs/execution/middleware.rst +++ b/docs/execution/middleware.rst @@ -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 diff --git a/docs/quickstart.rst b/docs/quickstart.rst index fc333fae2..a9d2d9606 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -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) diff --git a/docs/relay/connection.rst b/docs/relay/connection.rst index c2379cbc1..8ff1d9fe7 100644 --- a/docs/relay/connection.rst +++ b/docs/relay/connection.rst @@ -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 [] diff --git a/docs/types/interfaces.rst b/docs/types/interfaces.rst index f2073d6a3..aff675189 100644 --- a/docs/types/interfaces.rst +++ b/docs/types/interfaces.rst @@ -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: diff --git a/docs/types/mutations.rst b/docs/types/mutations.rst index 8532595ea..d488c4ea5 100644 --- a/docs/types/mutations.rst +++ b/docs/types/mutations.rst @@ -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: diff --git a/docs/types/objecttypes.rst b/docs/types/objecttypes.rst index 18f91bd34..3324ed9aa 100644 --- a/docs/types/objecttypes.rst +++ b/docs/types/objecttypes.rst @@ -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)