Overview

GraphQL is a query language for APIs. It is often used as an alternative to RESTful APIs, but can also be used as an additional "gateway" layer on top of existing RESTful services.

With Prisma, you can build GraphQL servers that connect to a database. Prisma is completely agnostic to the GraphQL tools you use. When building as GraphQL server, you can combine Prisma with tools like Apollo Server, express-graphql, TypeGraphQL, GraphQL.js or pretty much any tool or library that you're using in your GraphQL server setup.

GraphQL servers under the hood

A GraphQL server consists of two major components:

  • GraphQL schema (type definitions + resolvers)
  • HTTP server

Note that a GraphQL schema can be written code-first or SDL-first. Check out this article to learn more about these two approaches. If you like the SDL-first approach but still want to make your code type safe, check out GraphQL Code Generator to generate various type definitions based on SDL.

The GraphQL schema and HTTP server are typically handled by separate libraries. Here is an overview of current GraphQL server tools and their purpose:

Library (npm package)PurposeCompatible with PrismaPrisma integration
graphqlGraphQL schema (code-first)YesNo
graphql-toolsGraphQL schema (SDL-first)YesNo
type-graphqlGraphQL schema (code-first)YesIn progress
apollo-serverHTTP serverYesn/a
express-graphqlHTTP serverYesn/a
fastify-gqlHTTP serverYesn/a
graphql-yogaHTTP serverYesn/a

In addition to these standalone and single-purpose libraries, there are several projects building integrated application frameworks:

FrameworkStackBuilt byPrismaDescription
NexusBackend onlyPrisma LabsPrisma is optionalDelightful GraphQL Application Framework
Redwood.jsFullstackTom Preston-WernerBuilt on top of PrismaBringing full-stack to the JAMstack.
BlitzFullstackBrandon BayerBuilt on top of PrismaRails-like framework for monolithic, full-stack React apps — built on Next.js.

Note: If you notice any GraphQL libraries/frameworks missing from the list, please let us know.

Prisma & GraphQL examples

Below you find a number of ready-to-run examples that showcase how to use Prisma with different combination of the tools mentioned in the table above.

TypeScript

DemoHTTP ServerGraphQL schemaDescription
GraphQLgraphql-yoganexusSimple GraphQL server based on graphql-yoga
GraphQL (Apollo Server)apollo-servernexusSimple GraphQL server based on apollo-server
GraphQL (SDL-first)graphql-yogagraphql-toolsSimple GraphQL server based on the SDL-first approach of graphql-tools (Apollo)
GraphQL (Auth)graphql-yoganexusGraphQL server with email-password authentication & permissions

JavaScript (Node.js)

DemoHTTP ServerGraphQL schemaDescription
GraphQLgraphql-yoganexusSimple GraphQL server based on graphql-yoga
GraphQL (Apollo Server)apollo-servernexusSimple GraphQL server based on apollo-server
GraphQL (SDL-first)graphql-yogagraphql-toolsSimple GraphQL server based on the SDL-first approach of graphql-tools (Apollo)
GraphQL (Auth)graphql-yoganexusGraphQL server with email-password authentication & permissions

FAQ

What is Prisma's role in a GraphQL server?

No matter which of the above GraphQL tools/libraries you use, Prisma is used inside your GraphQL resolvers to connect to your database. It has the same role that any other ORM or SQL query builder would have inside your resolvers.

In the resolver of a GraphQL query, Prisma typically reads data from the database to return it in the GraphQL response. In the resolver of a GraphQL mutation, Prisma typically also writes data to the database (e.g. creating new or updating existing records).

Is there a special connection between Prisma and Nexus?

Yes. At Prisma, we love GraphQL and strongly believe in its bright future. While Prisma is compatible with all tools from the GraphQL ecosystem, we want to leverage the amazing things that become possible when Nexus and Prisma are combined and are therefore helping to build it.

As an example, when you use Nexus together with Prisma, Nexus can leverage the information from the Prisma schema and automatically generate queries and mutations for your Prisma models.

The Prisma Labs team has been founded as an independent part of the rest of Prisma's engineering organization to work on open-source tools that are not directly tied to the Prisma database tools. Prisma Labs is currently dedicating most of their time to work on Nexus with the vision of making it a fully-fledged backend framework for building GraphQL servers.

Edit this page on Github