-
More hands on leanring with prisma 2 today. Followed this tutorial to setup up a Next, TS, Prisma, GraphQL and have CRUD operations with Nexus.
- Next already has an API instance that comes with it so dont need to setup node seperately
- Prisma doesnt come with shipped with graphql anymore, but we can easily setup graphql with Apollo Server by creating a graphql enpoint in the
pages/api
directory, then add the following boilerplate to a file calledgraphql.ts
import { ApolloServer } from "apollo-server-micro"; import { schema } from "src/schema"; import { createContext } from "src/context"; const server = new ApolloServer({ schema, context: createContext, tracing: process.env.NODE_ENV === "development", }); const handler = server.createHandler({ path: "/api/graphql" }); export const config = { api: { bodyParser: false, }, }; export default handler;
context is factory function which returns an instance of Prisma:
//createContext.ts import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export interface context { prisma: PrismaClient; } export function createContext() { return { prisma, }; }
- As Prisma 2 does not come with grapql server anymore and is more a library. This means that it does not come shipped with graphql CRUD operation out of the box. Nexus is a new library which can create these CRUD operations based on an existing schema. It has the ability to create queries and mutations and for a single or mutliple elements.