diff --git a/src/test/testMergeSchemas.ts b/src/test/testMergeSchemas.ts index 72b32ae9864..648a8dcccf9 100644 --- a/src/test/testMergeSchemas.ts +++ b/src/test/testMergeSchemas.ts @@ -12,6 +12,7 @@ import { ExecutionResult, defaultFieldResolver, findDeprecatedUsages, + printSchema, } from 'graphql'; import mergeSchemas from '../stitching/mergeSchemas'; import { @@ -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 }); @@ -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), } ]; diff --git a/src/utils/getResolversFromSchema.ts b/src/utils/getResolversFromSchema.ts new file mode 100644 index 00000000000..5b467b75c51 --- /dev/null +++ b/src/utils/getResolversFromSchema.ts @@ -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; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 2b01b5c4ea1..cabd47a08d5 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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';