Why is my context not typed on my Nexus, Prisma, TS project?

220 views Asked by At

I'm practicing setting up a Nexus, Prisma, TS API and am having issues getting my context typed on the resolvers.

src/context.ts:

import { PrismaClient } from "@prisma/client"
import { prisma } from "./db"

export interface Context {
    prisma: PrismaClient
}

export const context: Context = {
    prisma,
}

src/graphql/schema.ts:

import * as allTypes from "./types"
import path from "path"
import { makeSchema } from "nexus"

export const schema = makeSchema({
    types: allTypes,
    outputs: {
        schema: path.join(process.cwd(), "nexus", "schema.graphql"),
        typegen: path.join(process.cwd(), "nexus", "nexus.ts"),
    },
    contextType: {
        module: path.join(process.cwd(), "src/context.ts"),
        export: "Context",
    },
})

src/index.ts:

...
import { context } from "./context"
import { schema } from "./graphql/schema"

const main = async () => {
    dotenv.config()
    const app = express()

    const apolloServer = new ApolloServer({
        schema,
        context,
    })
...

src/graphql/user.ts:

export const getUser = queryField("getUser", {
    type: nullable(UserType),
    args: {
        id: nonNull(intArg()),
    },
    resolve: (
    _,
    { id }: Pick<User, "id">,
    { prisma } // <== not typed!
  ) =>
        prisma.user.findFirst({ where: { id } }),
})

My context is not typed, prisma is typed as any and I don't have any autocompletion on VS Code.

0

There are 0 answers