Query data from MySQL, PostgreSQL & SQL Server databases in GraphQL with Prisma – a better ORM for JavaScript and TypeScript.
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.
// Creating a new recordawait prisma.user.create({ firstName: “Alice”, email: “alice@prisma.io”})
id firstName email 1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
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.
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 database2model User {3 id String @id @default(cuid())4 email String @unique5 password String6 name String?7 posts Post[]8}9
10// Define the `Post` table in the database11model Post {12 id String @id @default(cuid())13 title String14 content String?15 authorId String16 author User17}
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.
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: String10 }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 });
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: String10 }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."
Get coherent typings for your application, from database to frontend, to boost productivity and avoid errors.
Prisma's built-in dataloader ensures optimized and performant database queries, even for N+1 queries.
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Prisma's modeling language is inspired by GraphQL SDL and lets you intuitively describe your database schema.
Map your Prisma schema to the database so you don't need to write SQL to manage your database schema.
Prisma Client reduces boilerplates by providing convenient APIs for common database features.
A comprehensive tutorial that explains how to build a GraphQL API with Apollo Server and Prisma and deploy it to DigitalOcean's App Platform.
A ready-to-run example project with an SDL-first schema and a SQLite database
A ready-to-run example project with Nexus (code-first) and a SQLite database
We have multiple channels where you can engage with members of our community as well as the Prisma team.