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
|
from graphql.execution import execute_sync
from graphql.language import parse
from graphql.type import (
GraphQLArgument,
GraphQLBoolean,
GraphQLField,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
)
def describe_execute_handles_execution_with_a_complex_schema():
def executes_using_a_schema():
class Article:
# noinspection PyShadowingBuiltins
def __init__(self, id: int):
self.id = id
self.isPublished = True
self.author = JohnSmith()
self.title = f"My Article {id}"
self.body = "This is a post"
self.hidden = "This data is not exposed in the schema"
self.keywords = ["foo", "bar", 1, True, None]
BlogImage = GraphQLObjectType(
"Image",
{
"url": GraphQLField(GraphQLString),
"width": GraphQLField(GraphQLInt),
"height": GraphQLField(GraphQLInt),
},
)
BlogArticle: GraphQLObjectType
BlogAuthor = GraphQLObjectType(
"Author",
lambda: {
"id": GraphQLField(GraphQLString),
"name": GraphQLField(GraphQLString),
"pic": GraphQLField(
BlogImage,
args={
"width": GraphQLArgument(GraphQLInt),
"height": GraphQLArgument(GraphQLInt),
},
resolve=lambda obj, info, width, height: obj.pic(
info, width, height
),
),
"recentArticle": GraphQLField(BlogArticle),
},
)
BlogArticle = GraphQLObjectType(
"Article",
{
"id": GraphQLField(GraphQLNonNull(GraphQLString)),
"isPublished": GraphQLField(GraphQLBoolean),
"author": GraphQLField(BlogAuthor),
"title": GraphQLField(GraphQLString),
"body": GraphQLField(GraphQLString),
"keywords": GraphQLField(GraphQLList(GraphQLString)),
},
)
# noinspection PyShadowingBuiltins
BlogQuery = GraphQLObjectType(
"Query",
{
"article": GraphQLField(
BlogArticle,
args={"id": GraphQLArgument(GraphQLID)},
resolve=lambda _obj, _info, id: Article(id),
),
"feed": GraphQLField(
GraphQLList(BlogArticle),
resolve=lambda *_args: [Article(n + 1) for n in range(10)],
),
},
)
BlogSchema = GraphQLSchema(BlogQuery)
# noinspection PyPep8Naming,PyMethodMayBeStatic
class Author:
def pic(self, info_, width: int, height: int) -> "Pic":
return Pic(123, width, height)
@property
def recentArticle(self) -> Article:
return Article(1)
class JohnSmith(Author):
id = 123
name = "John Smith"
class Pic:
def __init__(self, uid: int, width: int, height: int):
self.url = f"cdn://{uid}"
self.width = f"{width}"
self.height = f"{height}"
document = parse(
"""
{
feed {
id,
title
},
article(id: "1") {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
}
}
fragment articleFields on Article {
id,
isPublished,
title,
body,
hidden,
notDefined
}
"""
)
# Note: this is intentionally not validating to ensure appropriate
# behavior occurs when executing an invalid query.
assert execute_sync(schema=BlogSchema, document=document) == (
{
"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,
"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,
"title": "My Article 1",
"body": "This is a post",
"keywords": ["foo", "bar", "1", "true", None],
},
},
},
},
None,
)
|