Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nexus-prisma

Package Overview
Dependencies
Maintainers
1
Versions
225
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nexus-prisma

- **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 G

  • 0.0.1-beta.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.2K
decreased by-19.64%
Maintainers
1
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 04 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc