kafein / nexus-prisma

GraphQL ORM + CRUD schema generator for database-backed GraphQL servers

Home Page:https://nexus.js.org/docs/database-access-with-prisma

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nexus-prisma

FeaturesMotivationDocsExamplesGet 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:

  1. The Problems of "Schema-First" GraphQL Server Development
  2. Introducing GraphQL Nexus: Code-First GraphQL Server Development
  3. 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';

// Expose some CRUD operations
const Query = objectType({
  name: 'Query',
  definition(t) {
    t.crud.todo();
    t.crud.todos();
  }
});

// Customize the "Mutation" building block
const Mutation = objectType({
  name: 'Mutation',
  definition(t) {
    // Expose only the `createTodo` mutation (`updateTodo` and `deleteTodo` not exposed)
    t.crud.createTodo();

    // Add a custom `markAsDone` mutation
    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:

# The fully exposed "Query" building block
type Query {
  todo(where: TodoWhereUniqueInput!): Todo
  todos(
    after: String
    before: String
    first: Int
    last: Int
    skip: Int
  ): [Todo!]!
}

# The customized "Mutation" building block
type Mutation {
  createTodo(data: TodoCreateInput!): Todo!
  markAsDone(id: ID): Todo
}

# The Prisma model
type Todo {
  done: Boolean!
  id: ID!
  title: String!
}

# More of the generated building blocks:
# e.g. `TodoWhereUniqueInput`, `TodoCreateInput`, ...
  • 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.

About

GraphQL ORM + CRUD schema generator for database-backed GraphQL servers

https://nexus.js.org/docs/database-access-with-prisma


Languages

Language:TypeScript 99.9%Language:JavaScript 0.1%