Skip to content

Commit

Permalink
fixes for ast merging issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Nov 17, 2018
1 parent c8b2aa4 commit 9de68cc
Show file tree
Hide file tree
Showing 4 changed files with 2,150 additions and 5 deletions.
18 changes: 15 additions & 3 deletions packages/epoxy/src/schema-mergers/merge-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefinitionNode, DocumentNode, GraphQLSchema, parse, print, Source } from 'graphql';
import { buildASTSchema, printSchema, DefinitionNode, DocumentNode, GraphQLSchema, parse, print, Source } from 'graphql';
import { isGraphQLSchema, isSourceTypes, isStringTypes } from './utils';
import { MergedResultMap, mergeGraphQLNodes } from './merge-nodes';

Expand All @@ -14,13 +14,25 @@ export function mergeGraphQLSchemas(types: Array<string | Source | DocumentNode
.join('\n');
}

function fixSchemaAst(schema: GraphQLSchema): GraphQLSchema {
return buildASTSchema(parse(printSchema(schema)));
}

export function mergeGraphQLTypes(types: Array<string | Source | DocumentNode | GraphQLSchema>): DefinitionNode[] {
const allNodes: ReadonlyArray<DefinitionNode> = types
.map<DocumentNode>(type => {
if (isGraphQLSchema(type)) {
const typesMap = type.getTypeMap();
let schema: GraphQLSchema = type;
let typesMap = type.getTypeMap();
const validAstNodes = Object.keys(typesMap).filter(key => typesMap[key].astNode);

if (validAstNodes.length === 0 && Object.keys(typesMap).length > 0) {
schema = fixSchemaAst(schema);
typesMap = schema.getTypeMap();
}

const allTypesPrinted = Object.keys(typesMap).map(key => typesMap[key]).map(type => type.astNode ? print(type.astNode) : null).filter(e => e);
const directivesDeclaration = type.getDirectives().map(directive => directive.astNode ? print(directive.astNode) : null).filter(e => e);
const directivesDeclaration = schema.getDirectives().map(directive => directive.astNode ? print(directive.astNode) : null).filter(e => e);
const printedSchema = [...directivesDeclaration, ...allTypesPrinted].join('\n');

return parse(printedSchema);
Expand Down
37 changes: 36 additions & 1 deletion packages/epoxy/tests/merge-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
import { mergeGraphQLSchemas, mergeGraphQLTypes } from '../src/schema-mergers/merge-schema';
import { makeExecutableSchema } from 'graphql-tools';
import { printSchema } from 'graphql';
import { buildSchema, buildClientSchema } from 'graphql';
import { stripWhitespaces } from './utils';
import gql from 'graphql-tag';
import * as introspectionSchema from './schema.json';

describe('Merge Schema', () => {
describe('AST Schema Fixing', () => {
it('Should handle correctly schema without valid root AST node', () => {
const schema = buildSchema(`
type A {
a: String
}
type Query {
a: A
}
`);

expect(schema.astNode).toBeUndefined();

expect(() => {
mergeGraphQLTypes([
schema,
]);
}).not.toThrow();
});

it('Should handle correctly schema without valid types AST nodes', () => {
const schema = buildClientSchema(introspectionSchema);

expect(schema.astNode).toBeUndefined();

expect(() => {
mergeGraphQLTypes([
schema,
]);
}).not.toThrow();
});
});

describe('mergeGraphQLTypes', () => {
it('should return the correct definition of Schema', () => {
const mergedArray = mergeGraphQLTypes([
Expand Down

0 comments on commit 9de68cc

Please sign in to comment.