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
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.
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 database2model User {3 id String @id @default(cuid())4 email String @unique5 password String 6 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 User @relation(fields: [authorId], references: [id])17}
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.
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: String12 }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);
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: String12 }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);
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.
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.
A weekly newsletter all around the GraphQL community & ecosystem
The GraphQL Berlin Meetup has been started in 2016 and is one of the most popular GraphQL Meetups in the world
We've built the popular fullstack GraphQL tutorial website How to GraphQL to help educate developers about GraphQL.
We have multiple channels where you can engage with members of our community as well as the Prisma team.