-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathtest_execute_schema.py
More file actions
190 lines (174 loc) · 5.45 KB
/
test_execute_schema.py
File metadata and controls
190 lines (174 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# type: ignore
from itertools import starmap, repeat
from typing import Union
from graphql.execution import execute
from graphql.language.parser import parse
from graphql.type import (
GraphQLArgument,
GraphQLBoolean,
GraphQLField,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
)
def test_executes_using_a_schema():
# type: () -> None
BlogImage = GraphQLObjectType(
"BlogImage",
{
"url": GraphQLField(GraphQLString),
"width": GraphQLField(GraphQLInt),
"height": GraphQLField(GraphQLInt),
},
)
BlogAuthor = GraphQLObjectType(
"Author",
lambda: {
"id": GraphQLField(GraphQLString),
"name": GraphQLField(GraphQLString),
"pic": GraphQLField(
BlogImage,
args={
"width": GraphQLArgument(GraphQLInt),
"height": GraphQLArgument(GraphQLInt),
},
resolver=lambda obj, info, **args: obj.pic(
args["width"], args["height"]
),
),
"recentArticle": GraphQLField(BlogArticle),
},
)
BlogArticle = GraphQLObjectType(
"Article",
{
"id": GraphQLField(GraphQLNonNull(GraphQLString)),
"isPublished": GraphQLField(GraphQLBoolean),
"topic": GraphQLField(GraphQLString),
"author": GraphQLField(BlogAuthor),
"title": GraphQLField(GraphQLString),
"body": GraphQLField(GraphQLString),
"keywords": GraphQLField(GraphQLList(GraphQLString)),
},
)
def _resolve_article(obj, info, id, topic):
return Article(id, topic)
def _resolve_feed(*_):
return list(starmap(Article, zip(range(1, 10 + 1), repeat("food"))))
BlogQuery = GraphQLObjectType(
"Query",
{
"article": GraphQLField(
BlogArticle,
args={
"id": GraphQLArgument(GraphQLID),
"topic": GraphQLArgument(GraphQLNonNull(GraphQLString)),
},
resolver=_resolve_article,
),
"feed": GraphQLField(GraphQLList(BlogArticle), resolver=_resolve_feed),
},
)
BlogSchema = GraphQLSchema(BlogQuery)
class Article(object):
def __init__(self, id, topic):
# type: (int, Union[str, None]) -> None
self.id = id
self.isPublished = True
self.author = Author()
self.topic = "My topic is {}".format(topic or "null")
self.title = "My Article {}".format(id)
self.body = "This is a post"
self.hidden = "This data is not exposed in the schema"
self.keywords = ["foo", "bar", 1, True, None]
class Author(object):
id = 123
name = "John Smith"
def pic(self, width, height):
# type: (int, int) -> Pic
return Pic(123, width, height)
@property
def recentArticle(self):
# type: () -> Article
return Article(1, "food")
class Pic(object):
def __init__(self, uid, width, height):
# type: (int, int, int) -> None
self.url = "cdn://{}".format(uid)
self.width = str(width)
self.height = str(height)
request = """
{
feed {
id,
title
},
article(id: "1", topic: null) {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
}
}
fragment articleFields on Article {
id,
isPublished,
topic,
title,
body,
hidden,
notdefined
}
"""
# Note: this is intentionally not validating to ensure appropriate
# behavior occurs when executing an invalid query.
result = execute(BlogSchema, parse(request))
assert not result.errors
assert result.data == {
"feed": [
{"id": "1", "title": "My Article 1"},
{"id": "2", "title": "My Article 2"},
{"id": "3", "title": "My Article 3"},
{"id": "4", "title": "My Article 4"},
{"id": "5", "title": "My Article 5"},
{"id": "6", "title": "My Article 6"},
{"id": "7", "title": "My Article 7"},
{"id": "8", "title": "My Article 8"},
{"id": "9", "title": "My Article 9"},
{"id": "10", "title": "My Article 10"},
],
"article": {
"id": "1",
"isPublished": True,
"topic": "My topic is null",
"title": "My Article 1",
"body": "This is a post",
"author": {
"id": "123",
"name": "John Smith",
"pic": {"url": "cdn://123", "width": 640, "height": 480},
"recentArticle": {
"id": "1",
"isPublished": True,
"topic": "My topic is food",
"title": "My Article 1",
"body": "This is a post",
"keywords": ["foo", "bar", "1", "true", None],
},
},
},
}