docs/1.14/04-Reference/08-Prisma-Bindings/01-Overview.md
prisma-binding is a dedicated GraphQL binding for Prisma GraphQL APIs. Think of it like an auto-generated SDK for Prisma services:
Note: If you're curious about this topic, you can read the following two blog posts:
- [Reusing & Composing GraphQL APIs with GraphQL Bindings] (https://blog.graph.cool/reusing-composing-graphql-apis-with-graphql-bindings-80a4aa37cff5)
- GraphQL Binding 2.0: Improved API, schema transforms & automatic codegen
prisma-binding provides a convenience layer for building GraphQL servers on top of Prisma services. In short, it simplifies implementing your GraphQL resolvers by delegating execution of queries (or mutations) to the API of the underlying Prisma database service. Rather than writing SQL or accessing a NoSQL database API like MongoDB inside your resolvers, most of your resolver will be implemented as simple one-liners.
Consider the following application schema for your GraphQL server:
# import Post from "./generated/prisma.graphql"
type Query {
posts: [Post!]!
post(id: ID!): Post
description: String!
}
type Mutation {
createDraft(title: String!, text: String): Post
deletePost(id: ID!): Post
publish(id: ID!): Post
}
This is how the corresponding resolvers are implemented with a Prisma binding available as a db object on ctx:
const resolvers = {
Query: {
posts(parent, args, ctx, info) {
return ctx.db.query.posts({ }, info)
},
post(parent, args, ctx, info) {
return ctx.db.query.post({ where: { id: args.id } }, info)
},
},
Mutation: {
createDraft(parent, { title, text }, ctx, info) {
return ctx.db.mutation.createPost(
{
data: {
title,
text,
},
},
info,
)
},
deletePost(parent, { id }, ctx, info) {
return ctx.db.mutation.deletePost({ where: { id } }, info)
},
publish(parent, { id }, ctx, info) {
return ctx.db.mutation.updatePost(
{
where: { id },
data: { isPublished: true },
},
info,
)
},
},
}
Note: To learn more about this particular example, check out this tutorial.
Here is how it works:
prisma.graphql (contains the full CRUD API)schema.graphqlPrisma with information about your Prisma service (such as its endpoint and the path to the database schema definition)Note: If you're using a GraphQL boilerplate project (e.g. with
graphql create), the Prisma binding will already be configured and a few example resolvers implemented for you. You can either try the dynamic binding (e.g. in thenode-basicboilerplate) or a static binding (e.g in thetypescript-basicboilerplate).