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.
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 specifycontextType: { 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.
https://nexusjs.org/docs/adoption-guides/nextjs-users#sdl-and-type-generation
Most helpful comment
https://nexusjs.org/docs/adoption-guides/nextjs-users#sdl-and-type-generation