Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): makeExtendSchemaPlugin accepts typeDef array #574

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/graphile-utils/__tests__/ExtendSchemaPlugin.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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[]
);
jkantr marked this conversation as resolved.
Show resolved Hide resolved

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