prismagraphql/prisma-binding

Name: prisma-binding

Owner: Prisma

Description: GraphQL Binding for Prisma services (GraphQL Database)

Created: 2017-11-29 18:26:05.0

Updated: 2018-05-22 21:39:37.0

Pushed: 2018-05-22 16:47:16.0

Homepage:

Size: 632

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

prisma-binding

CircleCI npm version

GraphQL Binding for Prisma services (GraphQL Database)

Overview

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.

Here is how it works:

  1. Create your Prisma service by defining data model
  2. Download generated database schema definition prisma.graphql (contains the full CRUD API)
  3. Define your application schema, typically called app.graphql
  4. Instantiate Prisma with information about your Prisma service (such as its endpoint and the path to the database schema definition)
  5. Implement the resolvers for your application schema by delegating to the underlying Prisma service using the generated delegate resolver functions

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 the node-basic boilerplate) or a static binding (e.g in the typescript-basic boilerplate).

Install
 add prisma-binding

install --save prisma-binding
Example

Consider the following data model for your Prisma service:

 User {
: ID! @unique
me: String

If you instantiate Prisma based on this service, you'll be able to send the following queries/mutations:

nstantiate `Prisma` based on concrete service
t prisma = new Prisma({
peDefs: 'schemas/database.graphql',
dpoint: 'https://us1.prisma.sh/demo/my-service/dev'
cret: 'my-super-secret-secret'


etrieve `name` of a specific user
ma.query.user({ where { id: 'abc' } }, '{ name }')

etrieve `id` and `name` of all users
ma.query.users(null, '{ id name }')

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

pdate name of a specific user and retrieve the `id`
ma.mutation.updateUser({ where: { id: 'abc' }, data: { name: 'Sarah' } }, '{ id }')

elete a specific user and retrieve the `id`
ma.mutation.deleteUser({ where: { id: 'abc' } }, '{ id }')

Under the hood, each of these function calls is simply translated into an actual HTTP request against your Prisma service (using graphql-request).

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

sk whether a post exists with `id` equal to `abc` and whose
author` is called `Sarah` (return boolean value)
ma.exists.Post({
: 'abc',
thor: {
name: 'Sarah'


API
constructor(options: PrismaOptions): Prisma

The PrismaOptions type has the following fields:

| Key | Required | Type | Default | Note | | — | — | — | — | — | | typeDefs | Yes | string | - | Type definition string or file path to the schema definition of your Prisma service (typically a file called database.graphql or prisma.graphql) | | endpoint | Yes | string | - | The endpoint of your Prisma service | | secret | Yes | string | - | The secret of your Prisma service | | fragmentReplacements | No | FragmentReplacements | null | A list of GraphQL fragment definitions, specifying fields that are required for the resolver to function correctly | | debug | No | boolean | false | Log all queries/mutations to the console |

query and mutation

query and mutation are public properties on your Prisma instance. They both are of type Query and expose a number of auto-generated delegate resolver functions that are named after the fields on the Query and Mutation types in your Prisma database schema.

Each of these delegate resolvers in essence provides a convenience API for sending queries/mutations to your Prisma service, so you don't have to spell out the full query/mutation from scratch and worry about sending it over HTTP. This is all handled by the delegate resolver function under the hood.

Delegate resolver have the following interface:

s: any, info: GraphQLResolveInfo | string): Promise<T>

The input arguments are used as follows:

The generic type T corresponds to the type of the respective field.

exists

exists also is a public property on your Prisma instance. Similar to query and mutation, it also exposes a number of auto-generated functions. However, it exposes only a single function per type. This function is named according to the root field that allows the retrieval of a single node of that type (e.g. User for a type called User). It takes a where object as an input argument and returns a boolean value indicating whether the condition expressed with where is met.

This function enables you to easily check whether a node of a specific type exists in your Prisma database.

request

The request method lets you send GraphQL queries/mutations to your Prisma service. The functionality is identical to the auto-generated delegate resolves, but the API is more verbose as you need to spell out the full query/mutation. request uses graphql-request under the hood.

Here is an example of how it can be used:

t query = `
ery ($userId: ID!){
user(id: $userId) {
  id
  name
}



t variables = { userId: 'abc' }

ma.request(query, variables)
hen(result => console.log(result))
ample result:
"data": { "user": { "id": "abc", "name": "Sarah" } } }
forwardTo

If you just want to forward a query to the exact same underlying prisma query, you can use forwardTo:

t {forwardTo} = require('prisma-binding')

t resolvers = {
ery: {
posts: forwardTo('db')



t server = new GraphQLServer({
peDefs: './src/schema.graphql',
solvers,
ntext: req => ({
...req,
db: new Prisma({
  typeDefs: 'src/generated/prisma.graphql',
  endpoint: '...',
  secret: 'mysecret123',
}),
debug: true,
,


er.start(
 => console.log(`Server is running on http://localhost:4000`),

Usage
Next steps

Prisma


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.