0
0
GraphQLquery~10 mins

Prisma ORM with GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prisma ORM with GraphQL
Define Prisma schema
Generate Prisma Client
Set up GraphQL schema
Create resolvers using Prisma Client
GraphQL server handles queries/mutations
Resolvers call Prisma Client to access DB
Return data to GraphQL client
This flow shows how Prisma schema leads to a client used in GraphQL resolvers, which handle queries and mutations to access the database.
Execution Sample
GraphQL
model Post {
  id    Int     @id @default(autoincrement())
  title String
  body  String
}

query {
  posts {
    id
    title
  }
}
Defines a Post model in Prisma and a GraphQL query to fetch all posts with id and title.
Execution Table
StepActionPrisma Client CallDatabase QueryGraphQL Response
1GraphQL query receivedNoneNoneNone
2Resolver calls prisma.post.findMany()prisma.post.findMany()SELECT id, title, body FROM Post;None
3Database returns rowsNone[{id:1, title:'Hello', body:'World'}, {id:2, title:'Hi', body:'GraphQL'}]None
4Resolver returns filtered dataNoneNone[{id:1, title:'Hello'}, {id:2, title:'Hi'}]
5GraphQL server sends responseNoneNone[{id:1, title:'Hello'}, {id:2, title:'Hi'}]
💡 Query completed and response sent to client.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
GraphQL QueryNone{ posts { id title } }{ posts { id title } }{ posts { id title } }None
Prisma Client CallNoneprisma.post.findMany()prisma.post.findMany()prisma.post.findMany()None
Database ResultNoneNone[{id:1, title:'Hello', body:'World'}, {id:2, title:'Hi', body:'GraphQL'}][{id:1, title:'Hello', body:'World'}, {id:2, title:'Hi', body:'GraphQL'}]None
GraphQL ResponseNoneNoneNone[{id:1, title:'Hello'}, {id:2, title:'Hi'}][{id:1, title:'Hello'}, {id:2, title:'Hi'}]
Key Moments - 3 Insights
Why does the resolver call prisma.post.findMany() instead of directly querying the database?
The resolver uses Prisma Client to abstract database queries, making code simpler and safer. See execution_table step 2 where the Prisma Client call happens instead of raw SQL.
Why does the GraphQL response only include id and title, not the body?
GraphQL returns only requested fields. The query asks for id and title, so the resolver filters out body before sending response (execution_table step 4).
What happens if the database returns no posts?
Prisma Client returns an empty array, so the resolver returns an empty list in GraphQL response, ending the query gracefully.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the database query Prisma Client sends at step 2?
ASELECT id, title FROM Post;
BSELECT id, title, body FROM Post;
Cprisma.post.findMany()
DGraphQL query posts { id title }
💡 Hint
Check the 'Database Query' column at step 2 in execution_table.
At which step does the resolver filter the database result to only include requested fields?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'GraphQL Response' column in execution_table where filtered data appears.
If the GraphQL query requested the 'body' field too, how would the GraphQL response at step 5 change?
AIt would include id, title, and body fields for each post.
BIt would only include id and title as before.
CIt would cause an error because body is not in Prisma schema.
DIt would return an empty list.
💡 Hint
Refer to variable_tracker and execution_table steps showing how requested fields affect response.
Concept Snapshot
Prisma ORM with GraphQL:
- Define data models in Prisma schema.
- Generate Prisma Client to access DB.
- Create GraphQL schema with types and queries.
- Write resolvers calling Prisma Client methods.
- GraphQL queries invoke resolvers, which fetch data via Prisma.
- Responses include only requested fields, ensuring efficient data fetching.
Full Transcript
This visual execution trace shows how Prisma ORM integrates with GraphQL. First, you define your data model in the Prisma schema. Then, Prisma Client is generated to provide easy database access. In GraphQL, you define your schema and resolvers. When a GraphQL query arrives, the resolver calls Prisma Client methods like findMany to fetch data from the database. The database returns full records, but the resolver filters the data to include only the fields requested by the GraphQL query. Finally, the filtered data is sent back as the GraphQL response. This flow helps keep database access safe, simple, and efficient by using Prisma as an abstraction layer and GraphQL to control data shape.