Simple Database Access
in GraphQL servers

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 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 ORM comes in. Prisma ORM is used inside of GraphQL resolvers to query a database. It integrates seamlessly with all your favorite tools and libraries from the GraphQL ecosystem.

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.

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 @relation(fields: [authorId], references: [id])
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 Tools (SDL-first)

GraphQL Tools — 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 Tools — 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.