graphql / express-graphql

Create a GraphQL HTTP server with Express.
MIT License
6.34k stars 538 forks source link

Unable to find Prisma Client in GraphQL context. Please provide it under the `context[\"prisma\"]` key. #810

Closed bmccorm2 closed 2 years ago

bmccorm2 commented 2 years ago

Hi - I'm using some various packages to try and get a graphql server up and running: type-graphql to generate my schema, and prisma to connect to my database. Everything is working fine but it seems express doesn't have access to the prisma client. Do you know what i am doing wrong?

Error: Unable to find Prisma Client in GraphQL context. Please provide it under the context[\"prisma\"] key

import express from "express";
import "reflect-metadata";
import { PrismaClient } from "@prisma/client";
import { resolvers } from "@generated/type-graphql";
import { buildSchema } from "type-graphql";
import { graphqlHTTP } from "express-graphql";

(async () => {
    const prisma = new PrismaClient();
    const schema = await buildSchema({
        resolvers,
        validate: false,
    });

    const app = express();
    app.use(
        "/graphql",
        graphqlHTTP({
            schema,
            context: () => ({ prisma }),
            graphiql: true,
        })
    );

    app.listen(3000, () => {
        console.log(`Example app listening on port 3000`);
    });
})();
bmccorm2 commented 2 years ago

Resolved my issue here: https://github.com/prisma/prisma-examples/tree/latest/typescript/graphql-express