Skip to content

Commit

Permalink
feat(utils): makeExtendSchemaPlugin accepts typeDef array (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkantr authored and benjie committed Dec 11, 2019
1 parent b2d8c65 commit 82ff872
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
91 changes: 91 additions & 0 deletions packages/graphile-utils/__tests__/ExtendSchemaPlugin.test.js
Expand Up @@ -223,6 +223,97 @@ it("allows adding a non-null list of non-null type", async () => {
expect(data.randomNumbers).toEqual([5, 3, 6]);
});

it("accepts an array of typedefs", async () => {
const schema = await buildSchema([
...simplePlugins,
makeExtendSchemaPlugin(_build => ({
typeDefs: [
gql`
extend type Query {
"""
A random number generated by a fair dice roll.
"""
randomNumber: Int!
}
`,
gql`
extend type Query {
"""
Gives a list of numbers that were randomly generated by fair dice roll
"""
randomNumbers: [Int!]!
}
`,
],
resolvers,
})),
]);
expect(schema).toMatchSnapshot();
const { data } = await graphql(
schema,
`
{
randomNumber
randomNumbers
}
`
);
expect(data.randomNumber).toEqual(4);
expect(data.randomNumbers).toEqual([5, 3, 6]);
});

it("throws the proper error if an array of typeDefs aren't all DocumentNodes", () => {
return expect(
buildSchema([
...simplePlugins,
makeExtendSchemaPlugin(_build => ({
typeDefs: [
gql`
extend type Query {
"""
A random number generated by a fair dice roll.
"""
randomNumber: Int!
}
`,
`
extend type Query {
"""
Gives a list of numbers that were randomly generated by fair dice roll
"""
randomNumbers: [Int!]!
}
`,
],
resolvers,
})),
])
).rejects.toMatchInlineSnapshot(
`[Error: The first argument to makeExtendSchemaPlugin must be generated by the \`gql\` helper, or be an array of the same.]`
);
});

it("throws the proper error if a single typeDef isn't a DocumentNode", () => {
return expect(
buildSchema([
...simplePlugins,
makeExtendSchemaPlugin(_build => ({
typeDefs: `
extend type Query {
"""
Gives a list of numbers that were randomly generated by fair dice roll
"""
randomNumbers: [Int!]!
}
`,
resolvers,
})),
])
).rejects.toMatchInlineSnapshot(
`[Error: The first argument to makeExtendSchemaPlugin must be generated by the \`gql\` helper, or be an array of the same.]`
);
});

it("allows adding a field with arguments", async () => {
const schema = await buildSchema([
...simplePlugins,
Expand Down
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`accepts an array of typedefs 1`] = `
"""The root query type which gives access points into the data universe."""
type Query {
"""
Exposes the root query type nested one level down. This is helpful for Relay 1
which can only query top level fields if they are in a particular form.
"""
query: Query!
"""A random number generated by a fair dice roll."""
randomNumber: Int!
"""Gives a list of numbers that were randomly generated by fair dice roll"""
randomNumbers: [Int!]!
}
`;

exports[`allows adding a field with arguments 1`] = `
"""The root query type which gives access points into the data universe."""
type Query {
Expand Down
25 changes: 18 additions & 7 deletions packages/graphile-utils/src/makeExtendSchemaPlugin.ts
Expand Up @@ -22,6 +22,7 @@ import {

// Nodes:
DirectiveNode,
DefinitionNode,
DocumentNode,
EnumValueDefinitionNode,
FieldDefinitionNode,
Expand Down Expand Up @@ -80,7 +81,7 @@ export interface Resolvers<TSource = any, TContext = any> {
}

export interface ExtensionDefinition {
typeDefs: DocumentNode;
typeDefs: DocumentNode | DocumentNode[];
resolvers?: Resolvers;
}

Expand Down Expand Up @@ -118,11 +119,21 @@ export default function makeExtendSchemaPlugin(
typeof generator === "function"
? generator(build, schemaOptions)
: generator;
if (!(typeDefs as any) || (typeDefs as any).kind !== "Document") {
throw new Error(
"The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper"
);
}

const typeDefsArr = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
const mergedTypeDefinitions = typeDefsArr.reduce(
(definitions, typeDef) => {
if (!(typeDef as any) || (typeDef as any).kind !== "Document") {
throw new Error(
"The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper, or be an array of the same."
);
}
definitions.push(...typeDef.definitions);
return definitions;
},
[] as DefinitionNode[]
);

const typeExtensions = {
GraphQLSchema: {
directives: [] as Array<any>,
Expand All @@ -132,7 +143,7 @@ export default function makeExtendSchemaPlugin(
GraphQLObjectType: {},
};
const newTypes: Array<NewTypeDef> = [];
typeDefs.definitions.forEach(definition => {
mergedTypeDefinitions.forEach(definition => {
if (definition.kind === "EnumTypeDefinition") {
newTypes.push({
type: GraphQLEnumType,
Expand Down

0 comments on commit 82ff872

Please sign in to comment.