Unable to retrieve shared schema from a different schema.graphql file within the context of schema stitching

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])
});

Answer №1

In my project, I successfully utilized a combination of require-graphql-file and lodash merge to achieve the desired outcome.

import requireGraphQLFile from 'require-graphql-file';
import { merge } from 'lodash';

const $userSchema = requireGraphQLFile('./schema/yourFolder/userSchema.graphql');
const $registerTypedefs= requireGraphQLFile('./schema/yourFolder/register.graphql');

import registerResolver from "./modules/auth/register/registerResolver";
import todoResolver from "./modules/todo/todoResolver";

const schema: GraphQLSchema = makeExecutableSchema({
   typeDefs: [$userSchema, 
              $registerTypedefs,
              todoTypedefs
             ],              
   resolvers: merge(registerResolver, todoResolver)
});

I trust this solution can be of assistance to you as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an error in production mode with Angular 7: Uncaught ReferenceError - "environment" variable

During development, my application runs smoothly, and ng build --prod --source-map successfully compiles the application. However, when attempting to access it through the browser, an error occurs: app.module.ts:47 Uncaught ReferenceError: env is not defi ...

What is the best way to manage multiple directives from a parent component?

Within my app-landing component, I have implemented multiple typeOut directives. These directives are responsible for gradually writing out text within their respective elements. While this functionality is working as intended, I now seek to exert control ...

What is the significance of default parameters in a JavaScript IIFE within a TypeScript module?

If I were to create a basic TypeScript module called test, it would appear as follows: module test { export class MyTest { name = "hello"; } } The resulting JavaScript generates an IIFE structured like this: var test; (function (test) { ...

What is the best way to perform a deep copy in Angular 4 without relying on JQuery functions?

Within my application, I am working with an array of heroes which are displayed in a list using *ngFor. When a user clicks on a hero in the list, the hero is copied to a new variable and that variable is then bound to an input field using two-way binding. ...

Angular HTTP Interceptor delays processing of http requests until a new refresh token is obtained

After creating my AuthInterceptor to handle 401 errors by requesting a new token, I encountered a problem. The handle401Error method is supposed to wait for other HTTP requests until the new token is received, but it isn't waiting as expected. Even th ...

Enhancing TypeScript Modules

Recently, I encountered an issue with my observable extension. Everything was functioning perfectly until I updated to angular 6 and typescript 2.7.2. import { Observable } from 'rxjs/Observable'; import { BaseComponent } from './base-compo ...

Unused code splitting chunk in React production build would improve performance and efficiency of

When running the command npm run build, a build directory is generated with js chunks. I have observed an unfamiliar file named [number].[hash].chunk.js that does not appear in the list of entrypoints in the asset-manifest.json file. Instead, this mysteri ...

Unable to transfer object from Angular service to controller

I am currently utilizing a service to make a $http.get request for my object and then transfer it to my controller. Although the custom service (getService) successfully retrieves the data object and saves it in the responseObj.Announcement variable when ...

JavaScript's async function has the capability to halt execution on its own accord

Presented here is a JavaScript async function designed to populate a sudoku board with numbers, essentially solving the puzzle. To enhance the user experience and showcase the recursion and backtracking algorithm in action, a sleeper function is utilized b ...

Filter the array while maintaining its current structure

I'm struggling to create an array filter that can handle exact and partial data within a nested array structure. The challenge is maintaining the integrity of the top-level structure while filtering based on data in the second layer. Here's an ex ...

What is the best approach to retrieve all user information using React with TypeScript and the Redux Toolkit?

I'm currently using React with TypeScript and Redux Toolkit, but I've hit a roadblock trying to retrieve user information. Below is my userSlice.ts file: export const userSlice = createSlice({ name: "user", initialState: { user: null, } ...

What is causing the transpiler to not be triggered by the code change?

My current project involves using a TypeScript backend for a Dialogflow application with fulfillment. I initially used a preconfigured project template and didn't delve into the details. I work in VS Code and never explicitly build my code. Instead, ...

Binding an ID to an <ion-textarea> in Ionic 3

Can an ID be assigned to an ion-textarea? For example: <ion-textarea placeholder="Enter your thoughts" id="thoughtsBox"></ion-textarea> Thank you very much ...

Adjust the component suppliers based on the @input

If I were to implement a material datepicker with a selection strategy, I would refer to this example There are instances where the selection strategy should not be used. The challenge lies in setting the selection strategy conditionally when it is insta ...

I encountered an issue while trying to implement a custom pipe using the built-in pipe

My custom pipe seems to be functioning well, except for the built-in pipes not working within it. I've imported Pipe and can't figure out what could be causing the issue. Below is the code with the errors: import { Pipe, PipeTransform } from &a ...

How can I generate codegen types using typeDefs?

I have been exploring the capabilities of graphql-codegen to automatically generate TypeScript types from my GraphQL type definitions. However, I encountered an issue where I received the following error message: Failed to load schema from code file " ...

Using custom Components to accept HTML input

I have recently developed a custom component to arrange content within IonCardContent. It has been effective for my current requirements: interface ContainerProps { position?: string; content?: string, colour?: string; custClass?: string; } ...

If an Angular reactive form component has a particular value

I am working with a group of radio buttons. When a user chooses the option "yes," I would like to display an additional input box on the form. Link to Code Example HTML.component <div formGroupName="radioButtonsGroup" class="form-group col-6 pl-0 pt- ...

What kind of impact on performance can be expected when using index.ts in a Typescript - Ionic App?

When setting up the structure of a standard Ionic app, it typically looks like this: app pages ----page1 ---------page1.ts ----page2 ---------page2.ts If I were to include an index.ts file in the pages folder as follows: pages/index.ts export { Page1 } ...

I am having trouble with my quiz function as it only checks the answer correctly for the first question. Does anyone have suggestions on how to make it work for

Currently, I'm tackling a quiz project that was assigned to me during my bootcamp. My focus right now is on the checkAnswer function, which evaluates the answer selected by the player. const startButton = document.querySelector(".start") as ...