📖 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.
Practice
(1/5)
1. What is the main role of Prisma ORM when used with GraphQL?
easy
A. To define GraphQL schema types
B. To style the frontend user interface
C. To handle HTTP requests from clients
D. To manage database access and queries easily
Solution
Step 1: Understand Prisma ORM's purpose
Prisma ORM acts as a tool to simplify database operations by providing an easy way to query and manage data.
Step 2: Differentiate Prisma from GraphQL schema and frontend roles
GraphQL schema defines data types and operations, while frontend styling is unrelated to Prisma's role.
Final Answer:
To manage database access and queries easily -> Option D
prisma.user.create returns a Promise, so it needs await to resolve before returning.
Step 2: Identify missing await causing error
Without await, newUser is a Promise, not the created user object, causing issues in return.
Final Answer:
Missing await before prisma.user.create call -> Option A
Quick Check:
Async Prisma calls need await [OK]
Hint: Always await Prisma async calls in resolvers [OK]
Common Mistakes:
Forgetting to await async Prisma methods
Assuming create returns data synchronously
Confusing create with findMany usage
5. You want to fetch a user and their posts in one GraphQL query using Prisma. Which resolver code correctly fetches the user with their posts included?
hard
A. const user = await prisma.user.findUnique({ where: { id }, include: { posts: true } });
B. const user = await prisma.user.findMany({ where: { id }, select: { posts: false } });
C. const user = await prisma.post.findMany({ where: { userId: id } });
D. const user = await prisma.user.findUnique({ where: { id } }); const posts = await prisma.post.findMany({ where: { userId: id } });
Solution
Step 1: Understand Prisma's include option
Using include: { posts: true } fetches related posts along with the user in one query.
Step 2: Compare options for fetching user with posts
const user = await prisma.user.findUnique({ where: { id }, include: { posts: true } }); uses findUnique with include correctly; const user = await prisma.user.findMany({ where: { id }, select: { posts: false } }); excludes posts; const user = await prisma.post.findMany({ where: { userId: id } }); fetches posts only; const user = await prisma.user.findUnique({ where: { id } }); const posts = await prisma.post.findMany({ where: { userId: id } }); fetches separately, not in one query.
Final Answer:
const user = await prisma.user.findUnique({ where: { id }, include: { posts: true } }); -> Option A
Quick Check:
Include fetches relations in one query [OK]
Hint: Use include to fetch related data in one Prisma query [OK]
Common Mistakes:
Using findMany instead of findUnique for single user
Not using include to fetch related posts
Fetching posts separately causing extra queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.