Improved documentation examples

This commit is contained in:
Syrus Akbary 2017-07-23 23:16:51 -07:00
parent 9769612a44
commit 0e355ee296
7 changed files with 22 additions and 25 deletions

View File

@ -99,8 +99,8 @@ 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, args, context, info): def resolve_best_friend(self):
return user_loader.load(self.best_friend_id) return user_loader.load(self.best_friend_id)
def resolve_friends(self, args, context, info): def resolve_friends(self):
return user_loader.load_many(self.friend_ids) return user_loader.load_many(self.friend_ids)

View File

@ -24,7 +24,8 @@ You can pass context to a query via ``context_value``.
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
name = graphene.String() name = graphene.String()
def resolve_name(self, args, context, info): @graphene.annotate(context=graphene.Context)
def resolve_name(self, context):
return context.get('name') return context.get('name')
schema = graphene.Schema(Query) schema = graphene.Schema(Query)

View File

@ -39,8 +39,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(name=graphene.Argument(graphene.String, default_value="stranger")) hello = graphene.String(name=graphene.Argument(graphene.String, default_value="stranger"))
def resolve_hello(self, args, context, info): def resolve_hello(self, name):
return 'Hello ' + args['name'] return 'Hello ' + name
schema = graphene.Schema(query=Query) schema = graphene.Schema(query=Query)

View File

@ -41,5 +41,5 @@ 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, args, context, info): def resolve_ships(self):
return [] return []

View File

@ -19,9 +19,8 @@ This example defines a Mutation:
ok = graphene.Boolean() ok = graphene.Boolean()
person = graphene.Field(lambda: Person) person = graphene.Field(lambda: Person)
@staticmethod def mutate(self, name):
def mutate(root, args, context, info): person = Person(name=name)
person = Person(name=args.get('name'))
ok = True ok = True
return CreatePerson(person=person, ok=ok) return CreatePerson(person=person, ok=ok)
@ -90,30 +89,29 @@ InputFields are used in mutations to allow nested input data for mutations
To use an InputField you define an InputObjectType that specifies the structure of your input data To use an InputField you define an InputObjectType that specifies the structure of your input data
.. code:: python .. code:: python
import graphene import graphene
class PersonInput(graphene.InputObjectType): class PersonInput(graphene.InputObjectType):
name = graphene.String() name = graphene.String(required=True)
age = graphene.Int() age = graphene.Int(required=True)
class CreatePerson(graphene.Mutation): class CreatePerson(graphene.Mutation):
class Input: class Input:
person_data = graphene.Argument(PersonInput) person_data = PersonInput(required=True)
person = graphene.Field(lambda: Person) person = graphene.Field(Person)
@staticmethod @staticmethod
def mutate(root, args, context, info): def mutate(root, person_data=None):
p_data = args.get('person_data')
name = p_data.get('name') name = p_data.get('name')
age = p_data.get('age') age = p_data.get('age')
person = Person(name=name, age=age) person = Person(
name=person_data.name,
age=person_data.age
)
return CreatePerson(person=person) return CreatePerson(person=person)

View File

@ -25,7 +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()
def resolve_full_name(self, args, context, info): def resolve_full_name(self):
return '{} {}'.format(self.first_name, self.last_name) return '{} {}'.format(self.first_name, self.last_name)
**first\_name** and **last\_name** are fields of the ObjectType. Each **first\_name** and **last\_name** are fields of the ObjectType. Each
@ -71,8 +71,7 @@ method in the class.
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
reverse = graphene.String(word=graphene.String()) reverse = graphene.String(word=graphene.String())
def resolve_reverse(self, args, context, info): def resolve_reverse(self, word):
word = args.get('word')
return word[::-1] return word[::-1]
Resolvers outside the class Resolvers outside the class
@ -84,8 +83,7 @@ A field can use a custom resolver from outside the class:
import graphene import graphene
def reverse(root, args, context, info): def reverse(root, word):
word = args.get('word')
return word[::-1] return word[::-1]
class Query(graphene.ObjectType): class Query(graphene.ObjectType):