Skip to content

Commit 2490f44

Browse files
simplify validation harness by extracting union types into approriate… (#3180)
1 parent f9bf263 commit 2490f44

File tree

4 files changed

+135
-49
lines changed

4 files changed

+135
-49
lines changed

src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,49 @@ import { buildSchema } from '../../utilities/buildASTSchema';
1010
import { validate } from '../validate';
1111
import { FieldsOnCorrectTypeRule } from '../rules/FieldsOnCorrectTypeRule';
1212

13-
import { expectValidationErrors } from './harness';
13+
import { expectValidationErrorsWithSchema } from './harness';
1414

1515
function expectErrors(queryStr: string) {
16-
return expectValidationErrors(FieldsOnCorrectTypeRule, queryStr);
16+
return expectValidationErrorsWithSchema(
17+
testSchema,
18+
FieldsOnCorrectTypeRule,
19+
queryStr,
20+
);
1721
}
1822

1923
function expectValid(queryStr: string) {
2024
expectErrors(queryStr).to.deep.equal([]);
2125
}
2226

27+
const testSchema = buildSchema(`
28+
interface Pet {
29+
name: String
30+
}
31+
32+
type Dog implements Pet {
33+
name: String
34+
nickname: String
35+
barkVolume: Int
36+
}
37+
38+
type Cat implements Pet {
39+
name: String
40+
nickname: String
41+
meowVolume: Int
42+
}
43+
44+
union CatOrDog = Cat | Dog
45+
46+
type Human {
47+
name: String
48+
pets: [Pet]
49+
}
50+
51+
type Query {
52+
human: Human
53+
}
54+
`);
55+
2356
describe('Validate: Fields on correct type', () => {
2457
it('Object field selection', () => {
2558
expectValid(`
@@ -237,7 +270,7 @@ describe('Validate: Fields on correct type', () => {
237270
`).to.deep.equal([
238271
{
239272
message:
240-
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Being", "Pet", "Canine", "Cat", or "Dog"?',
273+
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Pet", "Cat", or "Dog"?',
241274
locations: [{ line: 3, column: 9 }],
242275
},
243276
]);
@@ -332,7 +365,7 @@ describe('Validate: Fields on correct type', () => {
332365
});
333366

334367
it('Sort type suggestions based on inheritance order', () => {
335-
const schema = buildSchema(`
368+
const interfaceSchema = buildSchema(`
336369
interface T { bar: String }
337370
type Query { t: T }
338371
@@ -352,9 +385,27 @@ describe('Validate: Fields on correct type', () => {
352385
}
353386
`);
354387

355-
expectErrorMessage(schema, '{ t { foo } }').to.equal(
388+
expectErrorMessage(interfaceSchema, '{ t { foo } }').to.equal(
356389
'Cannot query field "foo" on type "T". Did you mean to use an inline fragment on "Z", "Y", or "X"?',
357390
);
391+
392+
const unionSchema = buildSchema(`
393+
interface Animal { name: String }
394+
interface Mammal implements Animal { name: String }
395+
396+
interface Canine implements Animal & Mammal { name: String }
397+
type Dog implements Animal & Mammal & Canine { name: String }
398+
399+
interface Feline implements Animal & Mammal { name: String }
400+
type Cat implements Animal & Mammal & Feline { name: String }
401+
402+
union CatOrDog = Cat | Dog
403+
type Query { catOrDog: CatOrDog }
404+
`);
405+
406+
expectErrorMessage(unionSchema, '{ catOrDog { name } }').to.equal(
407+
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Animal", "Mammal", "Canine", "Dog", or "Feline"?',
408+
);
358409
});
359410

360411
it('Limits lots of type suggestions', () => {

src/validation/__tests__/PossibleFragmentSpreadsRule-test.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11
import { describe, it } from 'mocha';
22

3+
import { buildSchema } from '../../utilities/buildASTSchema';
4+
35
import { PossibleFragmentSpreadsRule } from '../rules/PossibleFragmentSpreadsRule';
46

5-
import { expectValidationErrors } from './harness';
7+
import { expectValidationErrorsWithSchema } from './harness';
68

79
function expectErrors(queryStr: string) {
8-
return expectValidationErrors(PossibleFragmentSpreadsRule, queryStr);
10+
return expectValidationErrorsWithSchema(
11+
testSchema,
12+
PossibleFragmentSpreadsRule,
13+
queryStr,
14+
);
915
}
1016

1117
function expectValid(queryStr: string) {
1218
expectErrors(queryStr).to.deep.equal([]);
1319
}
1420

21+
const testSchema = buildSchema(`
22+
interface Being {
23+
name: String
24+
}
25+
26+
interface Pet implements Being {
27+
name: String
28+
}
29+
30+
type Dog implements Being & Pet {
31+
name: String
32+
barkVolume: Int
33+
}
34+
35+
type Cat implements Being & Pet {
36+
name: String
37+
meowVolume: Int
38+
}
39+
40+
union CatOrDog = Cat | Dog
41+
42+
interface Intelligent {
43+
iq: Int
44+
}
45+
46+
type Human implements Being & Intelligent {
47+
name: String
48+
pets: [Pet]
49+
iq: Int
50+
}
51+
52+
type Alien implements Being & Intelligent {
53+
name: String
54+
iq: Int
55+
}
56+
57+
union DogOrHuman = Dog | Human
58+
59+
union HumanOrAlien = Human | Alien
60+
61+
type Query {
62+
catOrDog: CatOrDog
63+
dogOrHuman: DogOrHuman
64+
humanOrAlien: HumanOrAlien
65+
}
66+
`);
67+
1568
describe('Validate: Possible fragment spreads', () => {
1669
it('of the same object', () => {
1770
expectValid(`

src/validation/__tests__/harness.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@ import { validate, validateSDL } from '../validate';
1212
import type { ValidationRule, SDLValidationRule } from '../ValidationContext';
1313

1414
export const testSchema: GraphQLSchema = buildSchema(`
15-
interface Being {
16-
name(surname: Boolean): String
17-
}
18-
1915
interface Mammal {
2016
mother: Mammal
2117
father: Mammal
2218
}
2319
24-
interface Pet implements Being {
20+
interface Pet {
2521
name(surname: Boolean): String
2622
}
2723
28-
interface Canine implements Mammal & Being {
24+
interface Canine implements Mammal {
2925
name(surname: Boolean): String
3026
mother: Canine
3127
father: Canine
@@ -37,7 +33,7 @@ export const testSchema: GraphQLSchema = buildSchema(`
3733
DOWN
3834
}
3935
40-
type Dog implements Being & Pet & Mammal & Canine {
36+
type Dog implements Pet & Mammal & Canine {
4137
name(surname: Boolean): String
4238
nickname: String
4339
barkVolume: Int
@@ -49,7 +45,7 @@ export const testSchema: GraphQLSchema = buildSchema(`
4945
father: Dog
5046
}
5147
52-
type Cat implements Being & Pet {
48+
type Cat implements Pet {
5349
name(surname: Boolean): String
5450
nickname: String
5551
meows: Boolean
@@ -59,27 +55,12 @@ export const testSchema: GraphQLSchema = buildSchema(`
5955
6056
union CatOrDog = Cat | Dog
6157
62-
interface Intelligent {
63-
iq: Int
64-
}
65-
66-
type Human implements Being & Intelligent {
58+
type Human {
6759
name(surname: Boolean): String
6860
pets: [Pet]
6961
relatives: [Human]
70-
iq: Int
7162
}
7263
73-
type Alien implements Being & Intelligent {
74-
name(surname: Boolean): String
75-
numEyes: Int
76-
iq: Int
77-
}
78-
79-
union DogOrHuman = Dog | Human
80-
81-
union HumanOrAlien = Human | Alien
82-
8364
enum FurColor {
8465
BROWN
8566
BLACK
@@ -120,13 +101,10 @@ export const testSchema: GraphQLSchema = buildSchema(`
120101
121102
type QueryRoot {
122103
human(id: ID): Human
123-
alien: Alien
124104
dog: Dog
125105
cat: Cat
126106
pet: Pet
127107
catOrDog: CatOrDog
128-
dogOrHuman: DogOrHuman
129-
humanOrAlien: HumanOrAlien
130108
complicatedArgs: ComplicatedArgs
131109
}
132110

src/validation/__tests__/validation-test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ describe('Validate: Supports full validation', () => {
2323
it('validates queries', () => {
2424
const doc = parse(`
2525
query {
26-
catOrDog {
27-
... on Cat {
28-
furColor
29-
}
30-
... on Dog {
31-
isHouseTrained
26+
human {
27+
pets {
28+
... on Cat {
29+
meowsVolume
30+
}
31+
... on Dog {
32+
barkVolume
33+
}
3234
}
3335
}
3436
}
@@ -60,12 +62,14 @@ describe('Validate: Supports full validation', () => {
6062

6163
const doc = parse(`
6264
query {
63-
catOrDog {
64-
... on Cat {
65-
furColor
66-
}
67-
... on Dog {
68-
isHouseTrained
65+
human {
66+
pets {
67+
... on Cat {
68+
meowsVolume
69+
}
70+
... on Dog {
71+
barkVolume
72+
}
6973
}
7074
}
7175
}
@@ -75,9 +79,9 @@ describe('Validate: Supports full validation', () => {
7579
const errorMessages = errors.map((error) => error.message);
7680

7781
expect(errorMessages).to.deep.equal([
78-
'Cannot query field "catOrDog" on type "QueryRoot". Did you mean "catOrDog"?',
79-
'Cannot query field "furColor" on type "Cat". Did you mean "furColor"?',
80-
'Cannot query field "isHouseTrained" on type "Dog". Did you mean "isHouseTrained"?',
82+
'Cannot query field "human" on type "QueryRoot". Did you mean "human"?',
83+
'Cannot query field "meowsVolume" on type "Cat". Did you mean "meowsVolume"?',
84+
'Cannot query field "barkVolume" on type "Dog". Did you mean "barkVolume"?',
8185
]);
8286
});
8387

0 commit comments

Comments
 (0)