Giter VIP home page Giter VIP logo

nexus-prisma's Introduction

nexus-prisma

FeaturesMotivationDocsExamplesGet startedVideo

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. Check out this 15min tutorial video to learn how to get started with nexus-prisma.

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

type Todo {
  id: ID! @id
  title: String!
  done: Boolean! @default(value: false)
}

GraphQL server code (based on graphql-yoga):

import { prismaObjectType, makePrismaSchema } from 'nexus-prisma'
import { idArg } from 'nexus'
import { GraphQLServer } from 'graphql-yoga'
import { prisma } from './generated/prisma-client'
import datamodelInfo from './generated/nexus-prisma'

// Expose the full "Query" building block
const Query = prismaObjectType({ 
  name: 'Query',
   // Expose all generated `Todo`-queries
  definition: t => t.prismaFields(['*'])
})

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

    // Add a custom `markAsDone` mutation
    t.field('markAsDone', {
      type: 'Todo',
      args: { id: idArg() },
      nullable: true,
      resolve: (_, { id }, ctx) => {
        return ctx.prisma.updateTodo({
          where: { id },
          data: { done: true }
        })
      }
    })
  }
})

const schema = makePrismaSchema({
  types: [Query, Mutation],

  prisma: {
    client: prisma,
    datamodelInfo
  },

  outputs: {
    schema: './generated/schema.graphql',
    typegen: './generated/nexus'
  }
})

const server = new GraphQLServer({
  schema,
  context: { prisma }
})
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
  todoes(after: String, before: String, first: Int, last: Int, orderBy: TodoOrderByInput, skip: Int, where: TodoWhereInput): [Todo!]!
  todoesConnection(after: String, before: String, first: Int, last: Int, orderBy: TodoOrderByInput, skip: Int, where: TodoWhereInput): TodoConnection!
}

# 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`, `TodoConnection`, ...

You can find some easy-to-run example projects based on nexus-prisma in the prisma-examples:

  • GraphQL: Simple setup keeping the entire schema in a single file.
  • GraphQL (Apollo Server): Simple setup (using apollo-server) keeping the entire schema in a single file.
  • GraphQL CRUD: Full CRUD operations with minimal boilerplate.
  • GraphQL + Auth: Advanced setup including authentication and authorization and a modularized schema.

You can also check out this quick demo on CodeSandbox:

Edit example

nexus-prisma's People

Contributors

weakky avatar renovate-bot avatar nikolasburk avatar schickling avatar renovate[bot] avatar brannon-darby avatar jangerhofer avatar matart15 avatar t0wer001 avatar devanb avatar pantharshit00 avatar denkristoffer avatar chenkie avatar tgriesser avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.