Skip to content

Commit 2f4d7d3

Browse files
committed
allow querying fields on unions if the unions are contrained to implement the interface
changed TypeInfo behavior
1 parent 58ad0da commit 2f4d7d3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/utilities/TypeInfo.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
isListType,
2828
isObjectType,
2929
isOutputType,
30+
isUnionType,
3031
} from '../type/definition';
3132
import type { GraphQLDirective } from '../type/directives';
3233
import {
@@ -323,6 +324,14 @@ function getFieldDef(
323324
if (isObjectType(parentType) || isInterfaceType(parentType)) {
324325
return parentType.getFields()[name];
325326
}
327+
if (isUnionType(parentType)) {
328+
for (const iface of parentType.getInterfaces()) {
329+
const field = iface.getFields()[name];
330+
if (field) {
331+
return field;
332+
}
333+
}
334+
}
326335
}
327336

328337
/**

src/validation/__tests__/FieldsOnCorrectTypeRule-test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ const testSchema = buildSchema(`
4141
meowVolume: Int
4242
}
4343
44-
union CatOrDog implements Pet = Cat | Dog
44+
union CatOrDog = Cat | Dog
45+
46+
union ConstrainedCatOrDog implements Pet = Cat | Dog
4547
4648
type Human {
4749
name: String
@@ -83,7 +85,7 @@ describe('Validate: Fields on correct type', () => {
8385

8486
it('Union implementing interface field selection', () => {
8587
expectValid(`
86-
fragment unionImplementingInterfaceFieldSelection on CatOrDog {
88+
fragment unionImplementingInterfaceFieldSelection on ConstrainedCatOrDog {
8789
__typename
8890
name
8991
}
@@ -408,7 +410,7 @@ describe('Validate: Fields on correct type', () => {
408410
interface Feline implements Animal & Mammal { name: String }
409411
type Cat implements Animal & Mammal & Feline { name: String }
410412
411-
union CatOrDog implements Animal & Mammal = Cat | Dog
413+
union CatOrDog = Cat | Dog
412414
type Query { catOrDog: CatOrDog }
413415
`);
414416

0 commit comments

Comments
 (0)