Use Prisma APIPrisma Bindings

Overview

Prisma Bindings

Prisma bindings are GraphQL bindings specifically for Prisma APIs. Think of them as an (auto-generated) SDK for Prisma.

The functions exposed by a Prisma binding are named after the root fields of the Prisma GraphQL schema (which defines your Prisma API). Each function call constructs and sends the respective query/mutation to the Prisma API.

Consider the following sample service configuration for a Prisma service:

prisma.yml

endpoint: http://localhost:4466
datamodel: datamodel.graphql
secret: my-secret-42

datamodel.graphql

type User {
  id: ID! @unique
  name: String!
}

Instantiating a Prisma binding

With above service configuration, you need to pass three arguments to the Prisma binding constructor:

const prisma = new Prisma({
  typeDefs: 'prisma.graphql',
  endpoint: 'http://localhost:4466'
  secret: 'my-secret-42'
})
  • typeDefs: The path to the Prisma GraphQL schema
  • endpoint: The endpoint of your Prisma API
  • secret: The service secret defined in prisma.yml

Sending queries

Sending the user query

// Retrieve `name` of a specific user
prisma.query.user({ where { id: 'cjcdi63j80adw0146z7r59bn5' } }, '{ name }')

This function call creates and sends the following query to the Prisma API:

query {
  user(where: { id: "cjcdi63j80adw0146z7r59bn5" }) {
    name
  }
}

Sending the users query

// Retrieve `id` and `name` of all users
prisma.query.users(null, '{ id name }')

This function call creates and sends the following query to the Prisma API:

query {
  users {
    id
    name
  }
}

Sending mutations

Sending the createUser mutation

// Create new user called `Sarah` and retrieve the `id`
prisma.mutation.createUser({ data: { name: 'Sarah' } }, '{ id }')

This function call creates and sends the following mutation to the Prisma API:

mutation {
  createUser(data: { name: "Sarah" }) {
    id
  }
}

Sending the updateUser mutation

// Update name of a specific user and retrieve the `id`
prisma.mutation.updateUser(
  { where: { id: 'cjcdi63j80adw0146z7r59bn5' }, data: { name: 'Sarah' } },
  '{ id }',
)

This function call creates and sends the following mutation to the Prisma API:

mutation {
  updateUser(
    where: {
      id: cjcdi63j80adw0146z7r59bn5""
    }
    data: {
      name: "Sarah"
    }
  ) {
    id
  }
}

Sending the deleteUser mutation

// Delete a specific user and retrieve the `name`
prisma.mutation.deleteUser(
  { where: { id: 'cjcdi63j80adw0146z7r59bn5' } },
  '{ id }',
)

This function call creates and sends the following mutation to the Prisma API:

mutation {
  deleteUser(
    where: {
      id: cjcdi63j80adw0146z7r59bn5""
    }
  ) {
    id
  }
}

Checking if a node exists

The Prisma binding API also allows to ask whether a specific node exists in the database:

// Ask whether a post exists with `id` equal to `cjcdi63j80adw0146z7r59bn5` and whose
// `author` is called `Sarah` (return boolean value)
prisma.exists.Post({
  id: 'cjcdi63j80adw0146z7r59bn5',
  author: {
    name: 'Sarah',
  },
})