Skip to content

Commit

Permalink
fix(typeMerging): fix interface merging (#1917)
Browse files Browse the repository at this point in the history
thanks go to @gmac for finding this bug and contributing test code
see: #1911 (comment)
  • Loading branch information
yaacovCR committed Aug 17, 2020
1 parent 948abc3 commit b8551da
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/stitch/src/mergeCandidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ function mergeObjectTypeCandidates(
{}
);

const interfaces = configs
const interfaceMap = configs
.map(config => config.interfaces)
.reduce((acc, interfaces) => {
return interfaces != null ? acc.concat(interfaces) : acc;
}, []);
if (interfaces != null) {
interfaces.forEach(iface => {
acc[iface.name] = iface;
});
}
return acc;
}, Object.create(null));
const interfaces = Object.keys(interfaceMap).map(interfaceName => interfaceMap[interfaceName]);

const astNodes = pluck<ObjectTypeDefinitionNode>('astNode', candidates);
const astNode = astNodes
Expand Down Expand Up @@ -157,14 +163,17 @@ function mergeInterfaceTypeCandidates(typeName: string, candidates: Array<MergeT
{}
);

const interfaces =
'interfaces' in candidates[0].type.toConfig()
? configs
.map(config => ((config as unknown) as { interfaces: Array<GraphQLInterfaceType> }).interfaces)
.reduce((acc, interfaces) => {
return interfaces != null ? acc.concat(interfaces) : acc;
}, [])
: undefined;
const interfaceMap = configs
.map(config => ((config as unknown) as { interfaces: Array<GraphQLInterfaceType> }).interfaces)
.reduce((acc, interfaces) => {
if (interfaces != null) {
interfaces.forEach(iface => {
acc[iface.name] = iface;
});
}
return acc;
}, Object.create(null));
const interfaces = Object.keys(interfaceMap).map(interfaceName => interfaceMap[interfaceName]);

const astNodes = pluck<InterfaceTypeDefinitionNode>('astNode', candidates);
const astNode = astNodes
Expand Down Expand Up @@ -196,7 +205,13 @@ function mergeUnionTypeCandidates(typeName: string, candidates: Array<MergeTypeC
const description = descriptions[descriptions.length - 1];

const configs = candidates.map(candidate => (candidate.type as GraphQLUnionType).toConfig());
const types = configs.reduce((acc, config) => acc.concat(config.types), []);
const typeMap = configs.reduce((acc, config) => {
config.types.forEach(type => {
typeMap[type.name] = type;
});
return acc;
}, Object.create(null));
const types = Object.keys(typeMap).map(typeName => typeMap[typeName]);

const astNodes = pluck<UnionTypeDefinitionNode>('astNode', candidates);
const astNode = astNodes
Expand Down
82 changes: 82 additions & 0 deletions packages/stitch/tests/mergeInterfaces.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { graphql } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { stitchSchemas } from '../src/stitchSchemas';

describe('merging interfaces', () => {
test('merges interfaces across subschemas', async () => {
const namedItemSchema = makeExecutableSchema({
typeDefs: `
interface Placement {
id: ID!
name: String!
}
type Item implements Placement {
id: ID!
name: String!
}
type Query {
itemById(id: ID!): Item
}
`,
resolvers: {
Query: {
itemById(_obj, args) {
return { id: args.id, name: `Item ${args.id}` };
}
}
}
});

const indexedItemSchema = makeExecutableSchema({
typeDefs: `
interface Placement {
id: ID!
index: Int!
}
type Item implements Placement {
id: ID!
index: Int!
}
type Query {
placementById(id: ID!): Placement
}
`,
resolvers: {
Query: {
placementById(_obj, args) {
return { __typename: 'Item', id: args.id, index: Number(args.id) };
}
}
}
});

const stitchedSchema = stitchSchemas({
subschemas: [
{
schema: namedItemSchema,
merge: {
Item: {
selectionSet: '{ id }',
fieldName: 'itemById',
args: ({ id }) => ({ id }),
}
}
},
{ schema: indexedItemSchema },
],
mergeTypes: true,
});

const result = await graphql(stitchedSchema, `
query {
placement: placementById(id: 23) {
id
index
name
}
}
`);

expect(result.data.placement).toEqual({ id: '23', index: 23, name: 'Item 23' });
});
});

0 comments on commit b8551da

Please sign in to comment.