mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 03:23:55 +03:00
Simplified README. Add Django example 💎
This commit is contained in:
parent
8274fcc5d9
commit
311e1048d3
77
README.md
77
README.md
|
@ -20,44 +20,17 @@ class Character(graphene.Interface):
|
||||||
id = graphene.IDField()
|
id = graphene.IDField()
|
||||||
name = graphene.StringField()
|
name = graphene.StringField()
|
||||||
friends = graphene.ListField('self')
|
friends = graphene.ListField('self')
|
||||||
appearsIn = graphene.ListField(Episode)
|
|
||||||
|
|
||||||
def resolve_friends(self, args, *_):
|
def resolve_friends(self, args, *_):
|
||||||
return [wrap_character(getCharacter(f)) for f in self.instance.friends]
|
return [Human(f) for f in self.instance.friends]
|
||||||
|
|
||||||
class Human(Character):
|
class Human(Character):
|
||||||
homePlanet = graphene.StringField()
|
homePlanet = graphene.StringField()
|
||||||
|
|
||||||
|
|
||||||
class Droid(Character):
|
|
||||||
primaryFunction = graphene.StringField()
|
|
||||||
|
|
||||||
|
|
||||||
class Query(graphene.ObjectType):
|
class Query(graphene.ObjectType):
|
||||||
hero = graphene.Field(Character,
|
human = graphene.Field(Human)
|
||||||
episode = graphene.Argument(Episode)
|
|
||||||
)
|
|
||||||
human = graphene.Field(Human,
|
|
||||||
id = graphene.Argument(graphene.String)
|
|
||||||
)
|
|
||||||
droid = graphene.Field(Droid,
|
|
||||||
id = graphene.Argument(graphene.String)
|
|
||||||
)
|
|
||||||
|
|
||||||
@resolve_only_args
|
schema = graphene.Schema(query=Query)
|
||||||
def resolve_hero(self, episode):
|
|
||||||
return wrap_character(getHero(episode))
|
|
||||||
|
|
||||||
@resolve_only_args
|
|
||||||
def resolve_human(self, id):
|
|
||||||
return wrap_character(getHuman(id))
|
|
||||||
|
|
||||||
@resolve_only_args
|
|
||||||
def resolve_droid(self, id):
|
|
||||||
return wrap_character(getDroid(id))
|
|
||||||
|
|
||||||
|
|
||||||
Schema = graphene.Schema(query=Query)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Querying
|
### Querying
|
||||||
|
@ -72,17 +45,14 @@ query = '''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
result = Schema.execute(query)
|
result = schema.execute(query)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Relay Schema
|
### Relay Schema
|
||||||
|
|
||||||
Graphene also supports Relay, check the (Starwars Relay example)[/tests/starwars_relay]!
|
Graphene also supports Relay, check the (Starwars Relay example)[tests/starwars_relay]!
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import graphene
|
|
||||||
from graphene import relay
|
|
||||||
|
|
||||||
class Ship(relay.Node):
|
class Ship(relay.Node):
|
||||||
'''A ship in the Star Wars saga'''
|
'''A ship in the Star Wars saga'''
|
||||||
name = graphene.StringField(description='The name of the ship.')
|
name = graphene.StringField(description='The name of the ship.')
|
||||||
|
@ -92,39 +62,28 @@ class Ship(relay.Node):
|
||||||
return Ship(getShip(id))
|
return Ship(getShip(id))
|
||||||
|
|
||||||
|
|
||||||
class Faction(relay.Node):
|
|
||||||
'''A faction in the Star Wars saga'''
|
|
||||||
name = graphene.StringField(description='The name of the faction.')
|
|
||||||
ships = relay.ConnectionField(Ship, description='The ships used by the faction.')
|
|
||||||
|
|
||||||
@resolve_only_args
|
|
||||||
def resolve_ships(self, **kwargs):
|
|
||||||
return [Ship(getShip(ship)) for ship in self.instance.ships]
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_node(cls, id):
|
|
||||||
return Faction(getFaction(id)
|
|
||||||
|
|
||||||
|
|
||||||
class Query(graphene.ObjectType):
|
class Query(graphene.ObjectType):
|
||||||
rebels = graphene.Field(Faction)
|
ships = relay.ConnectionField(Ship, description='The ships used by the faction.')
|
||||||
empire = graphene.Field(Faction)
|
|
||||||
node = relay.NodeField()
|
node = relay.NodeField()
|
||||||
|
|
||||||
@resolve_only_args
|
@resolve_only_args
|
||||||
def resolve_rebels(self):
|
def resolve_ships(self):
|
||||||
return Faction(getRebels())
|
return [Ship(s) for s in getShips()]
|
||||||
|
|
||||||
@resolve_only_args
|
```
|
||||||
def resolve_empire(self):
|
|
||||||
return Faction(getEmpire())
|
|
||||||
|
|
||||||
|
### Django+Relay Schema
|
||||||
|
|
||||||
Schema = graphene.Schema(query=Query)
|
Graphene also supports Relay, check the (Starwars Django example)[tests/starwars_django]!
|
||||||
|
|
||||||
# Later on, for querying
|
```python
|
||||||
Schema.execute('''rebels { name }''')
|
class Ship(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = YourDjangoModelHere
|
||||||
|
# only_fields = ('id', 'name') # Only map this fields from the model
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
node = relay.NodeField()
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
Loading…
Reference in New Issue
Block a user