I am currently facing a challenge while trying to integrate Prisma and Nexus into NextJS. The issue arises when I attempt to define the contextType in the GraphQL schema.
Here is how I have defined the schema:
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"),
},
});
When I try to start the development server using npm run dev
, I encounter the following error:
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 })],
The content of the context.ts
file looks like this:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export interface Context {
prisma: PrismaClient;
}
export function createContext(): Context {
return { prisma };
}
Below are the dependencies for my project:
"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"
},
I have been searching for a solution but haven't been able to find one yet. It seems like the issue might be related to Webpack configurations in NextJS, but as I lack knowledge in that area, I'm unsure about what steps to take next.
Any assistance would be greatly appreciated.
Directory structure of the project:
root
├─ 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