Skip to content

Commit 82ff872

Browse files
jkantrbenjie
authored andcommitted
feat(utils): makeExtendSchemaPlugin accepts typeDef array (#574)
1 parent b2d8c65 commit 82ff872

3 files changed

Lines changed: 127 additions & 7 deletions

File tree

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,97 @@ it("allows adding a non-null list of non-null type", async () => {
223223
expect(data.randomNumbers).toEqual([5, 3, 6]);
224224
});
225225

226+
it("accepts an array of typedefs", async () => {
227+
const schema = await buildSchema([
228+
...simplePlugins,
229+
makeExtendSchemaPlugin(_build => ({
230+
typeDefs: [
231+
gql`
232+
extend type Query {
233+
"""
234+
A random number generated by a fair dice roll.
235+
"""
236+
randomNumber: Int!
237+
}
238+
`,
239+
gql`
240+
extend type Query {
241+
"""
242+
Gives a list of numbers that were randomly generated by fair dice roll
243+
"""
244+
randomNumbers: [Int!]!
245+
}
246+
`,
247+
],
248+
resolvers,
249+
})),
250+
]);
251+
expect(schema).toMatchSnapshot();
252+
const { data } = await graphql(
253+
schema,
254+
`
255+
{
256+
randomNumber
257+
randomNumbers
258+
}
259+
`
260+
);
261+
expect(data.randomNumber).toEqual(4);
262+
expect(data.randomNumbers).toEqual([5, 3, 6]);
263+
});
264+
265+
it("throws the proper error if an array of typeDefs aren't all DocumentNodes", () => {
266+
return expect(
267+
buildSchema([
268+
...simplePlugins,
269+
makeExtendSchemaPlugin(_build => ({
270+
typeDefs: [
271+
gql`
272+
extend type Query {
273+
"""
274+
A random number generated by a fair dice roll.
275+
"""
276+
randomNumber: Int!
277+
}
278+
`,
279+
`
280+
extend type Query {
281+
"""
282+
Gives a list of numbers that were randomly generated by fair dice roll
283+
"""
284+
randomNumbers: [Int!]!
285+
}
286+
`,
287+
],
288+
resolvers,
289+
})),
290+
])
291+
).rejects.toMatchInlineSnapshot(
292+
`[Error: The first argument to makeExtendSchemaPlugin must be generated by the \`gql\` helper, or be an array of the same.]`
293+
);
294+
});
295+
296+
it("throws the proper error if a single typeDef isn't a DocumentNode", () => {
297+
return expect(
298+
buildSchema([
299+
...simplePlugins,
300+
makeExtendSchemaPlugin(_build => ({
301+
typeDefs: `
302+
extend type Query {
303+
"""
304+
Gives a list of numbers that were randomly generated by fair dice roll
305+
"""
306+
randomNumbers: [Int!]!
307+
}
308+
`,
309+
resolvers,
310+
})),
311+
])
312+
).rejects.toMatchInlineSnapshot(
313+
`[Error: The first argument to makeExtendSchemaPlugin must be generated by the \`gql\` helper, or be an array of the same.]`
314+
);
315+
});
316+
226317
it("allows adding a field with arguments", async () => {
227318
const schema = await buildSchema([
228319
...simplePlugins,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`accepts an array of typedefs 1`] = `
4+
"""The root query type which gives access points into the data universe."""
5+
type Query {
6+
"""
7+
Exposes the root query type nested one level down. This is helpful for Relay 1
8+
which can only query top level fields if they are in a particular form.
9+
"""
10+
query: Query!
11+
12+
"""A random number generated by a fair dice roll."""
13+
randomNumber: Int!
14+
15+
"""Gives a list of numbers that were randomly generated by fair dice roll"""
16+
randomNumbers: [Int!]!
17+
}
18+
19+
`;
20+
321
exports[`allows adding a field with arguments 1`] = `
422
"""The root query type which gives access points into the data universe."""
523
type Query {

packages/graphile-utils/src/makeExtendSchemaPlugin.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222

2323
// Nodes:
2424
DirectiveNode,
25+
DefinitionNode,
2526
DocumentNode,
2627
EnumValueDefinitionNode,
2728
FieldDefinitionNode,
@@ -80,7 +81,7 @@ export interface Resolvers<TSource = any, TContext = any> {
8081
}
8182

8283
export interface ExtensionDefinition {
83-
typeDefs: DocumentNode;
84+
typeDefs: DocumentNode | DocumentNode[];
8485
resolvers?: Resolvers;
8586
}
8687

@@ -118,11 +119,21 @@ export default function makeExtendSchemaPlugin(
118119
typeof generator === "function"
119120
? generator(build, schemaOptions)
120121
: generator;
121-
if (!(typeDefs as any) || (typeDefs as any).kind !== "Document") {
122-
throw new Error(
123-
"The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper"
124-
);
125-
}
122+
123+
const typeDefsArr = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
124+
const mergedTypeDefinitions = typeDefsArr.reduce(
125+
(definitions, typeDef) => {
126+
if (!(typeDef as any) || (typeDef as any).kind !== "Document") {
127+
throw new Error(
128+
"The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper, or be an array of the same."
129+
);
130+
}
131+
definitions.push(...typeDef.definitions);
132+
return definitions;
133+
},
134+
[] as DefinitionNode[]
135+
);
136+
126137
const typeExtensions = {
127138
GraphQLSchema: {
128139
directives: [] as Array<any>,
@@ -132,7 +143,7 @@ export default function makeExtendSchemaPlugin(
132143
GraphQLObjectType: {},
133144
};
134145
const newTypes: Array<NewTypeDef> = [];
135-
typeDefs.definitions.forEach(definition => {
146+
mergedTypeDefinitions.forEach(definition => {
136147
if (definition.kind === "EnumTypeDefinition") {
137148
newTypes.push({
138149
type: GraphQLEnumType,

0 commit comments

Comments
 (0)