nexus-prisma
Features • Motivation • Docs • Examples • Get started
nexus-prisma
offers a code-first approach for building GraphQL servers with a database. It auto-generates CRUD operations/resolvers that can be exposed and customized in your own GraphQL schema.
Thanks to its unique appoach for constructing GraphQL schemas and generating resolvers, nexus-prisma
removes the need for a traditional ORM or query builder (such as TypeORM, Sequelize, knex.js....).
Features
- No boilerplate: Auto-generated CRUD operations for Prisma models
- Full type-safety: Coherent set of types for GraphQL schema and database
- Customize Prisma models: Easily hide fields or add computed fields
- Best practices: Generated GraphQL schema follows best practices (e.g.
input
types for mutations) - Code-first: Programmatically define your GraphQL schema in JavaScript/TypeScript
- Compatible with GraphQL ecosystem: Works with (
graphql-yoga
, apollo-server
, ...) - Incrementally adoptable: Gradually migrate your app to
nexus-prisma
Motivation
nexus-prisma
provides CRUD building blocks based on the Prisma datamodel. When implementing your GraphQL server, you build upon these building blocks and expose/customize them to your own API needs.
When using nexus-prisma
, you're using a code-first (instead of an SDL-first) approach for GraphQL server development. Read more about the benefits of code-first in this article series:
- The Problems of "Schema-First" GraphQL Server Development
- Introducing GraphQL Nexus: Code-First GraphQL Server Development
- Using GraphQL Nexus with a Database
Documentation
You can find the docs here. They also include a Getting started-section.
Examples
Here's a minimal example for using nexus-prisma
:
Prisma schema:
datasource db {
provider = "sqlite"
url = "file:db/next.db"
default = true
}
generator photon {
provider = "photonjs"
}
generator nexus_prisma {
provider = "nexus-prisma"
}
model Todo {
id ID @id
title String
done Boolean @default(false)
}
GraphQL server code (based on graphql-yoga
):
import { nexusPrismaPlugin } from '@generated/nexus-prisma';
import { Photon } from '@generated/photon';
import { objectType, makeSchema, idArg } from '@prisma/nexus';
import { GraphQLServer } from 'graphql-yoga';
const Query = objectType({
name: 'Query',
definition(t) {
t.crud.todo();
t.crud.todos();
}
});
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.crud.createTodo();
t.field('markAsDone', {
type: 'Todo',
args: { id: idArg() },
nullable: true,
resolve: (_, { id }, ctx) => {
return ctx.photon.todos.updateTodo({
where: { id },
data: { done: true }
});
}
});
}
});
const Todo = objectType({
name: 'Todo',
definition(t) {
t.model.id();
t.model.title();
t.model.done();
}
});
const photon = new Photon();
const nexusPrisma = nexusPrismaPlugin({
photon: ctx => photon
});
const schema = makeSchema({
types: [Query, Mutation, nexusPrisma],
outputs: {
schema: './generated/schema.graphql',
typegen: './generated/nexus'
}
});
const server = new GraphQLServer({
schema,
context: { photon }
});
server.start(() => console.log('Server is running on http://localhost:4000'));
Generated GraphQL schema:
type Query {
todo(where: TodoWhereUniqueInput!): Todo
todos(
after: String
before: String
first: Int
last: Int
skip: Int
): [Todo!]!
}
type Mutation {
createTodo(data: TodoCreateInput!): Todo!
markAsDone(id: ID): Todo
}
type Todo {
done: Boolean!
id: ID!
title: String!
}
-
You can find some easy-to-run example projects based on nexus-prisma
in the photonjs repository
:
- GraphQL: Simple setup keeping the entire schema in a single file.
- GraphQL + Auth: Advanced setup including authentication and authorization and a modularized schema.