GraphQL with a Database Has Never Been Easier with Apollo & Prisma

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

What is Prisma?

Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.

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.

Diagram showing Prisma fitting in the Server part of the Client-server-database stack
apollo clientapollo serverdatabase

The 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.

The syntax of the Prisma schema is heavily inspired by GraphQL SDL. If you're already familiar with SDL, picking it up to model your database tables will be a breeze.

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 KempleKurt 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 migrations

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

Get Involved

Join the Apollo + Prisma Community

Join us on Slack

Get help and engage with thousands of Apollo and Prisma developers on Slack

Join Prisma Slack

Showcase on Twitter

We’d love to see what you’re building with Apollo and Prisma

Tweet #prisma+apollo

Get Stickers

Show your Apollo + Prisma love with custom free stickers

Order stickers