1
1
# type: ignore
2
2
3
+ from itertools import starmap , repeat
4
+ from typing import Union
3
5
from graphql .execution import execute
4
6
from graphql .language .parser import parse
5
7
from graphql .type import (
@@ -51,36 +53,44 @@ def test_executes_using_a_schema():
51
53
{
52
54
"id" : GraphQLField (GraphQLNonNull (GraphQLString )),
53
55
"isPublished" : GraphQLField (GraphQLBoolean ),
56
+ "topic" : GraphQLField (GraphQLString ),
54
57
"author" : GraphQLField (BlogAuthor ),
55
58
"title" : GraphQLField (GraphQLString ),
56
59
"body" : GraphQLField (GraphQLString ),
57
60
"keywords" : GraphQLField (GraphQLList (GraphQLString )),
58
61
},
59
62
)
60
63
64
+ def _resolve_article (obj , info , id , topic ):
65
+ return Article (id , topic )
66
+
67
+ def _resolve_feed (* _ ):
68
+ return list (starmap (Article , zip (range (1 , 10 + 1 ), repeat ("food" ))))
69
+
61
70
BlogQuery = GraphQLObjectType (
62
71
"Query" ,
63
72
{
64
73
"article" : GraphQLField (
65
74
BlogArticle ,
66
- args = {"id" : GraphQLArgument (GraphQLID )},
67
- resolver = lambda obj , info , ** args : Article (args ["id" ]),
68
- ),
69
- "feed" : GraphQLField (
70
- GraphQLList (BlogArticle ),
71
- resolver = lambda * _ : map (Article , range (1 , 10 + 1 )),
75
+ args = {
76
+ "id" : GraphQLArgument (GraphQLID ),
77
+ "topic" : GraphQLArgument (GraphQLNonNull (GraphQLString )),
78
+ },
79
+ resolver = _resolve_article ,
72
80
),
81
+ "feed" : GraphQLField (GraphQLList (BlogArticle ), resolver = _resolve_feed ),
73
82
},
74
83
)
75
84
76
85
BlogSchema = GraphQLSchema (BlogQuery )
77
86
78
87
class Article (object ):
79
- def __init__ (self , id ):
80
- # type: (int) -> None
88
+ def __init__ (self , id , topic ):
89
+ # type: (int, Union[str, None] ) -> None
81
90
self .id = id
82
91
self .isPublished = True
83
92
self .author = Author ()
93
+ self .topic = "My topic is {}" .format (topic or "null" )
84
94
self .title = "My Article {}" .format (id )
85
95
self .body = "This is a post"
86
96
self .hidden = "This data is not exposed in the schema"
@@ -97,7 +107,7 @@ def pic(self, width, height):
97
107
@property
98
108
def recentArticle (self ):
99
109
# type: () -> Article
100
- return Article (1 )
110
+ return Article (1 , "food" )
101
111
102
112
class Pic (object ):
103
113
def __init__ (self , uid , width , height ):
@@ -112,7 +122,7 @@ def __init__(self, uid, width, height):
112
122
id,
113
123
title
114
124
},
115
- article(id: "1") {
125
+ article(id: "1", topic: null ) {
116
126
...articleFields,
117
127
author {
118
128
id,
@@ -132,6 +142,7 @@ def __init__(self, uid, width, height):
132
142
fragment articleFields on Article {
133
143
id,
134
144
isPublished,
145
+ topic,
135
146
title,
136
147
body,
137
148
hidden,
@@ -159,6 +170,7 @@ def __init__(self, uid, width, height):
159
170
"article" : {
160
171
"id" : "1" ,
161
172
"isPublished" : True ,
173
+ "topic" : "My topic is null" ,
162
174
"title" : "My Article 1" ,
163
175
"body" : "This is a post" ,
164
176
"author" : {
@@ -168,6 +180,7 @@ def __init__(self, uid, width, height):
168
180
"recentArticle" : {
169
181
"id" : "1" ,
170
182
"isPublished" : True ,
183
+ "topic" : "My topic is food" ,
171
184
"title" : "My Article 1" ,
172
185
"body" : "This is a post" ,
173
186
"keywords" : ["foo" , "bar" , "1" , "true" , None ],
0 commit comments