question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the correct way to define ContextType?

See original GitHub issue

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:

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

I posted this question also to Stack Overflow.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

What is the correct way to define a React component's ...
Type '{ router: Requireable<any>; }' is not assignable to type 'ValidationMap<any>'. Changing the contextTypes to static contextTypes: React.
Read more >
What is contextType in React? - DEV Community ‍ ‍
To use the new way, at the top of the class we declare a static property called contextType and set it equal to...
Read more >
Context - React
Context provides a way to pass data through the component tree without having to ... Component { // Assign a contextType to read...
Read more >
contextType property of a React component ... - DeepScan
This rule applies when the static contextType property of a React component is not properly specified. The contextType property is used toΒ ...
Read more >
How to use React Context with TypeScript - Felix Gerschau
In this tutorial, you will learn how to use React Context with TypeScript, including function and class components.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found