Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
feat(utils): getResolversFromSchema
Browse files Browse the repository at this point in the history
add function to get graphql-tools style resolvers map from an existing schema, parallels graphql-js toConfig.
  • Loading branch information
yaacovCR committed Sep 26, 2019
1 parent f0bbd7f commit 630e9a8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ExecutionResult,
defaultFieldResolver,
findDeprecatedUsages,
printSchema,
} from 'graphql';
import mergeSchemas from '../stitching/mergeSchemas';
import {
Expand All @@ -34,6 +35,7 @@ import {
} from '../Interfaces';
import { delegateToSchema } from '../stitching';
import { cloneSchema } from '../utils';
import { getResolversFromSchema } from '../utils/getResolversFromSchema';

const removeLocations = ({ locations, ...rest }: any): any => ({ ...rest });

Expand All @@ -59,7 +61,10 @@ const testCombinations = [
{
name: 'recreated',
booking: cloneSchema(localBookingSchema),
property: cloneSchema(localPropertySchema),
property: makeExecutableSchema({
typeDefs: printSchema(localPropertySchema),
resolvers: getResolversFromSchema(localPropertySchema),
}),
product: cloneSchema(localProductSchema),
}
];
Expand Down
64 changes: 64 additions & 0 deletions src/utils/getResolversFromSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
GraphQLSchema,
GraphQLScalarType,
GraphQLEnumType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
} from 'graphql';
import { IResolvers } from '../Interfaces';
import isSpecifiedScalarType from './isSpecifiedScalarType';
import { cloneType } from './clone';

export function getResolversFromSchema(schema: GraphQLSchema): IResolvers {
const resolvers = Object.create({});

const typeMap = schema.getTypeMap();

Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];

if (type instanceof GraphQLScalarType) {
if (!isSpecifiedScalarType(type)) {
resolvers[typeName] = cloneType(type);
}
} else if (type instanceof GraphQLEnumType) {
resolvers[typeName] = {};

const values = type.getValues();
values.forEach(value => {
resolvers[typeName][value.name] = value.value;
});
} else if (type instanceof GraphQLInterfaceType) {
if (type.resolveType) {
resolvers[typeName] = {
__resolveType: type.resolveType,
};
}
} else if (type instanceof GraphQLUnionType) {
if (type.resolveType) {
resolvers[typeName] = {
__resolveType: type.resolveType,
};
}
} else if (type instanceof GraphQLObjectType) {
resolvers[typeName] = {};

if (type.isTypeOf) {
resolvers[typeName].__isTypeOf = type.isTypeOf;
}

const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];

resolvers[typeName][fieldName] = {
resolve: field.resolve,
subscribe: field.subscribe,
};
});
}
});

return resolvers;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { healSchema, healTypes } from './heal';
export { SchemaVisitor } from './SchemaVisitor';
export { SchemaDirectiveVisitor } from './SchemaDirectiveVisitor';
export { visitSchema } from './visitSchema';
export { getResolversFromSchema } from './getResolversFromSchema';

0 comments on commit 630e9a8

Please sign in to comment.