Building a Credit System w/ Prisma (Pt. 2)

Abhi Aiyer
Open GraphQL
Published in
3 min readMay 26, 2018

Alright! Back to it! In the last chapter we scaffolded the beginnings of our credit service with the hope we can facilitate in app purchases!

If you want to get caught up on this series, checkout Part 1 here.

Now, with our data access layer generated for us from the datamodel created in the last part, I want to create what I like to call the Service layer that holds the business logic of our service.

Why create this layer? A couple reasons

  • Clients of this service should be consuming APIs within the domain it serves. You don’t want these clients having to understand all the CRUD magic when working on the product.
  • We want to add more layers: permissions, authentication, etc in front of our data access for security.
  • I don’t want people to know the secret sauce of our database. Well, except you all reading this.

Just like the last post, I’m going to write code and write this post at the same time!

Application Server

Alright let’s move the 2 generated files to a prisma folder.

Cool, now I need to make a GraphQL server for this tier of the application. I’m going to use graphql-yoga a thin wrapper around apollo-server. We’re all about not doing a lot of work around here.

Let’s make bootstrap the project with yarn

$ yarn init -y for those that don’t know, if you pass -y to init, it just says yes to everything. See once again, laziness FTW.

$ yarn add graphql-yoga

Now I need to do some boilerplatey things:

$ yarn add babel-preset-env -D

Make a .babelrc

{
"presets": ["env"]
}

Let’s add a start script:

"scripts": {
"start": "babel-node ./src"
}

Alright, let’s make a src file and drop an index.js file in there. I’ll make a .gitignore file too, I hate dealing accidentally adding random files when I start new projects.

Let’s do a little Hello World action:

I stole this from: https://github.com/prismagraphql/graphql-yoga

Okay, so I need to be able to use my data access types in the service layer. There’s a lot of cool utilities to download that schema and incorporate it into our app.

$ yarn global add graphql-cli

This CLI tool is an awesome way to generate and integrate schemas into your project. This along with a .graphqlconfig.yml is a great way to make our dev environment running nice and smooth.

So in my .graphqlconfig.yml:

Our service schema will be located in the src directory under service-schema.graphql. Our prisma config will be sourced from the prisma folder we created earlier.

Let’s use the CLI to get our data access schema

$ graphql get-schema --project prisma

If you take a look at the prisma.graphql you’ll see so many types we can now incorporate into our service layer!

And because I’m super lazy, our prisma.yml file which manages our deployments will run this generation command for us everytime we run prisma deploy

My yml looks like this now:

Credit System Hello World

Let’s make our first api for the service layer:

type Query {
getBalance: Int
}

This will get the current user’s balance!

Now let’s make a resolver that returns a fake balance, I’m going to make a resolvers folder and in the index.js file:

And our server file now looks like:

Great, let’s start the server:

$ yarn start

Woo hoo. Making some progress. Okay break time, see you in the next part.

--

--