Skip to content

Commit 112145b

Browse files
committed
simplify validation harness by extracting union types
Replicates graphql/graphql-js@2490f44
1 parent e8459cb commit 112145b

File tree

4 files changed

+141
-50
lines changed

4 files changed

+141
-50
lines changed

tests/validation/harness.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@
1515

1616
test_schema = build_schema(
1717
"""
18-
interface Being {
19-
name(surname: Boolean): String
20-
}
21-
22-
interface Mammal {
18+
interface Mammal {
2319
mother: Mammal
2420
father: Mammal
2521
}
2622
27-
interface Pet implements Being {
23+
interface Pet {
2824
name(surname: Boolean): String
2925
}
3026
31-
interface Canine implements Mammal & Being {
27+
interface Canine implements Mammal {
3228
name(surname: Boolean): String
3329
mother: Canine
3430
father: Canine
@@ -40,7 +36,7 @@
4036
DOWN
4137
}
4238
43-
type Dog implements Being & Pet & Mammal & Canine {
39+
type Dog implements Pet & Mammal & Canine {
4440
name(surname: Boolean): String
4541
nickname: String
4642
barkVolume: Int
@@ -52,7 +48,7 @@
5248
father: Dog
5349
}
5450
55-
type Cat implements Being & Pet {
51+
type Cat implements Pet {
5652
name(surname: Boolean): String
5753
nickname: String
5854
meows: Boolean
@@ -62,27 +58,12 @@
6258
6359
union CatOrDog = Cat | Dog
6460
65-
interface Intelligent {
66-
iq: Int
67-
}
68-
69-
type Human implements Being & Intelligent {
61+
type Human {
7062
name(surname: Boolean): String
7163
pets: [Pet]
7264
relatives: [Human]
73-
iq: Int
7465
}
7566
76-
type Alien implements Being & Intelligent {
77-
name(surname: Boolean): String
78-
numEyes: Int
79-
iq: Int
80-
}
81-
82-
union DogOrHuman = Dog | Human
83-
84-
union HumanOrAlien = Human | Alien
85-
8667
enum FurColor {
8768
BROWN
8869
BLACK
@@ -123,13 +104,10 @@
123104
124105
type QueryRoot {
125106
human(id: ID): Human
126-
alien: Alien
127107
dog: Dog
128108
cat: Cat
129109
pet: Pet
130110
catOrDog: CatOrDog
131-
dogOrHuman: DogOrHuman
132-
humanOrAlien: HumanOrAlien
133111
complicatedArgs: ComplicatedArgs
134112
}
135113

tests/validation/test_fields_on_correct_type.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,41 @@
77

88
from .harness import assert_validation_errors
99

10-
assert_errors = partial(assert_validation_errors, FieldsOnCorrectTypeRule)
10+
test_schema = build_schema(
11+
"""
12+
interface Pet {
13+
name: String
14+
}
15+
16+
type Dog implements Pet {
17+
name: String
18+
nickname: String
19+
barkVolume: Int
20+
}
21+
22+
type Cat implements Pet {
23+
name: String
24+
nickname: String
25+
meowVolume: Int
26+
}
27+
28+
union CatOrDog = Cat | Dog
29+
30+
type Human {
31+
name: String
32+
pets: [Pet]
33+
}
34+
35+
type Query {
36+
human: Human
37+
}
38+
"""
39+
)
40+
41+
42+
assert_errors = partial(
43+
assert_validation_errors, FieldsOnCorrectTypeRule, schema=test_schema
44+
)
1145

1246
assert_valid = partial(assert_errors, errors=[])
1347

@@ -259,7 +293,7 @@ def defined_on_implementors_queried_on_union():
259293
{
260294
"message": "Cannot query field 'name' on type 'CatOrDog'."
261295
" Did you mean to use an inline fragment"
262-
" on 'Being', 'Pet', 'Canine', 'Cat', or 'Dog'?",
296+
" on 'Pet', 'Cat', or 'Dog'?",
263297
"locations": [(3, 15)],
264298
},
265299
],
@@ -355,7 +389,7 @@ def only_shows_one_set_of_suggestions_at_a_time_preferring_types():
355389
)
356390

357391
def sort_type_suggestions_based_on_inheritance_order():
358-
schema = build_schema(
392+
interface_schema = build_schema(
359393
"""
360394
interface T { bar: String }
361395
type Query { t: T }
@@ -377,11 +411,33 @@ def sort_type_suggestions_based_on_inheritance_order():
377411
"""
378412
)
379413

380-
assert _error_message(schema, "{ t { foo } }") == (
414+
assert _error_message(interface_schema, "{ t { foo } }") == (
381415
"Cannot query field 'foo' on type 'T'."
382416
" Did you mean to use an inline fragment on 'Z', 'Y', or 'X'?"
383417
)
384418

419+
union_schema = build_schema(
420+
"""
421+
interface Animal { name: String }
422+
interface Mammal implements Animal { name: String }
423+
424+
interface Canine implements Animal & Mammal { name: String }
425+
type Dog implements Animal & Mammal & Canine { name: String }
426+
427+
interface Feline implements Animal & Mammal { name: String }
428+
type Cat implements Animal & Mammal & Feline { name: String }
429+
430+
union CatOrDog = Cat | Dog
431+
type Query { catOrDog: CatOrDog }
432+
"""
433+
)
434+
435+
assert _error_message(union_schema, "{ catOrDog { name } }") == (
436+
"Cannot query field 'name' on type 'CatOrDog'."
437+
" Did you mean to use an inline fragment"
438+
" on 'Animal', 'Mammal', 'Canine', 'Dog', or 'Feline'?"
439+
)
440+
385441
def limits_lots_of_type_suggestions():
386442
schema = build_schema(
387443
"""

tests/validation/test_possible_fragment_spreads.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
11
from functools import partial
22

3+
from graphql.utilities import build_schema
34
from graphql.validation import PossibleFragmentSpreadsRule
45

56
from .harness import assert_validation_errors
67

7-
assert_errors = partial(assert_validation_errors, PossibleFragmentSpreadsRule)
8+
test_schema = build_schema(
9+
"""
10+
interface Being {
11+
name: String
12+
}
13+
14+
interface Pet implements Being {
15+
name: String
16+
}
17+
18+
type Dog implements Being & Pet {
19+
name: String
20+
barkVolume: Int
21+
}
22+
23+
type Cat implements Being & Pet {
24+
name: String
25+
meowVolume: Int
26+
}
27+
28+
union CatOrDog = Cat | Dog
29+
30+
interface Intelligent {
31+
iq: Int
32+
}
33+
34+
type Human implements Being & Intelligent {
35+
name: String
36+
pets: [Pet]
37+
iq: Int
38+
}
39+
40+
type Alien implements Being & Intelligent {
41+
name: String
42+
iq: Int
43+
}
44+
45+
union DogOrHuman = Dog | Human
46+
47+
union HumanOrAlien = Human | Alien
48+
49+
type Query {
50+
catOrDog: CatOrDog
51+
dogOrHuman: DogOrHuman
52+
humanOrAlien: HumanOrAlien
53+
}
54+
"""
55+
)
56+
57+
58+
assert_errors = partial(
59+
assert_validation_errors, PossibleFragmentSpreadsRule, schema=test_schema
60+
)
861

962
assert_valid = partial(assert_errors, errors=[])
1063

tests/validation/test_validation.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ def validates_queries():
4949
doc = parse(
5050
"""
5151
query {
52-
catOrDog {
53-
... on Cat {
54-
furColor
55-
}
56-
... on Dog {
57-
isHouseTrained
52+
human {
53+
pets {
54+
... on Cat {
55+
meowsVolume
56+
}
57+
... on Dog {
58+
barkVolume
59+
}
5860
}
5961
}
6062
}
@@ -85,12 +87,14 @@ def deprecated_validates_using_a_custom_type_info():
8587
doc = parse(
8688
"""
8789
query {
88-
catOrDog {
89-
... on Cat {
90-
furColor
91-
}
92-
... on Dog {
93-
isHouseTrained
90+
human {
91+
pets {
92+
... on Cat {
93+
meowsVolume
94+
}
95+
... on Dog {
96+
barkVolume
97+
}
9498
}
9599
}
96100
}
@@ -100,11 +104,11 @@ def deprecated_validates_using_a_custom_type_info():
100104
errors = validate(test_schema, doc, None, None, type_info)
101105

102106
assert [error.message for error in errors] == [
103-
"Cannot query field 'catOrDog' on type 'QueryRoot'."
104-
" Did you mean 'catOrDog'?",
105-
"Cannot query field 'furColor' on type 'Cat'. Did you mean 'furColor'?",
106-
"Cannot query field 'isHouseTrained' on type 'Dog'."
107-
" Did you mean 'isHouseTrained'?",
107+
"Cannot query field 'human' on type 'QueryRoot'. Did you mean 'human'?",
108+
"Cannot query field 'meowsVolume' on type 'Cat'."
109+
" Did you mean 'meowsVolume'?",
110+
"Cannot query field 'barkVolume' on type 'Dog'."
111+
" Did you mean 'barkVolume'?",
108112
]
109113

110114
def validates_using_a_custom_rule():

0 commit comments

Comments
 (0)