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(mergeSchemas): Prevent custom resolvers from being overwritten by default resolvers #4455

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/hip-tools-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/schema': major
---

Fix `mergeSchema` so the provided `resolver` overrides the resolvers in the `schema` with the same name
2 changes: 1 addition & 1 deletion packages/schema/src/merge-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function mergeSchemas(config: MergeSchemasConfig) {
const schemas = config.schemas || [];
for (const schema of schemas) {
extractedTypeDefs.push(schema);
extractedResolvers.push(getResolversFromSchema(schema));
extractedResolvers.unshift(getResolversFromSchema(schema));
extractedSchemaExtensions.push(extractExtensionsFromSchema(schema));
}

Expand Down
54 changes: 54 additions & 0 deletions packages/schema/tests/merge-schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,60 @@ describe('Merge Schemas', () => {
expect(data['bar']).toBe('BAR');
expect(data['qux']).toBe('QUX');
});
it('should override resolver in schema with resolver passed into config', async () => {
const fooSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => 'FOO',
},
},
});
const barSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
bar: String
}
`,
resolvers: {
Query: {
bar: () => 'BAR',
},
},
});
const { errors, data } = await graphql({
schema: mergeSchemas({
schemas: [fooSchema, barSchema],
typeDefs: /* GraphQL */ `
type Query {
qux: String
}
`,
resolvers: {
Query: {
qux: () => 'QUX',
foo: () => 'FOO_BAR_QUX',
},
},
}),
source: `
{
foo
bar
qux
}
`,
});
expect(errors).toBeFalsy();
assertSome(data);
expect(data['foo']).toBe('FOO_BAR_QUX');
expect(data['bar']).toBe('BAR');
expect(data['qux']).toBe('QUX');
});
it('should merge two valid executable schemas with extra typeDefs and resolvers', async () => {
const fooSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
Expand Down