Skip to content

Commit

Permalink
fix(stitchSchemas): subschemas with object inputs and mergeTypes as t…
Browse files Browse the repository at this point in the history
…rue (#1631)
  • Loading branch information
nicolas-cherel committed Jun 11, 2020
1 parent fa07fd1 commit 6059c40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/stitch/src/typeCandidates.ts
Expand Up @@ -16,9 +16,12 @@ import {
isInterfaceType,
isUnionType,
isEnumType,
isInputObjectType,
SchemaDefinitionNode,
SchemaExtensionNode,
GraphQLFieldConfigMap,
GraphQLInputObjectType,
GraphQLInputFieldConfigMap,
} from 'graphql';

import { wrapSchema } from '@graphql-tools/wrap';
Expand Down Expand Up @@ -277,6 +280,22 @@ function merge(typeName: string, candidates: Array<MergeTypeCandidate>): GraphQL
extensionASTNodes: initialCandidateType.extensionASTNodes,
};
return new GraphQLObjectType(config);
} else if (isInputObjectType(initialCandidateType)) {
const config = {
name: typeName,
fields: candidates.reduce<GraphQLInputFieldConfigMap>(
(acc, candidate) => ({
...acc,
...(candidate.type as GraphQLInputObjectType).toConfig().fields,
}),
{}
),
description: initialCandidateType.description,
extensions: initialCandidateType.extensions,
astNode: initialCandidateType.astNode,
extensionASTNodes: initialCandidateType.extensionASTNodes,
};
return new GraphQLInputObjectType(config);
} else if (isInterfaceType(initialCandidateType)) {
const config = {
name: typeName,
Expand Down
16 changes: 16 additions & 0 deletions packages/stitch/tests/alternateStitchSchemas.test.ts
Expand Up @@ -1853,6 +1853,10 @@ describe('mergeTypes', () => {

beforeEach(() => {
const typeDefs1 = `
input ObjectInput {
val: String!
}
type Query {
rootField1: Wrapper
getTest(id: ID): Test
Expand Down Expand Up @@ -1981,6 +1985,18 @@ describe('mergeTypes', () => {
},
},
});

const stitchedSchemaWithMerge = stitchSchemas({
subschemas: [subschemaConfig1, subschemaConfig2],
mergeTypes: true,
})

const result2 = await graphql(
stitchedSchemaWithMerge,
`{ rootField1 { test { id } } }`
);

expect(result2).toEqual({ data: { rootField1: { test: { id: '1' } } } })
});
});

Expand Down

0 comments on commit 6059c40

Please sign in to comment.