mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Improved python syntax
This commit is contained in:
parent
d46e957863
commit
0a395fec58
|
@ -1,10 +1,10 @@
|
||||||
humanData = {}
|
human_data = {}
|
||||||
droidData = {}
|
droid_data = {}
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
from .schema import Human, Droid
|
from .schema import Human, Droid
|
||||||
global humanData, droidData
|
global human_data, droid_data
|
||||||
luke = Human(
|
luke = Human(
|
||||||
id='1000',
|
id='1000',
|
||||||
name='Luke Skywalker',
|
name='Luke Skywalker',
|
||||||
|
@ -45,7 +45,7 @@ def setup():
|
||||||
home_planet=None,
|
home_planet=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
humanData = {
|
human_data = {
|
||||||
'1000': luke,
|
'1000': luke,
|
||||||
'1001': vader,
|
'1001': vader,
|
||||||
'1002': han,
|
'1002': han,
|
||||||
|
@ -69,29 +69,29 @@ def setup():
|
||||||
primary_function='Astromech',
|
primary_function='Astromech',
|
||||||
)
|
)
|
||||||
|
|
||||||
droidData = {
|
droid_data = {
|
||||||
'2000': threepio,
|
'2000': threepio,
|
||||||
'2001': artoo,
|
'2001': artoo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def getCharacter(id):
|
def get_character(id):
|
||||||
return humanData.get(id) or droidData.get(id)
|
return human_data.get(id) or droid_data.get(id)
|
||||||
|
|
||||||
|
|
||||||
def getFriends(character):
|
def get_friends(character):
|
||||||
return map(getCharacter, character.friends)
|
return map(get_character, character.friends)
|
||||||
|
|
||||||
|
|
||||||
def getHero(episode):
|
def get_hero(episode):
|
||||||
if episode == 5:
|
if episode == 5:
|
||||||
return humanData['1000']
|
return human_data['1000']
|
||||||
return droidData['2001']
|
return droid_data['2001']
|
||||||
|
|
||||||
|
|
||||||
def getHuman(id):
|
def get_human(id):
|
||||||
return humanData.get(id)
|
return human_data.get(id)
|
||||||
|
|
||||||
|
|
||||||
def getDroid(id):
|
def get_droid(id):
|
||||||
return droidData.get(id)
|
return droid_data.get(id)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import graphene
|
||||||
from graphene import resolve_only_args
|
from graphene import resolve_only_args
|
||||||
from graphql.core.type import GraphQLEnumValue
|
from graphql.core.type import GraphQLEnumValue
|
||||||
|
|
||||||
from .data import getCharacter, getDroid, getHero, getHuman
|
from .data import get_character, get_droid, get_hero, get_human
|
||||||
|
|
||||||
Episode = graphene.Enum('Episode', dict(
|
Episode = graphene.Enum('Episode', dict(
|
||||||
NEWHOPE=GraphQLEnumValue(4),
|
NEWHOPE=GraphQLEnumValue(4),
|
||||||
|
@ -19,7 +19,7 @@ class Character(graphene.Interface):
|
||||||
|
|
||||||
def resolve_friends(self, args, *_):
|
def resolve_friends(self, args, *_):
|
||||||
# The character friends is a list of strings
|
# The character friends is a list of strings
|
||||||
return [getCharacter(f) for f in self.friends]
|
return [get_character(f) for f in self.friends]
|
||||||
|
|
||||||
|
|
||||||
class Human(Character):
|
class Human(Character):
|
||||||
|
@ -43,15 +43,15 @@ class Query(graphene.ObjectType):
|
||||||
|
|
||||||
@resolve_only_args
|
@resolve_only_args
|
||||||
def resolve_hero(self, episode=None):
|
def resolve_hero(self, episode=None):
|
||||||
return getHero(episode)
|
return get_hero(episode)
|
||||||
|
|
||||||
@resolve_only_args
|
@resolve_only_args
|
||||||
def resolve_human(self, id):
|
def resolve_human(self, id):
|
||||||
return getHuman(id)
|
return get_human(id)
|
||||||
|
|
||||||
@resolve_only_args
|
@resolve_only_args
|
||||||
def resolve_droid(self, id):
|
def resolve_droid(self, id):
|
||||||
return getDroid(id)
|
return get_droid(id)
|
||||||
|
|
||||||
|
|
||||||
Schema = graphene.Schema(query=Query)
|
Schema = graphene.Schema(query=Query)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user