Skip to content

Commit e2b284f

Browse files
committed
Add Star Wars Tests (fixes #7)
1 parent 765640d commit e2b284f

File tree

5 files changed

+699
-1
lines changed

5 files changed

+699
-1
lines changed

graphql/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def graphql(schema, request='', root=None, vars=None, operation_name=None):
1717
schema,
1818
root or object(),
1919
ast,
20+
operation_name,
2021
vars or {},
21-
operation_name
2222
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from collections import namedtuple
2+
3+
Human = namedtuple('Human', 'id name friends appearsIn homePlanet')
4+
5+
luke = Human(
6+
id='1000',
7+
name='Luke Skywalker',
8+
friends=[ '1002', '1003', '2000', '2001' ],
9+
appearsIn=[ 4, 5, 6 ],
10+
homePlanet='Tatooine',
11+
)
12+
13+
vader = Human(
14+
id='1001',
15+
name='Darth Vader',
16+
friends=[ '1004' ],
17+
appearsIn=[ 4, 5, 6 ],
18+
homePlanet='Tatooine',
19+
)
20+
21+
han = Human(
22+
id='1002',
23+
name='Han Solo',
24+
friends=[ '1000', '1003', '2001' ],
25+
appearsIn=[ 4, 5, 6 ],
26+
homePlanet=None,
27+
)
28+
29+
leia = Human(
30+
id='1003',
31+
name='Leia Organa',
32+
friends=[ '1000', '1002', '2000', '2001' ],
33+
appearsIn=[ 4, 5, 6 ],
34+
homePlanet='Alderaan',
35+
)
36+
37+
tarkin = Human(
38+
id='1004',
39+
name='Wilhuff Tarkin',
40+
friends=[ '1001' ],
41+
appearsIn=[ 4 ],
42+
homePlanet=None,
43+
)
44+
45+
humanData = {
46+
'1000': luke,
47+
'1001': vader,
48+
'1002': han,
49+
'1003': leia,
50+
'1004': tarkin,
51+
}
52+
53+
Droid = namedtuple('Droid', 'id name friends appearsIn primaryFunction')
54+
55+
threepio = Droid(
56+
id='2000',
57+
name='C-3PO',
58+
friends=[ '1000', '1002', '1003', '2001' ],
59+
appearsIn=[ 4, 5, 6 ],
60+
primaryFunction='Protocol',
61+
)
62+
63+
artoo = Droid(
64+
id='2001',
65+
name='R2-D2',
66+
friends=[ '1000', '1002', '1003' ],
67+
appearsIn=[ 4, 5, 6 ],
68+
primaryFunction='Astromech',
69+
)
70+
71+
droidData = {
72+
'2000': threepio,
73+
'2001': artoo,
74+
}
75+
76+
def getCharacter(id):
77+
return humanData.get(id) or droidData.get(id)
78+
79+
80+
def getFriends(character):
81+
return map(getCharacter, character.friends)
82+
83+
84+
def getHero(episode):
85+
if episode == 5:
86+
return luke
87+
return artoo
88+
89+
90+
def getHuman(id):
91+
return humanData.get(id)
92+
93+
94+
def getDroid(id):
95+
return droidData.get(id)
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from graphql.core.type import (
2+
GraphQLEnumType,
3+
GraphQLEnumValue,
4+
GraphQLInterfaceType,
5+
GraphQLObjectType,
6+
GraphQLField,
7+
GraphQLArgument,
8+
GraphQLList,
9+
GraphQLNonNull,
10+
GraphQLSchema,
11+
GraphQLString,
12+
)
13+
import starwars_fixtures
14+
15+
episodeEnum = GraphQLEnumType(
16+
'Episode',
17+
description='One of the films in the Star Wars Trilogy',
18+
values={
19+
'NEWHOPE': GraphQLEnumValue(
20+
4,
21+
description='Released in 1977.',
22+
),
23+
'EMPIRE': GraphQLEnumValue(
24+
5,
25+
description='Released in 1980.',
26+
),
27+
'JEDI': GraphQLEnumValue(
28+
6,
29+
description='Released in 1983.',
30+
)
31+
}
32+
)
33+
34+
characterInterface = GraphQLInterfaceType(
35+
'Character',
36+
description='A character in the Star Wars Trilogy',
37+
fields=lambda: {
38+
'id': GraphQLField(
39+
GraphQLNonNull(GraphQLString),
40+
description='The id of the character.'
41+
),
42+
'name': GraphQLField(
43+
GraphQLString,
44+
description='The name of the character.'
45+
),
46+
'friends': GraphQLField(
47+
GraphQLList(characterInterface),
48+
description='The friends of the character, or an empty list if they have none.'
49+
),
50+
'appearsIn': GraphQLField(
51+
GraphQLList(episodeEnum),
52+
description='Which movies they appear in.'
53+
),
54+
},
55+
resolve_type=lambda character, *_: humanType if starwars_fixtures.getHuman(character.id) else droidType,
56+
)
57+
58+
humanType = GraphQLObjectType(
59+
'Human',
60+
description='A humanoid creature in the Star Wars universe.',
61+
fields=lambda: {
62+
'id': GraphQLField(
63+
GraphQLNonNull(GraphQLString),
64+
description='The id of the human.',
65+
),
66+
'name': GraphQLField(
67+
GraphQLString,
68+
description='The name of the human.',
69+
),
70+
'friends': GraphQLField(
71+
GraphQLList(characterInterface),
72+
description='The friends of the human, or an empty list if they have none.',
73+
resolver=lambda human, *_: starwars_fixtures.getFriends(human),
74+
),
75+
'appearsIn': GraphQLField(
76+
GraphQLList(episodeEnum),
77+
description='Which movies they appear in.',
78+
),
79+
'homePlanet': GraphQLField(
80+
GraphQLString,
81+
description='The home planet of the human, or null if unknown.',
82+
)
83+
},
84+
interfaces=[characterInterface]
85+
)
86+
87+
droidType = GraphQLObjectType(
88+
'Droid',
89+
description='A mechanical creature in the Star Wars universe.',
90+
fields=lambda: {
91+
'id': GraphQLField(
92+
GraphQLNonNull(GraphQLString),
93+
description='The id of the droid.',
94+
),
95+
'name': GraphQLField(
96+
GraphQLString,
97+
description='The name of the droid.',
98+
),
99+
'friends': GraphQLField(
100+
GraphQLList(characterInterface),
101+
description='The friends of the droid, or an empty list if they have none.',
102+
resolver=lambda droid, *_: starwars_fixtures.getFriends(droid),
103+
),
104+
'appearsIn': GraphQLField(
105+
GraphQLList(episodeEnum),
106+
description='Which movies they appear in.',
107+
),
108+
'primaryFunction': GraphQLField(
109+
GraphQLString,
110+
description='The primary function of the droid.',
111+
)
112+
},
113+
interfaces=[characterInterface]
114+
)
115+
116+
queryType = GraphQLObjectType(
117+
'Query',
118+
fields=lambda: {
119+
'hero': GraphQLField(
120+
characterInterface,
121+
args={
122+
'episode': GraphQLArgument(
123+
description='If omitted, returns the hero of the whole saga. If '
124+
'provided, returns the hero of that particular episode.',
125+
type=episodeEnum,
126+
)
127+
},
128+
resolver=lambda root, args, *_: starwars_fixtures.getHero(args['episode']),
129+
),
130+
'human': GraphQLField(
131+
humanType,
132+
args={
133+
'id': GraphQLArgument(
134+
description='id of the human',
135+
type=GraphQLNonNull(GraphQLString),
136+
)
137+
},
138+
resolver=lambda root, args, *_: starwars_fixtures.getHuman(args['id']),
139+
),
140+
'droid': GraphQLField(
141+
droidType,
142+
args={
143+
'id': GraphQLArgument(
144+
description='id of the droid',
145+
type=GraphQLNonNull(GraphQLString),
146+
)
147+
},
148+
resolver=lambda root, args, *_: starwars_fixtures.getDroid(args['id']),
149+
),
150+
}
151+
)
152+
153+
StarWarsSchema = GraphQLSchema(query=queryType)

0 commit comments

Comments
 (0)