Simple Database Access
in GraphQL servers

Query your MySQL or PostgreSQL databases in a GraphQL server using Prisma – a next-generation 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 GraphQL fit together

GraphQL provides a powerful way for web and mobile apps to fetch data from an API. However, as a backend developer, you are still responsible for how your GraphQL server retrieves the requested data from the database by implementing your GraphQL resolvers – that's where Prisma comes in.

Prisma is an ORM that is used inside your GraphQL resolvers to query your database. It works perfectly with all your favorite tools and libraries from the GraphQL ecosystem. You can use with SDL-first and code-first GraphQL schemas and integrate it with any server library such as Apollo Server, Express, NestJS and Mercurius.

GraphQL Diagram showing Prisma fitting in the Server part of the Client-server-database stack

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 GraphQL Use Cases

Prisma can be used in your GraphQL resolvers, no matter whether you're using an SDL-first approach using makeExecutableSchema from graphql-tools or a code-first approach like Nexus or TypeGraphQL.

Note: All examples below are using express-graphql as a GraphQL server, but they also work with any other library like Apollo Server, NestJS or Mercurius.

GraphQL.js (SDL-first)

GraphQL.js — SDL-First

When using the 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 express from 'express';
3import { graphqlHTTP } from 'express-graphql';
4import { makeExecutableSchema } from '@graphql-tools/schema';
5
6const prisma = new PrismaClient();
7
8const typeDefs = `
9 type User {
10 email: String!
11 name: String
12 }
13
14 type Query {
15 allUsers: [User!]!
16 }
17`;
18
19const resolvers = {
20 Query: {
21 allUsers: () => {
22 return prisma.user.findMany();
23 }
24 }
25};
26
27export const schema = makeExecutableSchema({
28 resolvers,
29 typeDefs,
30});
31
32const app = express();
33app.use('/graphql', graphqlHTTP({
34 schema,
35}));
36
37app.listen(4000);
Nexus (code-first)
TypeGraphQL (code-first)

GraphQL.js — SDL-First

When using the 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 express from 'express';
3import { graphqlHTTP } from 'express-graphql';
4import { makeExecutableSchema } from '@graphql-tools/schema';
5
6const prisma = new PrismaClient();
7
8const typeDefs = `
9 type User {
10 email: String!
11 name: String
12 }
13
14 type Query {
15 allUsers: [User!]!
16 }
17`;
18
19const resolvers = {
20 Query: {
21 allUsers: () => {
22 return prisma.user.findMany();
23 }
24 }
25};
26
27export const schema = makeExecutableSchema({
28 resolvers,
29 typeDefs,
30});
31
32const app = express();
33app.use('/graphql', graphqlHTTP({
34 schema,
35}));
36
37app.listen(4000);

Why Prisma and GraphQL?

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.

Timeline from GraphCool to Prisma and Nexus Schema

We ❤️ GraphQL

At Prisma, we love GraphQL and believe in its bright future. Since running Graphcool, a popular GraphQL BaaS, we have have been contributing a lot to the GraphQL ecosystem in the past and still invest into a variety of tools that help developers adopt GraphQL.

We are also proud members of the GraphQL Foundation where we're helping to push the GraphQL community and ecosystem forward.

Our GraphQL Resources

GraphQL Weekly

A weekly newsletter all around the GraphQL community & ecosystem

GraphQL Berlin Meetup

The GraphQL Berlin Meetup has been started in 2016 and is one of the most popular GraphQL Meetups in the world

How to GraphQL

We've built the popular fullstack GraphQL tutorial website How to GraphQL to help educate developers about GraphQL.

Get Involved

Join the GraphQL + Prisma Community

Join us on Slack

Get help and engage with thousands of GraphQL and Prisma develpers on Slack

Join Prisma Slack

Showcase on Twitter

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

Tweet #prisma+graphql

Get Stickers

Show your GraphQL + Prisma love with custom free stickers

Order stickers