Easy, type-safe database access with Prisma & Apollo

Query data from MySQL, PostgreSQL & SQL Server databases in GraphQL with Prisma – a better ORM for JavaScript and TypeScript.

Why Prisma and Apollo?

End-to-end type safety

Get coherent typings for your application, from database to frontend, to boost productivity and avoid errors.

Optimized database queries

Prisma's built-in dataloader ensures optimized and performant database queries, even for N+1 queries.

Type-safe database client

Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.

Intuitive data modeling

Prisma's modeling language is inspired by GraphQL SDL and lets you intuitively describe your database schema.

Easy database migration

Map your Prisma schema to the database so you don't need to write SQL to manage your database schema.

Filters, pagination & ordering

Prisma Client reduces boilerplates by providing convenient APIs for common database features.

How Prisma and Apollo fit together

Apollo provides a great ecosystem for building applications with GraphQL. When building GraphQL APIs with Apollo Server against a database, you need to send database queries inside your GraphQL resolvers – that's where Prisma comes in. Prisma is an ORM that is used inside the GraphQL resolvers of your Apollo Server to query your database.

Apollo Server — SDL-First

When using Apollo's native SDL-first approach for constructing your GraphQL schema, you provide your GraphQL schema definition as a string and a resolver map that implement this definition.

Inside your resolvers, you can use Prisma Client to read and write data in your database in order to resolve the incoming GraphQL queries and mutations.

import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/client';
import { ApolloServer } from 'apollo-server'

const adapter = new PrismaPg({
  connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter });

const typeDefs = `
  type User {
    email: String!
    name: String
  }

  type Query {
    allUsers: [User!]!
  }
`;

const resolvers = {
  Query: {
    allUsers: () => {
      return prisma.user.findMany();
    }
  }
};

const server = new ApolloServer({ resolvers, typeDefs });
server.listen({ port: 4000 });

Nexus — Code-First

Nexus is a GraphQL schema library that allows developers to create strongly-typed GraphQL APIs in a code-first fashion. It can be used in TypeScript or plain JavaScript projects.

With Nexus, Prisma Client is used inside your resolve functions to read and write data in your database in order to resolve the incoming GraphQL queries and mutations.

import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/client';
import {
  queryType,
  objectType,
  makeSchema
} from '@nexus/schema';
import { ApolloServer } from 'apollo-server'

const adapter = new PrismaPg({
  connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter });

const User = objectType({
  name: 'User',
  definition(t) {
    t.string('email');
    t.string('name', { nullable: true });
  }
});

const Query = queryType({
  definition(t) {
    t.list.field('allUsers', {
      type: 'User',
      resolve: () => prisma.user.findMany()
    });
  }
});

const schema = makeSchema({
  types: [User, Query]
});

const server = new ApolloServer({ schema });
server.listen({ port: 4000 });

TypeGraphQL — Code-First

Similar to Nexus, TypeGraphQL is a code-first GraphQL schema library that allows developers to create strongly-typed GraphQL APIs in TypeScript.

With TypeGraphQL, Prisma Client is used inside your resolver classes to read and write data in your database in order to resolve the incoming GraphQL queries and mutations.

import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../generated/client'
import { ObjectType, Field, buildSchemaSync, Resolver, Query } from 'type-graphql'
import { ApolloServer } from 'apollo-server'

const adapter = new PrismaPg({
  connectionString: process.env.DATABASE_URL,
})
const prisma = new PrismaClient({ adapter })

@ObjectType()
export class User {
  @Field()
  email: string

  @Field((type) => String, { nullable: true })
  name?: string | null
}

@Resolver(User)
export class UserResolver {
  @Query((returns) => [User], { nullable: true })
  async allUsers() {
    return prisma.user.findMany()
  }
}

const schema = buildSchemaSync({
  resolvers: [UserResolver],
})
const server = new ApolloServer({ schema });
server.listen({ port: 4000 });

Prisma provides an excellent modeling language for defining your database, as well as a powerful ORM for working with SQL in JavaScript & TypeScript. It's the perfect match to Apollo Server and makes building GraphQL APIs with a database feel delightful.

Kurt Kemple
Kurt Kemple
DevRel Manager
Apollo

Featured Prisma & Apollo examples

How to build and deploy a GraphQL API

A comprehensive tutorial that explains how to build a GraphQL API with Apollo Server and Prisma and deploy it to DigitalOcean's App Platform.

SDL-first starter kit

A ready-to-run example project with an SDL-first schema and a Postgres database

Nexus starter kit

A ready-to-run example project with Nexus (code-first) and a SQLite database