Skip to content

fix(mergeSchemas): handle entire SDL surface #1401

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

Merged
merged 2 commits into from
Apr 23, 2020
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
34 changes: 24 additions & 10 deletions src/stitch/typeFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TokenKind,
GraphQLEnumValueConfigMap,
GraphQLFieldConfigArgumentMap,
valueFromASTUntyped,
} from 'graphql';

import { graphqlVersion } from '../utils/index';
Expand Down Expand Up @@ -67,12 +68,13 @@ export default function typeFromAST(
function makeObjectType(node: ObjectTypeDefinitionNode): GraphQLObjectType {
const config = {
name: node.name.value,
fields: () => makeFields(node.fields),
description: getDescription(node, backcompatOptions),
interfaces: () =>
node.interfaces.map((iface) =>
createNamedStub(iface.name.value, 'interface'),
),
description: getDescription(node, backcompatOptions),
fields: () => makeFields(node.fields),
astNode: node,
};
return new GraphQLObjectType(config);
}
Expand All @@ -82,16 +84,17 @@ function makeInterfaceType(
): GraphQLInterfaceType {
const config = {
name: node.name.value,
fields: () => makeFields(node.fields),
description: getDescription(node, backcompatOptions),
interfaces:
graphqlVersion() >= 15
? () =>
((node as unknown) as ObjectTypeDefinitionNode).interfaces.map(
(iface) => createNamedStub(iface.name.value, 'interface'),
)
: undefined,
description: getDescription(node, backcompatOptions),
fields: () => makeFields(node.fields),
resolveType: (parent: any) => resolveFromParentTypename(parent),
astNode: node,
};
return new GraphQLInterfaceType(config);
}
Expand All @@ -109,18 +112,20 @@ function makeEnumType(node: EnumTypeDefinitionNode): GraphQLEnumType {

return new GraphQLEnumType({
name: node.name.value,
values,
description: getDescription(node, backcompatOptions),
values,
astNode: node,
});
}

function makeUnionType(node: UnionTypeDefinitionNode): GraphQLUnionType {
return new GraphQLUnionType({
name: node.name.value,
description: getDescription(node, backcompatOptions),
types: () =>
node.types.map((type) => createNamedStub(type.name.value, 'object')),
description: getDescription(node, backcompatOptions),
resolveType: (parent) => resolveFromParentTypename(parent),
astNode: node,
});
}

Expand All @@ -135,6 +140,7 @@ function makeScalarType(node: ScalarTypeDefinitionNode): GraphQLScalarType {
// always pass validation.
parseValue: () => false,
parseLiteral: () => false,
astNode: node,
});
}

Expand All @@ -143,8 +149,9 @@ function makeInputObjectType(
): GraphQLInputObjectType {
return new GraphQLInputObjectType({
name: node.name.value,
fields: () => makeValues(node.fields),
description: getDescription(node, backcompatOptions),
fields: () => makeValues(node.fields),
astNode: node,
});
}

Expand All @@ -169,9 +176,10 @@ function makeFields(
...prev,
[node.name.value]: {
type: createStub(node.type, 'output'),
args: makeValues(node.arguments),
description: getDescription(node, backcompatOptions),
args: makeValues(node.arguments),
deprecationReason,
astNode: node,
},
};
}, {});
Expand All @@ -185,8 +193,12 @@ function makeValues(
...prev,
[node.name.value]: {
type: createStub(node.type, 'input'),
defaultValue: node.defaultValue,
defaultValue:
node.defaultValue != null
? valueFromASTUntyped(node.defaultValue)
: undefined,
description: getDescription(node, backcompatOptions),
astNode: node,
},
}),
{},
Expand All @@ -203,8 +215,10 @@ function makeDirective(node: DirectiveDefinitionNode): GraphQLDirective {
return new GraphQLDirective({
name: node.name.value,
description: node.description != null ? node.description.value : null,
args: makeValues(node.arguments),
locations,
isRepeatable: node.repeatable,
args: makeValues(node.arguments),
astNode: node,
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/alternateMergeSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
printSchema,
graphqlSync,
GraphQLField,
assertValidSchema,
} from 'graphql';

import {
Expand Down Expand Up @@ -1918,3 +1919,19 @@ describe('mergeTypes', () => {
});
});
});

describe('mergeSchemas handles typeDefs with default values', () => {
test('it works', () => {
const typeDefs = `
type Query {
foo(arg: String = "1"): String
}
`;

const schema = makeExecutableSchema({ typeDefs });
assertValidSchema(schema);

const mergedSchema = mergeSchemas({ typeDefs });
assertValidSchema(mergedSchema);
});
});