Skip to content

Commit 6e87476

Browse files
mgagliardo91benjie
andauthored
Add polymorphic types from extensions to the schema (#830)
Co-authored-by: Benjie Gillam <benjie@jemjie.com>
1 parent dcac7c8 commit 6e87476

4 files changed

Lines changed: 55 additions & 12 deletions

File tree

packages/graphile-utils/__tests__/ExtendSchemaPlugin.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ it("supports interfaces and unions", async () => {
817817
c: Int!
818818
}
819819
820+
# A child type that has no explicit references
821+
type D implements Named {
822+
name: String!
823+
d: Int!
824+
}
825+
820826
union ABC = A | B | C
821827
extend type Query {
822828
abc: [ABC!]
@@ -832,6 +838,9 @@ it("supports interfaces and unions", async () => {
832838
extend type B {
833839
nameLanguage: String
834840
}
841+
extend type D {
842+
nameLanguage: String
843+
}
835844
`,
836845
resolvers: {
837846
Query: {
@@ -843,6 +852,7 @@ it("supports interfaces and unions", async () => {
843852
named: () => [
844853
{ name: "A-one", a: 1, nameLanguage: "en" },
845854
{ name: "B-two", b: 2, nameLanguage: "en" },
855+
{ name: "D-three", d: 3, nameLanguage: "en" },
846856
],
847857
},
848858
ABC: {
@@ -857,6 +867,7 @@ it("supports interfaces and unions", async () => {
857867
__resolveType(obj) {
858868
if (obj.a != null) return "A";
859869
if (obj.b != null) return "B";
870+
if (obj.d != null) return "D";
860871
return null;
861872
},
862873
},
@@ -894,6 +905,9 @@ it("supports interfaces and unions", async () => {
894905
... on B {
895906
b
896907
}
908+
... on D {
909+
d
910+
}
897911
}
898912
}
899913
`

packages/graphile-utils/__tests__/__snapshots__/ExtendSchemaPlugin-pg.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,16 +2531,16 @@ type Mutation {
25312531
): RegisterUserPayload
25322532
}
25332533
2534-
type RegisterUserPayload {
2535-
user: User
2536-
}
2537-
25382534
input RegisterUserInput {
25392535
name: String!
25402536
email: String!
25412537
bio: String
25422538
}
25432539
2540+
type RegisterUserPayload {
2541+
user: User
2542+
}
2543+
25442544
`;
25452545
25462546
exports[`allows adding a simple mutation field to PG schema 2`] = `

packages/graphile-utils/__tests__/__snapshots__/ExtendSchemaPlugin.test.js.snap

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ type Mutation {
230230
echo(input: EchoInput): EchoOutput
231231
}
232232
233-
type EchoOutput {
233+
input EchoInput {
234234
text: String!
235235
int: Int
236236
float: Float!
237237
intList: [Int!]
238238
}
239239
240-
input EchoInput {
240+
type EchoOutput {
241241
text: String!
242242
int: Int
243243
float: Float!
@@ -406,16 +406,14 @@ type Query {
406406
named: [Named!]
407407
}
408408
409-
union ABC = A | B | C
410-
411-
type A implements Named {
409+
interface Named {
412410
name: String!
413-
a: Int!
414411
nameLanguage: String
415412
}
416413
417-
interface Named {
414+
type A implements Named {
418415
name: String!
416+
a: Int!
419417
nameLanguage: String
420418
}
421419
@@ -429,6 +427,14 @@ type C {
429427
c: Int!
430428
}
431429
430+
type D implements Named {
431+
name: String!
432+
d: Int!
433+
nameLanguage: String
434+
}
435+
436+
union ABC = A | B | C
437+
432438
`;
433439

434440
exports[`supports interfaces and unions 2`] = `
@@ -464,6 +470,12 @@ Object {
464470
"name": "B-two",
465471
"nameLanguage": "en",
466472
},
473+
Object {
474+
"__typename": "D",
475+
"d": 3,
476+
"name": "D-three",
477+
"nameLanguage": "en",
478+
},
467479
],
468480
}
469481
`;

packages/graphile-utils/src/makeExtendSchemaPlugin.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import type {
4444
GraphQLDirective,
4545
InputObjectTypeExtensionNode,
4646
InterfaceTypeExtensionNode,
47+
TypeDefinitionNode,
4748
} from "graphql";
4849
import { GraphileEmbed } from "./gql";
4950

@@ -478,16 +479,32 @@ export default function makeExtendSchemaPlugin(
478479
});
479480

480481
builder.hook("GraphQLSchema", (schema, build, _context) => {
482+
const { inflection } = build;
481483
const {
482484
[`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions,
485+
[`ExtendSchemaPlugin_${uniqueId}_newTypes`]: newTypeDefinitions,
483486
} = build;
487+
const newTypes = newTypeDefinitions.map(
488+
({ definition }: { definition: TypeDefinitionNode }) =>
489+
build.getTypeByName(definition.name.value)
490+
);
491+
484492
return {
485493
...schema,
486494
directives: [
487495
...(schema.directives || build.graphql.specifiedDirectives || []),
488496
...typeExtensions.GraphQLSchema.directives,
489497
],
490-
types: [...(schema.types || []), ...typeExtensions.GraphQLSchema.types],
498+
types: [
499+
...(schema.types || []),
500+
...[
501+
build.getTypeByName(inflection.builtin("Query")),
502+
build.getTypeByName(inflection.builtin("Mutation")),
503+
build.getTypeByName(inflection.builtin("Subscription")),
504+
].filter(_ => _),
505+
...typeExtensions.GraphQLSchema.types,
506+
...newTypes,
507+
],
491508
};
492509
});
493510

0 commit comments

Comments
 (0)