Prisma with Node.js - Part 1: Setup

October 22, 2019 0 Comments

Prisma with Node.js - Part 1: Setup

 

 

Prisma is very cool, but it’s still a bit limited on its own. What if need to manipulate the data before sending it to the database, like checking for authentication, or we need to do something with a library specific to Node.js?

In this 2-part series, we’re going to be linking a basic Prisma API to a Node.js server and going over some techniques for accessing our generated queries and resolvers and have more freedom and control over our requests. If you’re not interested in understanding how the setup works, you can copy the boilerplate from this repo.

Prerequisites

Previously we covered how to create a basic Prisma API using Postgres and Heroku. I will be assuming that you already have Docker and a basic Prisma container setup with your database of choice.

Installation

  • graphql-yoga to allow us to setup a Node.js GraphQL server.
  • prisma-bindings will give us access to all the goodies Prisma has in store for us.
  • graphql-cli gives us some utilities for compiling our Prisma resources into something that prisma-bindings can use.
  • nodemon, if you don’t have it installed already, for reloading our server on save.

To keep things inclusive I will be using Prepros for my compiling, but if you would like something a bit cleaner I recommend using Babel.

$ npm install graphql-yoga prisma-bindings graphql-cli 
$ npm install nodemon -g

File Structure

By the end our file structure should look something like the following. Since I’m using Propros instead of Babel there will be some dist files that I’ll be referencing whenever I import or run anything.

* prisma 📂 * generated.graphql * datamodel.graphql * docker-compose.yml * prisma.yml
* src 📂 * index-dist.js * index.js * prisma-dist.js * prisma.js
* .graphqlconfig
* package.json
* schema.graphql

Server Setup

Let’s start with setting up our server in Node.js.

index.js

import { GraphQLServer, PubSub } from 'graphql-yoga' const server = new GraphQLServer({ typeDefs: './schema.graphql', resolvers: {}, 
}) server.start(() => console.log('server running'))

package.json

{ "dependencies": { "graphql-cli": "^3.0.14", "prisma-binding": "2.1.1", "graphql-yoga": "^1.16.7" }, "name": "Prisma Example", "version": "1.0.0", "main": "index.js", "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon --ext js,graphql src/index-dist.js" } 
}

Now it’ll just complain because we don’t have anything in our schema, so let’s just add a User type as a placeholder.

schema.graphql

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

Over at localhost:4000 you should be able to get GraphQL playground started, meanwhile Node.js will be whining about not having any queries, since we haven’t set any up yet, it can just be ignored for now.

Connecting to Prisma

To use Prisma we need a way for our Node server to access what Prisma generates for us. with graphql-cli we can generate a new file that prisma-bindings will be able to work with.

First we need to set up a configuration file that will tell the graphql-cli where to put our generated schema and what endpoint we’re using for our Prisma container. Which should either be your Docker IP Address or 4466.

.graphqlconfig

{ "projects": { "prisma": { "schemaPath": "src/generated.graphql", "extensions": { "endpoints": { "default": "YOURENDPOINT" } } } } 
}

Once we have our configuration setup we can use the graphql-cli to compile our new schema. Let’s make a script to use get-schema on our prisma folder.

package.json

{ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "get-schema": "graphql get-schema -p prisma", "start": "nodemon --ext js,graphql src/index-dist.js" }, 
}

You should now have a new generated.graphql file in your prisma directory.

Now we can use prisma-bindings to connect to our generated schema and our endpoint and give us a variable where we can access all of our queries and resolvers on.

prisma.js

import { Prisma } from 'prisma-binding' const prisma = new Prisma({ typeDefs: 'src/generated/prisma.graphql', endpoint: 'YOURENDPOINT' 
}) export default prisma

index.js

import { GraphQLServer } from 'graphql-yoga' 
import prisma from './prisma-dist' const server = new GraphQLServer({ typeDefs: './schema.graphql', resolvers: {}, context: { prisma }
}) server.start(() => console.log('server running'))

Getting rid of errors

And if all has gone well the terminal should only be yelling at you Error: Query root type must be provided. To get it to stop complaining we can just add an empty schema and resolvers that we pass to our server.

resolvers.js

const Query = {} const Mutation = {} const Subscription = {} module.exports = { Query, Mutation, Subscription 
}

Let’s just comment-out mutations and subscriptions until we’re ready for them.

index.js

import { GraphQLServer } from 'graphql-yoga' 
import prisma from './prisma-dist'
import { Query, Mutation, Subscription } from './resolvers' const server = new GraphQLServer({ typeDefs: './schema.graphql', resolvers: { Query, // Mutation, // Subscription }, context: { prisma }
}) server.start(() => console.log('server running'))

schema.graphql

type Query { users: [User!]! 
} # type Mutation {}
# type Subscription {} type User { id: ID! name: String!
}

Closing Thoughts

This may seem like a lot of unnecessary boilerplate work when compared to how simple setting up a plain GraphQL server was, but I promise that in part 2 you’ll understand how much Prisma will save you in creating any GraphQL backend service.


Tag cloud