11# type: ignore
22
3+ from itertools import starmap , repeat
4+ from typing import Union
35from graphql .execution import execute
46from graphql .language .parser import parse
57from graphql .type import (
@@ -51,36 +53,44 @@ def test_executes_using_a_schema():
5153 {
5254 "id" : GraphQLField (GraphQLNonNull (GraphQLString )),
5355 "isPublished" : GraphQLField (GraphQLBoolean ),
56+ "topic" : GraphQLField (GraphQLString ),
5457 "author" : GraphQLField (BlogAuthor ),
5558 "title" : GraphQLField (GraphQLString ),
5659 "body" : GraphQLField (GraphQLString ),
5760 "keywords" : GraphQLField (GraphQLList (GraphQLString )),
5861 },
5962 )
6063
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+
6170 BlogQuery = GraphQLObjectType (
6271 "Query" ,
6372 {
6473 "article" : GraphQLField (
6574 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 ,
7280 ),
81+ "feed" : GraphQLField (GraphQLList (BlogArticle ), resolver = _resolve_feed ),
7382 },
7483 )
7584
7685 BlogSchema = GraphQLSchema (BlogQuery )
7786
7887 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
8190 self .id = id
8291 self .isPublished = True
8392 self .author = Author ()
93+ self .topic = "My topic is {}" .format (topic or "null" )
8494 self .title = "My Article {}" .format (id )
8595 self .body = "This is a post"
8696 self .hidden = "This data is not exposed in the schema"
@@ -97,7 +107,7 @@ def pic(self, width, height):
97107 @property
98108 def recentArticle (self ):
99109 # type: () -> Article
100- return Article (1 )
110+ return Article (1 , "food" )
101111
102112 class Pic (object ):
103113 def __init__ (self , uid , width , height ):
@@ -112,7 +122,7 @@ def __init__(self, uid, width, height):
112122 id,
113123 title
114124 },
115- article(id: "1") {
125+ article(id: "1", topic: null ) {
116126 ...articleFields,
117127 author {
118128 id,
@@ -132,6 +142,7 @@ def __init__(self, uid, width, height):
132142 fragment articleFields on Article {
133143 id,
134144 isPublished,
145+ topic,
135146 title,
136147 body,
137148 hidden,
@@ -159,6 +170,7 @@ def __init__(self, uid, width, height):
159170 "article" : {
160171 "id" : "1" ,
161172 "isPublished" : True ,
173+ "topic" : "My topic is null" ,
162174 "title" : "My Article 1" ,
163175 "body" : "This is a post" ,
164176 "author" : {
@@ -168,6 +180,7 @@ def __init__(self, uid, width, height):
168180 "recentArticle" : {
169181 "id" : "1" ,
170182 "isPublished" : True ,
183+ "topic" : "My topic is food" ,
171184 "title" : "My Article 1" ,
172185 "body" : "This is a post" ,
173186 "keywords" : ["foo" , "bar" , "1" , "true" , None ],
0 commit comments