0
0
GraphQLquery~30 mins

Prisma ORM with GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Build a Simple Prisma ORM with GraphQL API
📖 Scenario: You are creating a small backend API for a bookstore. The API will allow users to query and add books using GraphQL. The data will be stored using Prisma ORM connected to a database.
🎯 Goal: Build a GraphQL API that uses Prisma ORM to manage a list of books with fields id, title, and author. You will create the Prisma schema, configure the Prisma client, write GraphQL type definitions, and implement resolvers to query and add books.
📋 What You'll Learn
Create a Prisma schema with a Book model having id, title, and author fields
Generate Prisma client from the schema
Set up GraphQL type definitions for Book and queries/mutations
Implement resolvers to fetch all books and add a new book using Prisma client
💡 Why This Matters
🌍 Real World
Building backend APIs for applications that need to manage data with a database and expose it via GraphQL.
💼 Career
Understanding Prisma ORM and GraphQL is valuable for backend developers working with modern JavaScript/TypeScript stacks.
Progress0 / 4 steps
1
Create Prisma schema with Book model
Create a Prisma schema file named schema.prisma with a Book model. The model should have an id field of type Int that is the primary key and auto-increments, a title field of type String, and an author field of type String. Also include a datasource block for SQLite and a generator block for Prisma client.
GraphQL
Hint

Remember to define the datasource and generator blocks before the model.

2
Generate Prisma client and import it
After creating the Prisma schema, run npx prisma generate to generate the Prisma client. Then, in your GraphQL server file, import PrismaClient from @prisma/client and create a new instance called prisma.
GraphQL
Hint

Use import { PrismaClient } from '@prisma/client' and then const prisma = new PrismaClient().

3
Define GraphQL type definitions for Book and queries
Define GraphQL type definitions using gql from apollo-server. Create a Book type with id (ID!), title (String!), and author (String!). Define a Query type with a books field that returns a list of Book. Also define a Mutation type with addBook(title: String!, author: String!): Book!.
GraphQL
Hint

Use backticks to define the GraphQL schema string with gql.

4
Implement resolvers to query and add books
Create a resolvers object with a Query field containing a books resolver that returns prisma.book.findMany(). Add a Mutation field with an addBook resolver that takes title and author from args and returns prisma.book.create with the data. Export both typeDefs and resolvers.
GraphQL
Hint

Resolvers connect GraphQL queries and mutations to Prisma client methods.