This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to get graphql-tools style resolvers map from an existing schema, parallels graphql-js toConfig.
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters