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

fix(typeMerging): fix interface merging #1917

Merged
merged 1 commit into from
Aug 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' });
});
});