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.

tech

What is Prisma?

Prisma makes working with data easy! It offers a type-safe Node.js & TypeScript ORM, global database caching, connection pooling, and real-time database events.

Query
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
Table
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

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. It works perfectly with all your favorite tools and libraries from the GraphQL ecosystem. Learn more about Prisma with GraphQL.

Prisma Schema

The Prisma schema uses Prisma's modeling language to define your database schema. It makes data modeling easy and intuitive, especially when it comes to modeling relations.

You can also supercharge usage of Prisma ORM with our additional tools:
Prisma Accelerate is a global database cache and scalable connection pool that speeds up your database queries.
Prisma Pulse enables you to build reactive, real-time applications in a type-safe manner. Pulse is the perfect companion to implement GraphQL subscriptions or live queries.

1// Define the `User` table in the database
2model User {
3 id String @id @default(cuid())
4 email String @unique
5 password String
6 name String?
7 posts Post[]
8}
9
10// Define the `Post` table in the database
11model Post {
12 id String @id @default(cuid())
13 title String
14 content String?
15 authorId String
16 author User
17}

Prisma and Apollo use cases

Prisma can be used in the GraphQL resolvers of your Apollo Server to implement GraphQL queries and mutations by reading and writing data in your database.

It is compatible with Apollo's native SDL-first approach or a code-first approach as provided by libraries like Nexus or TypeGraphQL.

Apollo Server (SDL-first)

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.

1import { PrismaClient } from '@prisma/client';
2import { ApolloServer } from 'apollo-server'
3
4const prisma = new PrismaClient();
5
6const typeDefs = `
7 type User {
8 email: String!
9 name: String
10 }
11
12 type Query {
13 allUsers: [User!]!
14 }
15`;
16
17const resolvers = {
18 Query: {
19 allUsers: () => {
20 return prisma.user.findMany();
21 }
22 }
23};
24
25const server = new ApolloServer({ resolvers, typeDefs });
26server.listen({ port: 4000 });
Nexus (code-first)
TypeGraphQL (code-first)

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.

1import { PrismaClient } from '@prisma/client';
2import { ApolloServer } from 'apollo-server'
3
4const prisma = new PrismaClient();
5
6const typeDefs = `
7 type User {
8 email: String!
9 name: String
10 }
11
12 type Query {
13 allUsers: [User!]!
14 }
15`;
16
17const resolvers = {
18 Query: {
19 allUsers: () => {
20 return prisma.user.findMany();
21 }
22 }
23};
24
25const server = new ApolloServer({ resolvers, typeDefs });
26server.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 of Apollo

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.

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 SQLite database

Nexus starter kit

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