mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 04:07:16 +03:00
Improved documentation examples
This commit is contained in:
parent
9769612a44
commit
0e355ee296
|
@ -87,7 +87,7 @@ class User(ObjectType):
|
|||
class Meta:
|
||||
interfaces = [relay.Node]
|
||||
name = String()
|
||||
|
||||
|
||||
class Query(ObjectType):
|
||||
user_connection = relay.ConnectionField(User)
|
||||
```
|
||||
|
|
|
@ -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)
|
||||
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)
|
||||
|
||||
def resolve_friends(self, args, context, info):
|
||||
def resolve_friends(self):
|
||||
return user_loader.load_many(self.friend_ids)
|
||||
|
|
|
@ -24,7 +24,8 @@ You can pass context to a query via ``context_value``.
|
|||
class Query(graphene.ObjectType):
|
||||
name = graphene.String()
|
||||
|
||||
def resolve_name(self, args, context, info):
|
||||
@graphene.annotate(context=graphene.Context)
|
||||
def resolve_name(self, context):
|
||||
return context.get('name')
|
||||
|
||||
schema = graphene.Schema(Query)
|
||||
|
|
|
@ -39,8 +39,8 @@ one field: ``hello`` and an input name. And when we query it, it should return `
|
|||
class Query(graphene.ObjectType):
|
||||
hello = graphene.String(name=graphene.Argument(graphene.String, default_value="stranger"))
|
||||
|
||||
def resolve_hello(self, args, context, info):
|
||||
return 'Hello ' + args['name']
|
||||
def resolve_hello(self, name):
|
||||
return 'Hello ' + name
|
||||
|
||||
schema = graphene.Schema(query=Query)
|
||||
|
||||
|
|
|
@ -41,5 +41,5 @@ that implements ``Node`` will have a default Connection.
|
|||
name = graphene.String()
|
||||
ships = relay.ConnectionField(ShipConnection)
|
||||
|
||||
def resolve_ships(self, args, context, info):
|
||||
def resolve_ships(self):
|
||||
return []
|
||||
|
|
|
@ -19,9 +19,8 @@ This example defines a Mutation:
|
|||
ok = graphene.Boolean()
|
||||
person = graphene.Field(lambda: Person)
|
||||
|
||||
@staticmethod
|
||||
def mutate(root, args, context, info):
|
||||
person = Person(name=args.get('name'))
|
||||
def mutate(self, name):
|
||||
person = Person(name=name)
|
||||
ok = True
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
.. code:: python
|
||||
|
||||
import graphene
|
||||
|
||||
class PersonInput(graphene.InputObjectType):
|
||||
name = graphene.String()
|
||||
age = graphene.Int()
|
||||
name = graphene.String(required=True)
|
||||
age = graphene.Int(required=True)
|
||||
|
||||
class CreatePerson(graphene.Mutation):
|
||||
class Input:
|
||||
person_data = graphene.Argument(PersonInput)
|
||||
person_data = PersonInput(required=True)
|
||||
|
||||
person = graphene.Field(lambda: Person)
|
||||
person = graphene.Field(Person)
|
||||
|
||||
@staticmethod
|
||||
def mutate(root, args, context, info):
|
||||
p_data = args.get('person_data')
|
||||
|
||||
def mutate(root, person_data=None):
|
||||
name = p_data.get('name')
|
||||
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)
|
||||
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ This example model defines a Person, with a first and a last name:
|
|||
last_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)
|
||||
|
||||
**first\_name** and **last\_name** are fields of the ObjectType. Each
|
||||
|
@ -71,8 +71,7 @@ method in the class.
|
|||
class Query(graphene.ObjectType):
|
||||
reverse = graphene.String(word=graphene.String())
|
||||
|
||||
def resolve_reverse(self, args, context, info):
|
||||
word = args.get('word')
|
||||
def resolve_reverse(self, word):
|
||||
return word[::-1]
|
||||
|
||||
Resolvers outside the class
|
||||
|
@ -84,8 +83,7 @@ A field can use a custom resolver from outside the class:
|
|||
|
||||
import graphene
|
||||
|
||||
def reverse(root, args, context, info):
|
||||
word = args.get('word')
|
||||
def reverse(root, word):
|
||||
return word[::-1]
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
|
|
Loading…
Reference in New Issue
Block a user