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

should handle extend types when GraphQLSchema is the source #238

Merged
merged 7 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
58 changes: 45 additions & 13 deletions packages/epoxy/src/schema-mergers/merge-schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { buildASTSchema, printSchema, DefinitionNode, DocumentNode, GraphQLSchema, parse, print, Source, GraphQLObjectType, isSpecifiedScalarType, isIntrospectionType, GraphQLScalarType, printType } from 'graphql';
import {
buildASTSchema,
printSchema,
DefinitionNode,
DocumentNode,
GraphQLSchema,
parse,
print,
Source,
GraphQLObjectType,
isSpecifiedScalarType,
isIntrospectionType,
GraphQLScalarType,
printType,
ObjectTypeExtensionNode,
GraphQLNamedType,
isObjectType,
} from 'graphql';
import { isGraphQLSchema, isSourceTypes, isStringTypes, isSchemaDefinition } from './utils';
import { MergedResultMap, mergeGraphQLNodes } from './merge-nodes';

interface Config {
useSchemaDefinition?: boolean;
}

export function mergeGraphQLSchemas(
types: Array<string | Source | DocumentNode | GraphQLSchema>,
config?: Partial<Config>,
): DocumentNode {
export function mergeGraphQLSchemas(types: Array<string | Source | DocumentNode | GraphQLSchema>, config?: Partial<Config>): DocumentNode {
return {
kind: 'Document',
definitions: mergeGraphQLTypes(types, {
Expand All @@ -29,9 +43,9 @@ function createSchemaDefinition(def: {
subscription: string | GraphQLObjectType | null;
}): string {
const schemaRoot: {
query?: string,
mutation?: string,
subscription?: string,
query?: string;
mutation?: string;
subscription?: string;
} = {};

if (def.query) {
Expand All @@ -55,10 +69,7 @@ function createSchemaDefinition(def: {
return undefined;
}

export function mergeGraphQLTypes(
types: Array<string | Source | DocumentNode | GraphQLSchema>,
config: Config,
): DefinitionNode[] {
export function mergeGraphQLTypes(types: Array<string | Source | DocumentNode | GraphQLSchema>, config: Config): DefinitionNode[] {
const allNodes: ReadonlyArray<DefinitionNode> = types
.map<DocumentNode>(type => {
if (isGraphQLSchema(type)) {
Expand All @@ -84,7 +95,13 @@ export function mergeGraphQLTypes(

return !isPredefinedScalar && !isIntrospection;
})
.map(type => (type.astNode ? print(type.astNode) : printType(type)))
.map(type => {
if (type.astNode) {
return print(type.extensionASTNodes ? extendDefinition(type) : type.astNode);
} else {
return printType(type);
}
})
.filter(e => e);
const directivesDeclaration = schema
.getDirectives()
Expand Down Expand Up @@ -145,3 +162,18 @@ export function mergeGraphQLTypes(

return [...Object.values(mergedNodes), parse(schemaDefinition).definitions[0]];
}

function extendDefinition(type: GraphQLNamedType): GraphQLNamedType['astNode'] {
switch (type.astNode.kind) {
case 'ObjectTypeDefinition':
return {
...type.astNode,
// add fields from object extension (`extend type Query { newField: String }`)
fields: type.astNode.fields.concat(
(type.extensionASTNodes as ReadonlyArray<ObjectTypeExtensionNode>).reduce((fields, node) => fields.concat(node.fields), []),
),
};
default:
return type.astNode;
}
}
73 changes: 73 additions & 0 deletions packages/epoxy/tests/merge-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,79 @@ describe('Merge Schema', () => {
}
`));
});
it('should handle extend types when GraphQLSchema is the source', () => {
const schema = makeExecutableSchema({
typeDefs: [`
type Query {
foo: String
}

type User {
name: String
}
`, `
extend type Query {
bar: String
}

extend type User {
id: ID
}
`],
});
const merged = mergeGraphQLSchemas([schema]);
const printed = stripWhitespaces(print(merged));

expect(printed).toContain(stripWhitespaces(`
type Query {
foo: String
bar: String
}
`));
expect(printed).toContain(stripWhitespaces(`
type User {
name: String
id: ID
}
`));
});

it('should fail when a field is already defined and has a different type', () => {
expect(() => {
mergeGraphQLSchemas([`
type Query {
foo: String
}
`, `
extend type Query {
foo: Int
bar: String
}
`]);
}).toThrowError('Unable to merge GraphQL type');
});

it('should preserve an extend keyword if there is no base', () => {
const merged = mergeGraphQLSchemas([`
extend type Query {
foo: String
}
`, `
extend type Query {
bar: String
}
`]);

const printed = stripWhitespaces(print(merged));

expect(printed).toContain(stripWhitespaces(`
extend type Query {
foo: String
bar: String
}
`));
});

it('should handle extend inputs', () => {
const merged = mergeGraphQLSchemas([`
input TestInput {
Expand Down