Skip to content

Commit

Permalink
fix(stitch) fix abstract type merge failure (#1981)
Browse files Browse the repository at this point in the history
fix in #1917 includes regression
  • Loading branch information
gmac committed Sep 1, 2020
1 parent 8926480 commit 3f01f85
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/stitch/src/mergeCandidates.ts
Expand Up @@ -207,7 +207,7 @@ function mergeUnionTypeCandidates(typeName: string, candidates: Array<MergeTypeC
const configs = candidates.map(candidate => (candidate.type as GraphQLUnionType).toConfig());
const typeMap = configs.reduce((acc, config) => {
config.types.forEach(type => {
typeMap[type.name] = type;
acc[type.name] = type;
});
return acc;
}, Object.create(null));
Expand Down
95 changes: 95 additions & 0 deletions packages/stitch/tests/mergeAbstractTypes.test.ts
@@ -0,0 +1,95 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { stitchSchemas } from '@graphql-tools/stitch';
import { graphql } from 'graphql';

describe('Abstract type merge', () => {
it('merges with abstract type definitions', async () => {
const imageSchema = makeExecutableSchema({
typeDefs: `
type Image {
id: ID!
url: String!
}
type Query {
image(id: ID!): Image
}
`,
resolvers: {
Query: {
image: (root, { id }) => ({ id, url: `/path/to/${id}` }),
}
}
});

const contentSchema = makeExecutableSchema({
typeDefs: `
type Post {
id: ID!
leadArt: LeadArt
}
type Image {
id: ID!
}
type Video {
id: ID!
url: String!
}
union LeadArt = Image | Video
type Query {
post(id: ID!): Post
}
`,
resolvers: {
Query: {
post: (root, { id }) => ({ id, leadArt: { __typename: 'Image', id: '23' } }),
}
}
});

const gatewaySchema = stitchSchemas({
subschemas: [
{
schema: imageSchema,
merge: {
Image: {
selectionSet: '{ id }',
fieldName: 'image',
args: ({ id }) => ({ id }),
},
},
},
{
schema: contentSchema,
merge: {
Post: {
selectionSet: '{ id }',
fieldName: 'post',
args: ({ id }) => ({ id }),
},
},
},
],
mergeTypes: true
});

const { data } = await graphql(gatewaySchema, `
query {
post(id: 55) {
leadArt {
__typename
...on Image {
id
url
}
}
}
}
`);

expect(data.post.leadArt).toEqual({
__typename: 'Image',
url: '/path/to/23',
id: '23',
});
});
});

0 comments on commit 3f01f85

Please sign in to comment.