graphql-tools: mergeSchemas doesn't allow built in scalars in secondary schemas
I have the following code:
return mergeSchemas({
schemas: [actions, typeDefs],
resolvers,
schemaDirectives: {
date: DateFormatDirective,
},
});
actions
is a schema made using makeRemoteExecutableSchema and typeDefs
is a big gql schema string. The actions
schema does not contain any use of the built in Float, but typeDefs
does.
When you query the actions schema directly:
{
__type(name:"Float") {
name
}
}
you get back
{
"data": {
"__type": null
}
}
rather than
{
"data": {
"__type": {
"name": "Float"
}
}
}
This is normal and expected.
However, when the above mergeSchemas
function is called, an error is thrown:
Error: Can't find type Float.
at /usr/src/app/node_modules/graphql-tools/src/stitching/mergeSchemas.ts:102:13
at resolveType (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:187:14)
at /usr/src/app/node_modules/graphql-tools/src/stitching/schemaRecreation.ts:156:18
at Array.forEach (<anonymous>)
at fieldMapToFieldConfigMap (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:154:23)
at fields (node_modules/graphql-tools/src/stitching/schemaRecreation.ts:48:9)
at resolveThunk (node_modules/graphql/type/definition.js:370:40)
at defineFieldMap (node_modules/graphql/type/definition.js:532:18)
at GraphQLObjectType.getFields (node_modules/graphql/type/definition.js:506:44)
at typeMapReducer (node_modules/graphql/type/schema.js:232:38)
at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
at typeMapReducer (node_modules/graphql/type/schema.js:213:12)
at /usr/src/app/node_modules/graphql/type/schema.js:239:20
at Array.forEach (<anonymous>)
at typeMapReducer (node_modules/graphql/type/schema.js:232:51)
at /usr/src/app/node_modules/graphql/type/schema.js:239:20
at Array.forEach (<anonymous>)
at typeMapReducer (node_modules/graphql/type/schema.js:232:51)
at Array.reduce (<anonymous>)
at new GraphQLSchema (node_modules/graphql/type/schema.js:122:28)
at mergeSchemasImplementation (node_modules/graphql-tools/src/stitching/mergeSchemas.ts:243:22)
at mergeSchemas (node_modules/graphql-tools/src/stitching/mergeSchemas.ts:75:10)
...
It seems like the built in scalar types have to be used in the first schema for them to be valid in any subsequent schemas passed to mergeSchemas
.
I’m guessing this isn’t intentional functionality 😄
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 17
- Comments: 15
Found a “quick n dirty” work around that does the job until fixed. Simply apply a “mock” schema as the 1st element in the array of schemas to be merged.
Adding the reference to the types seemed to suppress any issues with GraphQLScalarType being undefined.
There may be a nicer way of doing this (tidyer mocked schema etc), but wanted to post the general jist to help out any stranded devs! 😃
@hayes you are right, using makeExecutableSchema does fix it
Instead of doing
I’m doing
Note: For this to work, the resolvers have to be part of the mergeSchemas, they can’t be part of the ApolloServer props. One issue with this is with the typings, the resolvers params in mergeSchemas expects a
IResolversParameter
when the ApolloServer expects anIResolvers
. Not sure what’s the difference thoThe problem seems to be fixed in
graphql-tools
version5.0.0-rc.0
🎉 .I hope we can have a stable version soon 😃
update to
5.0.0-rc.0
. It solved the issue for me.