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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
from textwrap import dedent
from graphql import graphql_sync
from ...types import Interface, ObjectType, Schema
from ...types.scalars import Int, String
from ..node import Node
class CustomNode(Node):
class Meta:
name = "Node"
@staticmethod
def to_global_id(type_, id):
return id
@staticmethod
def get_node_from_global_id(info, id, only_type=None):
assert info.schema is graphql_schema
if id in user_data:
return user_data.get(id)
else:
return photo_data.get(id)
class BasePhoto(Interface):
width = Int(description="The width of the photo in pixels")
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String(description="The full name of the user")
class Photo(ObjectType):
class Meta:
interfaces = [CustomNode, BasePhoto]
user_data = {"1": User(id="1", name="John Doe"), "2": User(id="2", name="Jane Smith")}
photo_data = {"3": Photo(id="3", width=300), "4": Photo(id="4", width=400)}
class RootQuery(ObjectType):
node = CustomNode.Field()
schema = Schema(query=RootQuery, types=[User, Photo])
graphql_schema = schema.graphql_schema
def test_str_schema_correct():
assert (
str(schema).strip()
== dedent(
'''
schema {
query: RootQuery
}
type User implements Node {
"""The ID of the object"""
id: ID!
"""The full name of the user"""
name: String
}
interface Node {
"""The ID of the object"""
id: ID!
}
type Photo implements Node & BasePhoto {
"""The ID of the object"""
id: ID!
"""The width of the photo in pixels"""
width: Int
}
interface BasePhoto {
"""The width of the photo in pixels"""
width: Int
}
type RootQuery {
node(
"""The ID of the object"""
id: ID!
): Node
}
'''
).strip()
)
def test_gets_the_correct_id_for_users():
query = """
{
node(id: "1") {
id
}
}
"""
expected = {"node": {"id": "1"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_gets_the_correct_id_for_photos():
query = """
{
node(id: "4") {
id
}
}
"""
expected = {"node": {"id": "4"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_gets_the_correct_name_for_users():
query = """
{
node(id: "1") {
id
... on User {
name
}
}
}
"""
expected = {"node": {"id": "1", "name": "John Doe"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_gets_the_correct_width_for_photos():
query = """
{
node(id: "4") {
id
... on Photo {
width
}
}
}
"""
expected = {"node": {"id": "4", "width": 400}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_gets_the_correct_typename_for_users():
query = """
{
node(id: "1") {
id
__typename
}
}
"""
expected = {"node": {"id": "1", "__typename": "User"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_gets_the_correct_typename_for_photos():
query = """
{
node(id: "4") {
id
__typename
}
}
"""
expected = {"node": {"id": "4", "__typename": "Photo"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_ignores_photo_fragments_on_user():
query = """
{
node(id: "1") {
id
... on Photo {
width
}
}
}
"""
expected = {"node": {"id": "1"}}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_returns_null_for_bad_ids():
query = """
{
node(id: "5") {
id
}
}
"""
expected = {"node": None}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_have_correct_node_interface():
query = """
{
__type(name: "Node") {
name
kind
fields {
name
type {
kind
ofType {
name
kind
}
}
}
}
}
"""
expected = {
"__type": {
"name": "Node",
"kind": "INTERFACE",
"fields": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {"name": "ID", "kind": "SCALAR"},
},
}
],
}
}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
def test_has_correct_node_root_field():
query = """
{
__schema {
queryType {
fields {
name
type {
name
kind
}
args {
name
type {
kind
ofType {
name
kind
}
}
}
}
}
}
}
"""
expected = {
"__schema": {
"queryType": {
"fields": [
{
"name": "node",
"type": {"name": "Node", "kind": "INTERFACE"},
"args": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {"name": "ID", "kind": "SCALAR"},
},
}
],
}
]
}
}
}
result = graphql_sync(graphql_schema, query)
assert not result.errors
assert result.data == expected
|