In my project, I have a user schema defined in a file named userSchema.graphql;
id: String!
userName: String!
email: String!
password: String!
}
In addition to the user schema, I also have separate schema files for login and register functionalities.
However, I encountered an issue where I could not access the User schema within the login/register schema.
type Query{
hi: String!
}
type Mutation {
register( userName: String!, email: String!, password: String! ): User
}
To resolve this problem, I decided to use schema stitching with graphql tools and merge typeDefs and resolvers using the merge-graphql-schema package.
import "graphql-import-node";
import { GraphQLSchema } from "graphql";
import { mergeResolvers, mergeTypes } from "merge-graphql-schemas";
import * as userSchema from './modules/auth/shared/schema.graphql'
import * as registerTypedefs from "./modules/auth/register/schema.graphql";
import registerResolver from "./modules/auth/register/registerResolver";
import * as todoTypedefs from "./modules/todo/schema.graphql";
import todoResolver from "./modules/todo/todoResolver";
import { makeExecutableSchema } from "graphql-tools";
const schema: GraphQLSchema = makeExecutableSchema({
typeDefs: mergeTypes([userSchema, registerTypedefs, todoTypedefs], { all: true }),
resolvers: mergeResolvers([registerResolver, todoResolver])
});
export default schema;
However, I ran into an error message:
Error: Field register: Couldn't find type User in any of the schemas.
at collectNode (E:\graphql\express-apollo-server\node_modules\graphql-import\src\definition.ts:154:15)
at E:\graphql\express-apollo-server\node_modules\graphql-import\src\definition.ts:135:7
at Array.forEach (<anonymous>)
EDIT After further investigation, I utilized the fileLoader function from 'merge-graphql-schemas' which resolved the issue seamlessly.
import { mergeResolvers, mergeTypes, fileLoader } from "merge-graphql-schemas";
const typesArray = fileLoader(path.join(__dirname, './modules/**/*.graphql'), { recursive: true })
const schema: GraphQLSchema = makeExecutableSchema({
typeDefs: mergeTypes(typesArray, { all: true }),
resolvers: mergeResolvers([registerResolver, loginRegister, todoResolver, storyResolver])
});