Nexus

Auto generate CRUD system from your schema.prisma file.

CONTENT

Example Usage

For more information about Prisma look at they Docs

schema.prisma

datasource postgresql {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}
generator client {
  provider = "prisma-client-js"
}
model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
}
model Post {
  id         Int        @id @default(autoincrement())
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
  published  Boolean    @default(false)
  title      String
  author     User?      @relation(fields:  [authorId], references: [id])
  authorId   Int?
}
enum Role {
  USER
  ADMIN
}

After build your schema.prisma file all you need to run

> prisma generate
> pal g

For more information about pal g command configurations click here

Output

Every model will have folder contain 11 files:

NOTE: You can customize all this files and add your logic code inside it just */type.ts will rewrite on it.

  • User model
    • User/mutations/createOne.ts
    • User/mutations/deleteOne.ts
    • User/mutations/updateOne.ts
    • User/mutations/upsertOne.ts
    • User/mutations/deleteMany.ts
    • User/mutations/updateMany.ts
    • User/queries/findCount.ts
    • User/queries/findCount.ts
    • User/queries/findMany.ts
    • User/queries/aggregate.ts
    • User/type.ts
  • Post model
    • Post/mutations/createOne.ts
    • Post/mutations/deleteOne.ts
    • Post/mutations/updateOne.ts
    • Post/mutations/upsertOne.ts
    • Post/mutations/deleteMany.ts
    • Post/mutations/updateMany.ts
    • Post/queries/findCount.ts
    • Post/queries/findMany.ts
    • Post/queries/findOne.ts
    • Post/queries/aggregate.ts
    • Post/type.ts

To understand this code structure please look to Nexus Docs

type.ts

import { objectType } from '@nexus/schema';

export const User = objectType({
  name: 'User',
  definition(t) {
    t.int('id', { nullable: false });
    t.string('email', { nullable: false });
    t.string('name', { nullable: true });
    t.field('posts', {
      nullable: false,
      list: [true],
      type: 'Post',
      args: {
        where: 'PostWhereInput',
        orderBy: 'PostOrderByInput',
        cursor: 'PostWhereUniqueInput',
        take: 'Int',
        skip: 'Int',
      },
      resolve(parent: any) {
        return parent['posts'];
      },
    });
  },
});

queries

    import { queryField, arg } from '@nexus/schema';
    
    export const UserFindOneQuery = queryField('findOneUser', {
      type: 'User',
      nullable: true,
      args: {
        where: arg({
          type: 'UserWhereUniqueInput',
          nullable: false,
        }),
      },
      resolve(_parent, { where }, { prisma, select }) {
        return prisma.user.findOne({
          where,
          ...select,
        });
      },
    });

    mutations

      import { mutationField, arg } from '@nexus/schema';
      
      export const UserCreateOneMutation = mutationField('createOneUser', {
        type: 'User',
        nullable: false,
        args: {
          data: arg({
            type: 'UserCreateInput',
            nullable: false,
          }),
        },
        resolve(_parent, { data }, { prisma, select }) {
          return prisma.user.create({
            data,
            ...select,
          });
        },
      });

      Add paljs plugin

      This plugin adds three parts

      • PrismaSelect plugin to convert info: GraphQLResolveInfo to select object accepted by prisma client and add to context
      • Admin settings queries and mutations getSchema, updateField, updateModel
      • All models inputs type
        • UserWhereInput
        • UserWhereUniqueInput
        • UserOrderByInput
        • UserCreateInput
        • UserUpdateInput
        • UserUpdateManyMutationInput

      for include admin settings queries you need to pass in plugin settings

      paljs({
        // include Prisma Admin schema query and mutations
        includeAdmin: true,
        // by default adminSchemaPath is `prisma/` you can change it
        adminSchemaPath: 'prisma/',
        // take default fields object accepted by PrismaSelect class { [key: string]: { [key: string]: boolean } }
        prismaSelectDefaultFields: { User: { id: true } },
      });

      Install

      Version Downloads/week License

      yarn add @paljs/nexus
      or
      npm i @paljs/nexus
      import { makeSchema } from '@nexus/schema';
      import * as types from './graphql';
      import { paljs } from '@paljs/nexus';
      
      export const schema = makeSchema({
        types,
        plugins: [paljs()],
        outputs: {
          schema: __dirname + '/generated/schema.graphql',
          typegen: __dirname + '/generated/nexus.ts',
        },
        typegenAutoConfig: {
          sources: [
            {
              source: '@prisma/client',
              alias: 'prisma',
            },
            {
              source: require.resolve('./context'),
              alias: 'Context',
            },
          ],
          contextType: 'Context.Context',
        },
      });

      Use

      extendType({
        type: 'Query',
        definition(t) {
          t.field('findOneUser', {
            type: 'User',
            nullable: true,
            args: {
              where: arg({
                type: 'UserWhereUniqueInput',
                nullable: false,
              }),
            },
            resolve(_, { where }, { prisma, select }) {
              return prisma.user.findOne({
                where,
                ...select,
              });
            },
          });
        },
      });