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.
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.
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 Postgres is a managed PostgreSQL service built on bare-metal servers and unikernel-based microVMs, designed for high efficiency, strong isolation, and predictable performance, with the ability to easily scale to millions of users.
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.
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/client';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter });
const typeDefs = `
type User { email: String! name: String }
type Query { allUsers: [User!]! }
`;
const resolvers = {
Query: {
allUsers: () => prisma.user.findMany()
}
};
export const schema = makeExecutableSchema({ resolvers, typeDefs });
const app = express();
app.use('/graphql', graphqlHTTP({ schema }));
app.listen(4000);Nexus — Code-First
Nexus is a GraphQL schema library that allows developers to create strongly-typed GraphQL APIs in a code-first fashion. It can be used in TypeScript or plain JavaScript projects. With Nexus, Prisma Client is used inside your resolve functions to read and write data in your database in order to resolve the incoming GraphQL queries and mutations.
Note that you can also use the nexus-plugin-prisma library to auto-generate your GraphQL types and CRUD resolvers from the Prisma schema. This is especially helpful during prototyping and rapid development cycles.
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/client';
import { queryType, objectType, makeSchema } from '@nexus/schema';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter });
const User = objectType({
name: 'User',
definition(t) {
t.string('email');
t.string('name', { nullable: true });
}
});
const Query = queryType({
definition(t) {
t.list.field('allUsers', {
type: 'User',
resolve: () => prisma.user.findMany()
});
}
});
const schema = makeSchema({ types: [User, Query] });
const app = express();
app.use('/graphql', graphqlHTTP({ schema }));
app.listen(4000);TypeGraphQL — Code-First
Similar to Nexus, TypeGraphQL is a code-first GraphQL schema library that allows developers to create strongly-typed GraphQL APIs in TypeScript. With TypeGraphQL, Prisma Client is used inside your @Resolver classes to read and write data in your database in order to resolve the incoming GraphQL queries and mutations.
Note that you can also use the typegraphql-prisma integration to auto-generate your GraphQL types and CRUD resolvers from the Prisma schema. This is especially helpful during prototyping and rapid development cycles.
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/client';
import { ObjectType, Field, Resolver, Query, buildSchemaSync } from 'type-graphql';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter });
@ObjectType()
export class User {
@Field() email: string
@Field((type) => String, { nullable: true }) name?: string | null
}
@Resolver(User)
export class UserResolver {
@Query((returns) => [User], { nullable: true })
async allUsers() { return prisma.user.findMany() }
}
const schema = buildSchemaSync({ resolvers: [UserResolver] });
const app = express();
app.use('/graphql', graphqlHTTP({ schema }));
app.listen(4000);Our GraphQL Resources
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.