Building a Basic GraphQL Server with Prisma

Getting Started with Prisma

Prisma is an open-source database toolkit that simplifies database access by providing an auto-generated query builder for TypeScript and Node.js. With Prisma, you can focus on your data without worrying about native database queries. The toolkit consists of three main parts: Prisma Client, Prisma Migrate, and Prisma Studio.

Creating a Basic Polling GraphQL Server

To build a basic polling GraphQL server, you’ll need a basic understanding of Prisma and Node.js. Ensure you have Node and Yarn or npm installed on your machine.

Setting Up the Project

Create a new folder for your project and add a README.md file with a description of your server. Initialize a package.json file using the following command:

yarn init -y

Installing Prisma and Setting Up the Database

Install Prisma 2 globally and create a new folder called prisma with a schema.prisma file. Define your database connection, generator, and data model in the schema.prisma file:
“`prisma
model User {
id String @id @default(cuid())
name String
polls Poll[]
}

model Poll {
id String @id @default(cuid())
title String
options String[]
user User @relation(fields: [id], references: [id])
votes Vote[]
}

model Vote {
id String @id @default(cuid())
poll Poll @relation(fields: [id], references: [id])
user User @relation(fields: [id], references: [id])
option String
}
“`
Generating the Prisma Client

Run the following command to generate the Prisma Client:

yarn add @prisma/client

Creating the GraphQL Server

Create a new file called index.js and add the following code:
“`javascript
const { GraphQLServer } = require(‘@graphql-yoga/node’);
const { PrismaClient } = require(‘@prisma/client’);

const prisma = new PrismaClient();

const typeDefs = `
type Query {
users: [User]
polls: [Poll]
}

type Mutation {
createUser(name: String!): User
createPoll(title: String!, options: [String!]!): Poll
vote(pollId: String!, option: String!): Vote
}

type User {
id: String!
name: String!
polls: [Poll]
}

type Poll {
id: String!
title: String!
options: [String!]!
user: User!
votes: [Vote]
}

type Vote {
id: String!
poll: Poll!
user: User!
option: String!
}
`;

const resolvers = {
Query: {
users: async () => {
return await prisma.user.findMany();
},
polls: async () => {
return await prisma.poll.findMany();
},
},
Mutation: {
createUser: async (parent, { name }) => {
return await prisma.user.create({ data: { name } });
},
createPoll: async (parent, { title, options }) => {
return await prisma.poll.create({ data: { title, options } });
},
vote: async (parent, { pollId, option }) => {
return await prisma.vote.create({ data: { pollId, option } });
},
},
};

const server = new GraphQLServer({ typeDefs, resolvers, context: { prisma } });
server.start(() => console.log(‘Server is running on http://localhost:8000’));
“`
Testing the Server

Start the server using the following command:

node index.js

Open a web browser and navigate to http://localhost:8000/playground. Test the server by running queries and mutations.

Next Steps

This article demonstrated how to build a basic GraphQL server using Prisma. To take it further, you can implement authentication, add resolver directives, and move type definitions and resolvers to separate files. The Prisma website provides extensive documentation on working with Prisma.

Monitor Your GraphQL Server

LogRocket can help you monitor and debug your GraphQL server. It provides a comprehensive overview of your server’s performance and helps you identify issues quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *