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(stitch) fix abstract type merge failure #1981

Merged
merged 2 commits into from Sep 1, 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
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',
});
});
});