Nexus: What is the correct way to define ContextType?

Created on 2 Jan 2021  ยท  9Comments  ยท  Source: graphql-nexus/nexus

I trying to get Prisma and Nexus working with NextJS, but I am having some trouble defining the contextType in the GraphQL schema.

I define the schema like this:

export const schema = makeSchema({
  types: [Query, User, Mutation],
  contextType: {
    module: require.resolve("./graphql/context"),
    export: "Context",
  },
  plugins: [nexusPrisma({ experimentalCRUD: true })],
  outputs: {
    typegen: path.join(process.cwd(), "generated", "nexus-typegen.ts"),
    schema: path.join(process.cwd(), "generated", "schema.graphql"),
  },
});

The error occurs when I start the development server by running npm run dev. The error is as follows:

Module not found: Can't resolve './graphql/context'
  46 |   types: [Query, User, Mutation],
  47 |   contextType: {
> 48 |     module: require.resolve("./graphql/context"),
     |            ^
  49 |     export: "Context",
  50 |   },
  51 |   plugins: [nexusPrisma({ experimentalCRUD: true })],

This is the context.ts file:

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

export interface Context {
  prisma: PrismaClient;
}

export function createContext(): Context {
  return { prisma };
}

My dependencies are these:

  "dependencies": {
    "@prisma/client": "^2.13.1",
    "apollo-server-micro": "^2.19.1",
    "next": "latest",
    "nexus": "^1.0.0",
    "nexus-plugin-prisma": "^0.27.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },
  "devDependencies": {
    "@prisma/cli": "^2.13.1",
    "@types/node": "^12.12.21",
    "@types/react": "^16.9.16",
    "@types/react-dom": "^16.9.4",
    "typescript": "^4.1.3"
  },

Any help is appreciated.

Project tree:

โ”œโ”€ generated
โ”‚  โ”œโ”€ nexus-typegen.ts
โ”‚  โ””โ”€ schema.graphql
โ”œโ”€ graphql
โ”‚  โ”œโ”€ context.ts
โ”‚  โ””โ”€ schema.ts
โ”œโ”€ interfaces
โ”‚  โ””โ”€ index.ts
โ”œโ”€ next-env.d.ts
โ”œโ”€ package-lock.json
โ”œโ”€ package.json
โ”œโ”€ pages
โ”‚  โ”œโ”€ api
โ”‚  โ”‚  โ”œโ”€ graphql.ts
โ”œโ”€ prisma
โ”‚  โ””โ”€ schema.prisma
โ”œโ”€ tsconfig.json

I posted this question also to Stack Overflow.

Most helpful comment

All 9 comments

I think the error is in the path you provided for your module. Assuming that you've defined the schema in ./schema.ts, your context type should look like this as it's the relative path that you need to specify

contextType: {
  module: require.resolve("./context"),
  export: "Context",
}

I think the error is in the path you provided for your module. Assuming that you've defined the schema in ./schema.ts, your context type should look like this as it's the relative path that you need to specify

contextType: {
  module: require.resolve("./context"),
  export: "Context",
}

Getting somewhere, but there is still an error:

Error: Expected an absolute path or Node package for the root typing path of the type context, saw ./graphql/context.ts
    at Object.resolveImportPath (C:\***\root\node_modules\nexus\dist\utils.js:399:15)
    at TypegenPrinter.printDynamicImport (C:\***\root\node_modules\nexus\dist\typegenPrinter.js:136:40) 
    at TypegenPrinter.printHeaders (C:\***\root\node_modules\nexus\dist\typegenPrinter.js:84:18)        
    at TypegenPrinter.print (C:\***\root\node_modules\nexus\dist\typegenPrinter.js:73:22)
    at TypegenMetadata.<anonymous> (C:\***\root\node_modules\nexus\dist\typegenMetadata.js:109:128)     
    at Generator.next (<anonymous>)
    at fulfilled (C:\***\root\node_modules\nexus\node_modules\tslib\tslib.js:111:62)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

When I hover over the module property, VSCode tells me this:

(property) TypingImport.module: string
An absolute path to a module in your project or the name of a package installed in your project.

That is why I figured that an absolute path was needed.

@slinden2 require.resolve() is supposed to return the absolute path, not sure why it's returning ./graphql/context.ts. Could you try passing path.join(__dirname, "context") to the module property?

@shreyas44 I tried a few things that all end up in an error.

Attempt 1:

module: path.join(__dirname, "context")
Error: Root typing path \context for the type context does not exist



md5-3274f5d201ff0e4f0540b83cd5291397



module: path.join(process.cwd(), "graphql", "context")



md5-c7dfdd673edcdc380563f429a4316f51



Error: Root typing path C:\***\root\graphql\context for the type context does not exist

@slinden2 could try adding the .ts extension like so path.join(__dirname, "graphql", "context.ts")?

@shreyas44 adding the .ts extension doesn't change anything. The error remains the same. ๐Ÿ˜ž

No, I think I messed up something before. This seems to work:

  contextType: {
    module: path.join(process.cwd(), "graphql", "context.ts"),
    export: "Context",
  }

I don't know if this stuff is somehow related to NextJS, but the way indicated in the documentation just doesn't work for me.

Solved for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisdrackett picture chrisdrackett  ยท  3Comments

deadcoder0904 picture deadcoder0904  ยท  3Comments

huv1k picture huv1k  ยท  6Comments

saostad picture saostad  ยท  3Comments

ryands17 picture ryands17  ยท  4Comments