Skip to content

Commit

Permalink
addResolversToSchema should merge scalar/enum resolvers
Browse files Browse the repository at this point in the history
instead of overwriting

closes #1587
  • Loading branch information
yaacovCR committed Jun 10, 2020
1 parent 34c1f96 commit 2595719
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/schema/src/addResolversToSchema.ts
Expand Up @@ -28,6 +28,7 @@ import {
healSchema,
parseInputValue,
forEachField,
mergeDeep,
} from '@graphql-tools/utils';

import { checkForResolveTypeResolver } from './checkForResolveTypeResolver';
Expand Down Expand Up @@ -163,6 +164,20 @@ function addResolversToExistingSchema(
Object.keys(resolverValue).forEach(fieldName => {
if (fieldName.startsWith('__')) {
type[fieldName.substring(2)] = resolverValue[fieldName];
} else if (fieldName === 'astNode' && type.astNode != null) {
type.astNode = {
...type.astNode,
description: (resolverValue as GraphQLScalarType)?.astNode.description ?? type.astNode.description,
directives: (type.astNode.directives ?? []).concat(
(resolverValue as GraphQLScalarType)?.astNode.directives ?? []
),
};
} else if (fieldName === 'extensionASTNodes' && type.extensionASTNodes != null) {
type.extensionASTNodes = type.extensionASTNodes.concat(
(resolverValue as GraphQLScalarType)?.extensionASTNodes ?? []
);
} else if (fieldName === 'extensions' && type.extensions != null) {
type.extensions = mergeDeep({}, type.extensions, (resolverValue as GraphQLScalarType).extensions);
} else {
type[fieldName] = resolverValue[fieldName];
}
Expand All @@ -174,6 +189,20 @@ function addResolversToExistingSchema(
Object.keys(resolverValue).forEach(fieldName => {
if (fieldName.startsWith('__')) {
config[fieldName.substring(2)] = resolverValue[fieldName];
} else if (fieldName === 'astNode' && config.astNode != null) {
config.astNode = {
...config.astNode,
description: (resolverValue as GraphQLScalarType)?.astNode.description ?? config.astNode.description,
directives: (config.astNode.directives ?? []).concat(
(resolverValue as GraphQLEnumType)?.astNode.directives ?? []
),
};
} else if (fieldName === 'extensionASTNodes' && config.extensionASTNodes != null) {
config.extensionASTNodes = config.extensionASTNodes.concat(
(resolverValue as GraphQLEnumType)?.extensionASTNodes ?? []
);
} else if (fieldName === 'extensions' && type.extensions != null) {
type.extensions = mergeDeep({}, type.extensions, (resolverValue as GraphQLEnumType).extensions);
} else if (enumValueConfigMap[fieldName]) {
enumValueConfigMap[fieldName].value = resolverValue[fieldName];
}
Expand Down Expand Up @@ -242,6 +271,20 @@ function createNewSchemaWithResolvers(
Object.keys(resolverValue).forEach(fieldName => {
if (fieldName.startsWith('__')) {
config[fieldName.substring(2)] = resolverValue[fieldName];
} else if (fieldName === 'astNode' && config.astNode != null) {
config.astNode = {
...config.astNode,
description: (resolverValue as GraphQLScalarType)?.astNode.description ?? config.astNode.description,
directives: (config.astNode.directives ?? []).concat(
(resolverValue as GraphQLScalarType)?.astNode.directives ?? []
),
};
} else if (fieldName === 'extensionASTNodes' && config.extensionASTNodes != null) {
config.extensionASTNodes = type.extensionASTNodes.concat(
(resolverValue as GraphQLScalarType)?.extensionASTNodes ?? []
);
} else if (fieldName === 'extensions' && config.extensions != null) {
config.extensions = mergeDeep({}, type.extensions, (resolverValue as GraphQLScalarType).extensions);
} else {
config[fieldName] = resolverValue[fieldName];
}
Expand All @@ -260,6 +303,20 @@ function createNewSchemaWithResolvers(
Object.keys(resolverValue).forEach(fieldName => {
if (fieldName.startsWith('__')) {
config[fieldName.substring(2)] = resolverValue[fieldName];
} else if (fieldName === 'astNode' && config.astNode != null) {
config.astNode = {
...config.astNode,
description: (resolverValue as GraphQLScalarType)?.astNode.description ?? config.astNode.description,
directives: (config.astNode.directives ?? []).concat(
(resolverValue as GraphQLEnumType)?.astNode.directives ?? []
),
};
} else if (fieldName === 'extensionASTNodes' && config.extensionASTNodes != null) {
config.extensionASTNodes = config.extensionASTNodes.concat(
(resolverValue as GraphQLEnumType)?.extensionASTNodes ?? []
);
} else if (fieldName === 'extensions' && config.extensions != null) {
config.extensions = mergeDeep({}, type.extensions, (resolverValue as GraphQLEnumType).extensions);
} else if (enumValueConfigMap[fieldName]) {
enumValueConfigMap[fieldName].value = resolverValue[fieldName];
}
Expand Down
29 changes: 29 additions & 0 deletions packages/schema/tests/schemaGenerator.test.ts
Expand Up @@ -838,6 +838,35 @@ describe('generating schema from shorthand', () => {
});
});

test('retains original scalar directives when passing in scalars in resolve functions', () => {
const schema = makeExecutableSchema({
typeDefs: `
directive @test on SCALAR
scalar Test @test
type Query {
test: Test
}
`,
resolvers: {
Test: new GraphQLScalarType({
name: 'Test',
description: 'Test resolver',
serialize: (value) => value,
parseValue: (value) => value,
}),
Query: {
test: () => 42,
},
},
});

const testType = schema.getType('Test');
expect(testType).toBeInstanceOf(GraphQLScalarType);
expect(testType.astNode.directives.length).toBe(1);
});

test('retains scalars after walking/recreating the schema', () => {
const shorthand = `
scalar Test
Expand Down

0 comments on commit 2595719

Please sign in to comment.