| 1 |
from graphql.execution import execute_sync |
| 2 |
from graphql.language import parse |
| 3 |
from graphql.type import ( |
| 4 |
GraphQLArgument, |
| 5 |
GraphQLBoolean, |
| 6 |
GraphQLField, |
| 7 |
GraphQLID, |
| 8 |
GraphQLInt, |
| 9 |
GraphQLList, |
| 10 |
GraphQLNonNull, |
| 11 |
GraphQLObjectType, |
| 12 |
GraphQLSchema, |
| 13 |
GraphQLString, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def describe_execute_handles_execution_with_a_complex_schema(): |
| 18 |
def executes_using_a_schema(): |
| 19 |
class Article: |
| 20 |
|
| 21 |
# noinspection PyShadowingBuiltins |
| 22 |
def __init__(self, id: int): |
| 23 |
self.id = id |
| 24 |
self.isPublished = True |
| 25 |
self.author = JohnSmith() |
| 26 |
self.title = f"My Article {id}" |
| 27 |
self.body = "This is a post" |
| 28 |
self.hidden = "This data is not exposed in the schema" |
| 29 |
self.keywords = ["foo", "bar", 1, True, None] |
| 30 |
|
| 31 |
BlogImage = GraphQLObjectType( |
| 32 |
"Image", |
| 33 |
{ |
| 34 |
"url": GraphQLField(GraphQLString), |
| 35 |
"width": GraphQLField(GraphQLInt), |
| 36 |
"height": GraphQLField(GraphQLInt), |
| 37 |
}, |
| 38 |
) |
| 39 |
|
| 40 |
BlogArticle: GraphQLObjectType |
| 41 |
|
| 42 |
BlogAuthor = GraphQLObjectType( |
| 43 |
"Author", |
| 44 |
lambda: { |
| 45 |
"id": GraphQLField(GraphQLString), |
| 46 |
"name": GraphQLField(GraphQLString), |
| 47 |
"pic": GraphQLField( |
| 48 |
BlogImage, |
| 49 |
args={ |
| 50 |
"width": GraphQLArgument(GraphQLInt), |
| 51 |
"height": GraphQLArgument(GraphQLInt), |
| 52 |
}, |
| 53 |
resolve=lambda obj, info, width, height: obj.pic( |
| 54 |
info, width, height |
| 55 |
), |
| 56 |
), |
| 57 |
"recentArticle": GraphQLField(BlogArticle), |
| 58 |
}, |
| 59 |
) |
| 60 |
|
| 61 |
BlogArticle = GraphQLObjectType( |
| 62 |
"Article", |
| 63 |
{ |
| 64 |
"id": GraphQLField(GraphQLNonNull(GraphQLString)), |
| 65 |
"isPublished": GraphQLField(GraphQLBoolean), |
| 66 |
"author": GraphQLField(BlogAuthor), |
| 67 |
"title": GraphQLField(GraphQLString), |
| 68 |
"body": GraphQLField(GraphQLString), |
| 69 |
"keywords": GraphQLField(GraphQLList(GraphQLString)), |
| 70 |
}, |
| 71 |
) |
| 72 |
|
| 73 |
# noinspection PyShadowingBuiltins |
| 74 |
BlogQuery = GraphQLObjectType( |
| 75 |
"Query", |
| 76 |
{ |
| 77 |
"article": GraphQLField( |
| 78 |
BlogArticle, |
| 79 |
args={"id": GraphQLArgument(GraphQLID)}, |
| 80 |
resolve=lambda _obj, _info, id: Article(id), |
| 81 |
), |
| 82 |
"feed": GraphQLField( |
| 83 |
GraphQLList(BlogArticle), |
| 84 |
resolve=lambda *_args: [Article(n + 1) for n in range(10)], |
| 85 |
), |
| 86 |
}, |
| 87 |
) |
| 88 |
|
| 89 |
BlogSchema = GraphQLSchema(BlogQuery) |
| 90 |
|
| 91 |
# noinspection PyPep8Naming,PyMethodMayBeStatic |
| 92 |
class Author: |
| 93 |
def pic(self, info_, width: int, height: int) -> "Pic": |
| 94 |
return Pic(123, width, height) |
| 95 |
|
| 96 |
@property |
| 97 |
def recentArticle(self) -> Article: |
| 98 |
return Article(1) |
| 99 |
|
| 100 |
class JohnSmith(Author): |
| 101 |
id = 123 |
| 102 |
name = "John Smith" |
| 103 |
|
| 104 |
class Pic: |
| 105 |
def __init__(self, uid: int, width: int, height: int): |
| 106 |
self.url = f"cdn://{uid}" |
| 107 |
self.width = f"{width}" |
| 108 |
self.height = f"{height}" |
| 109 |
|
| 110 |
document = parse(""" |
| 111 |
{ |
| 112 |
feed { |
| 113 |
id, |
| 114 |
title |
| 115 |
}, |
| 116 |
article(id: "1") { |
| 117 |
...articleFields, |
| 118 |
author { |
| 119 |
id, |
| 120 |
name, |
| 121 |
pic(width: 640, height: 480) { |
| 122 |
url, |
| 123 |
width, |
| 124 |
height |
| 125 |
}, |
| 126 |
recentArticle { |
| 127 |
...articleFields, |
| 128 |
keywords |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
fragment articleFields on Article { |
| 135 |
id, |
| 136 |
isPublished, |
| 137 |
title, |
| 138 |
body, |
| 139 |
hidden, |
| 140 |
notDefined |
| 141 |
} |
| 142 |
""") |
| 143 |
|
| 144 |
# Note: this is intentionally not validating to ensure appropriate |
| 145 |
# behavior occurs when executing an invalid query. |
| 146 |
assert execute_sync(schema=BlogSchema, document=document) == ( |
| 147 |
{ |
| 148 |
"feed": [ |
| 149 |
{"id": "1", "title": "My Article 1"}, |
| 150 |
{"id": "2", "title": "My Article 2"}, |
| 151 |
{"id": "3", "title": "My Article 3"}, |
| 152 |
{"id": "4", "title": "My Article 4"}, |
| 153 |
{"id": "5", "title": "My Article 5"}, |
| 154 |
{"id": "6", "title": "My Article 6"}, |
| 155 |
{"id": "7", "title": "My Article 7"}, |
| 156 |
{"id": "8", "title": "My Article 8"}, |
| 157 |
{"id": "9", "title": "My Article 9"}, |
| 158 |
{"id": "10", "title": "My Article 10"}, |
| 159 |
], |
| 160 |
"article": { |
| 161 |
"id": "1", |
| 162 |
"isPublished": True, |
| 163 |
"title": "My Article 1", |
| 164 |
"body": "This is a post", |
| 165 |
"author": { |
| 166 |
"id": "123", |
| 167 |
"name": "John Smith", |
| 168 |
"pic": {"url": "cdn://123", "width": 640, "height": 480}, |
| 169 |
"recentArticle": { |
| 170 |
"id": "1", |
| 171 |
"isPublished": True, |
| 172 |
"title": "My Article 1", |
| 173 |
"body": "This is a post", |
| 174 |
"keywords": ["foo", "bar", "1", "true", None], |
| 175 |
}, |
| 176 |
}, |
| 177 |
}, |
| 178 |
}, |
| 179 |
None, |
| 180 |
) |