GraphQL
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 ORM, you can build GraphQL servers that connect to a database. Prisma ORM is completely agnostic to the GraphQL tools you use. When building a GraphQL server, you can combine Prisma ORM with tools like Apollo Server, GraphQL Yoga, 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) | Purpose | Compatible with Prisma ORM | Prisma integration |
---|---|---|---|
graphql | GraphQL schema (code-first) | Yes | No |
graphql-tools | GraphQL schema (SDL-first) | Yes | No |
type-graphql | GraphQL schema (code-first) | Yes | typegraphql-prisma |
nexus | GraphQL schema (code-first) | Yes | nexus-prisma Early Preview |
apollo-server | HTTP server | Yes | n/a |
express-graphql | HTTP server | Yes | n/a |
fastify-gql | HTTP server | Yes | n/a |
graphql-yoga | HTTP server | Yes | n/a |
In addition to these standalone and single-purpose libraries, there are several projects building integrated application frameworks:
Framework | Stack | Built by | Prisma ORM | Description |
---|---|---|---|---|
Redwood.js | Fullstack | Tom Preston-Werner | Built on top of Prisma ORM | Bringing full-stack to the JAMstack. |
Note: If you notice any GraphQL libraries/frameworks missing from the list, please let us know.
Prisma ORM & GraphQL examples
In the following section will find several ready-to-run examples that showcase how to use Prisma ORM with different combinations of the tools mentioned in the table above.
Example | HTTP Server | GraphQL schema | Description |
---|---|---|---|
GraphQL API (Pothos) | graphql-yoga | pothos | GraphQL server based on graphql-yoga |
GraphQL API (SDL-first) | graphql-yoga | n/a | GraphQL server based on the SDL-first approach |
GraphQL API -- NestJs | @nestjs/apollo | n/a | GraphQL server based on NestJS |
GraphQL API -- NestJs (SDL-first) | @nestjs/apollo | n/a | GraphQL server based on NestJS |
GraphQL API (Nexus) | @apollo/server | nexus | GraphQL server based on @apollo/server |
GraphQL API (TypeGraphQL) | apollo-server | type-graphql | GraphQL server based on the code-first approach of TypeGraphQL |
GraphQL API (Auth) | apollo-server | nexus | GraphQL server with email-password authentication & permissions |
Fullstack app | graphql-yoga | pothos | Fullstack app with Next.js (React), Apollo Client, GraphQL Yoga and Pothos |
GraphQL subscriptions | apollo-server | nexus | GraphQL server implementing realtime GraphQL subscriptions |
GraphQL API -- Hapi | apollo-server-hapi | nexus | GraphQL server based on Hapi |
GraphQL API -- Hapi (SDL-first) | apollo-server-hapi | graphql-tools | GraphQL server based on Hapi |
GraphQL API -- Fastify | fastify & mercurius | n/a | GraphQL server based on Fastify and Mercurius |
GraphQL API -- Fastify (SDL-first) | fastify | Nexus | GraphQL server based on Fastify and Mercurius |
FAQ
What is Prisma ORM's role in a GraphQL server?
No matter which of the above GraphQL tools/libraries you use, Prisma ORM 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 ORM typically reads data from the database to return it in the GraphQL response. In the resolver of a GraphQL mutation, Prisma ORM typically also writes data to the database (e.g. creating new or updating existing records).
Other GraphQL Resources
Prisma curates GraphQL Weekly, a newsletter highlighting resources and updates from the GraphQL community. Subscribe to keep up-to-date with GraphQL articles, videos, tutorials, libraries, and more.