reactjstypescriptgraphqlnext.jsnexus

Nexus & GraphQL: Root typing path for the type "context" does not exist


I am trying to run graphql in Next.js API routes.

I am using nexus to write the graphql schema. This is the two files context.ts and schema.ts file for setting up nexus development mode.

// context.ts
import { database } from "../loaders/database";
import { PrismaClient } from "@prisma/client";

export interface Context {
  database: PrismaClient;
}

export const context: Context = {
  database,
};

// schema.ts
import { makeSchema } from "nexus";
import { nexusPrisma } from "nexus-plugin-prisma";
import { join } from "path";

import * as types from "./types";

export const schema = makeSchema({
  types,
  plugins: [
    nexusPrisma({
      prismaClient: (ctx) => ctx.database,
      experimentalCRUD: true,
    }),
  ],
  outputs: {
    schema: join(__dirname, "generated", "schema.graphql"),
    typegen: join(__dirname, "generated", "nexus-typegen.ts"),
  },
  contextType: {
    module: join(__dirname, "context.ts"),
    export: "Context",
  }
});

I searched online and the closet I found is here where they used sourceTypes to solve the issue. I tried it but the error doesn't go away.

I used the following script to generate the schema and types for graphql.

{
  "scripts": {
      "generate:nexus": "ts-node --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts",
  }
}

It's giving the following error though the code is compiled successfully.

event - build page: /api/graphql
event - compiled successfully
Error: Root typing path "/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/.next/server/pages/api/context.ts" for the type "context" does not exist
    at Object.resolveImportPath (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/utils.js:411:15)
    at TypegenPrinter.printDynamicImport (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:132:40)
    at TypegenPrinter.printHeaders (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:80:18)
    at TypegenPrinter.print (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:69:22)
    at TypegenMetadata.<anonymous> (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenMetadata.js:109:128)
    at Generator.next (<anonymous>)
    at fulfilled (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/tslib/tslib.js:114:62)

Can anyone please help me where I am doing wrong?

Thanks!


Solution

  • After some debugging, I finally found the solution.

    First of all, the nexus documentation has a part for next.js users which is a must-read for all. link.

    I have to replace the __dirname with the process.cwd(). The problem is finally solved.

    // schema.ts
    
    import { makeSchema } from "nexus";
    import { nexusPrisma } from "nexus-plugin-prisma";
    import { join } from "path";
    
    import * as types from "./types";
    
    export const schema = makeSchema({
      types,
      plugins: [
        nexusPrisma({
          prismaClient: (ctx) => ctx.database,
          experimentalCRUD: true,
        }),
      ],
      outputs: {
        schema: join(process.cwd(), "src/server/graphql/generated/schema.graphql"),
        typegen: join(process.cwd(), "src/server/graphql/generated/nexus-typegen.ts"),
      },
      contextType: {
        module: join(process.cwd(), "src/server/graphql/context.ts"),
        export: "Context",
      },
    });