Prisma

Prisma turns your database into a GraphQL API and enables the use of GraphQL as a universal query language for all databases. Instead of writing SQL or using a NoSQL API, you can query your database with GraphQL. In this chapter we won’t go into details about Prisma, so head over to their website and have a look what features are available.

Notice In this article, you’ll learn how to integrate Prisma into the Nest framework. We assume that you are already familiar with the GraphQL concepts and the @nestjs/graphql module.

Dependencies

Firstly, we need to install the required packages:

  1. $ npm install --save prisma-binding

Setup Prisma

While working with Prisma you can either host your own instance or use the Prisma Cloud. In this introduction we are going to use the demo server provided by Prisma.

  1. Install the Prisma CLI npm install -g prisma
  2. Create a new service prisma init, choose the demo server and follow the instructions
  3. Deploy your service prisma deploy

If you find yourself in trouble jump over to their Quick Start section for further details. Eventually you should see two new files in your project directory, prisma.yaml configuration file:

  1. endpoint: https://us1.prisma.sh/nest-f6ec12/prisma/dev
  2. datamodel: datamodel.graphql

and automatically created data model, datamodel.graphql.

  1. type User {
  2. id: ID! @unique
  3. name: String!
  4. }

Notice In the real-world applications you will create more complex data models. For more information about data modeling in Prisma click here.

By typing prisma playground you can open the Prisma GraphQL playground.

Create the client

There are a couple of ways to integrate a GraphQL API. We are going to use GraphQL CLI, a command line tool for common GraphQL development workflows. To install the GraphQL CLI use the following command:

  1. $ npm install -g graphql-cli

Next, create your .graphqlconfig in the root directory of the your Nest application:

  1. $ touch .graphqlconfig.yml

Put the following content into it:

  1. projects:
  2. database:
  3. schemaPath: src/prisma/prisma-types.graphql
  4. extensions:
  5. endpoints:
  6. default: https://us1.prisma.sh/nest-f6ec12/prisma/dev
  7. codegen:
  8. - generator: prisma-binding
  9. language: typescript
  10. output:
  11. binding: src/prisma/prisma.binding.ts

To download your Prisma GraphQL schema to prisma/prisma-types.graphql and create your Prisma client under prisma/prisma.binding.graphql, run the following commands in your terminal:

  1. $ graphql get-schema --project database
  2. $ graphql codegen --project database

Integration

Almost done. Now, let’s create a module for our Prisma integration.

prisma.service.ts

  1. import { Injectable } from '@nestjs/common';
  2. import { Prisma } from './prisma.binding';
  3. @Injectable()
  4. export class PrismaService extends Prisma {
  5. constructor() {
  6. super({
  7. endpoint: 'https://us1.prisma.sh/nest-f6ec12/prisma/dev',
  8. debug: false,
  9. });
  10. }
  11. }

Once PrismaService is ready, we need to create a corresponding module.

prisma.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { PrismaService } from './prisma.service';
  3. @Module({
  4. providers: [PrismaService],
  5. exports: [PrismaService],
  6. })
  7. export class PrismaModule {}

Hint To create new modules and services in no time we can make use of the Nest CLI. To create a PrismaModule type nest g module prisma and for the service nest g service prisma

Usage

To use your new service we are going to import the PrismaModule and inject the PrismaService into UsersResolver.

users.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { UsersResolver } from './users.resolver';
  3. import { PrismaModule } from '../prisma/prisma.module';
  4. @Module({
  5. imports: [PrismaModule],
  6. providers: [UsersResolver],
  7. })
  8. export class UsersModule {}

Importing PrismaModule makes exported PrismaService available in the UsersModule context.

users.resolver.ts

  1. import { Query, Resolver, Args, Info } from '@nestjs/graphql';
  2. import { PrismaService } from '../prisma/prisma.service';
  3. import { User } from '../graphql.schema';
  4. @Resolver()
  5. export class UsersResolver {
  6. constructor(private readonly prisma: PrismaService) {}
  7. @Query('users')
  8. async getUsers(@Args() args, @Info() info): Promise<User[]> {
  9. return this.prisma.query.users(args, info);
  10. }
  11. }

Example

A slightly modified example is available here.