PJPalJS

@paljs/nexus

Introduction

A Nexus plugin that provides Prisma integration with automatic field selection, admin schema generation, and GraphQL scalar types. Bridges Prisma and Nexus GraphQL to create type-safe, efficient GraphQL APIs.

Installation

Peer Dependencies

  • @prisma/client ^6 or ^7
  • graphql ^15 || ^16
  • nexus ^1

Setup

typescript
import { makeSchema } from 'nexus';
import { paljs } from '@paljs/nexus';
 
const schema = makeSchema({
  types: [/* your types */],
  plugins: [
    paljs({
      includeAdmin: true,
      adminSchemaPath: './prisma/schema.prisma',
    }),
  ],
});

Plugin Options

typescript
interface Settings {
  // Include admin queries and mutations
  includeAdmin?: boolean;
 
  // Path to Prisma schema file for admin generation
  adminSchemaPath?: string;
 
  // Scalar types to exclude from generation
  excludeScalar?: string[];
 
  // PrismaSelect configuration
  prismaSelectOptions?: {
    defaultFields?: Record<string, Record<string, boolean>>;
    excludeFields?: Record<string, string[]>;
    dmmf?: Pick<DMMF.Document, 'datamodel'>[];
  };
}

Automatic Field Selection

The plugin adds a select object to your GraphQL context based on the fields requested in the query:

typescript
import { queryType } from 'nexus';
 
export const Query = queryType({
  definition(t) {
    t.list.field('users', {
      type: 'User',
      resolve: async (_, args, { prisma, select }) => {
        // select is automatically generated from the GraphQL query
        return prisma.user.findMany({
          ...args,
          ...select,
        });
      },
    });
  },
});

Prisma 7 — DMMF Changes

In Prisma 7, Prisma.dmmf is no longer exported from the client. If you need DMMF for field validation, use the DMMF generated by @paljs/generator:

typescript
// Before (Prisma 6):
import { Prisma } from '@prisma/client';
paljs({
  prismaSelectOptions: {
    dmmf: [Prisma.dmmf],
  },
});
 
// After (Prisma 7):
import dmmf from './generated/paljs/dmmf';
paljs({
  prismaSelectOptions: {
    dmmf: [dmmf],
  },
});

Without DMMF, PrismaSelect still works for basic field selection but skips field validation. See @paljs/plugins for details.

Built-in Scalar Types

The plugin includes these GraphQL scalar types for Prisma:

  • DateTime
  • Json
  • Decimal
  • BigInt
  • Bytes

Exclude any you define yourself:

typescript
paljs({
  excludeScalar: ['Upload', 'JSON'],
});

Full Example

typescript
import { makeSchema, objectType, queryType, nonNull, intArg } from 'nexus';
import { paljs } from '@paljs/nexus';
 
const User = objectType({
  name: 'User',
  definition(t) {
    t.nonNull.int('id');
    t.nonNull.string('email');
    t.string('name');
    t.list.field('posts', {
      type: 'Post',
      resolve: (parent, _, { prisma, select }) =>
        prisma.user.findUnique({ where: { id: parent.id } }).posts(select),
    });
  },
});
 
const Query = queryType({
  definition(t) {
    t.list.field('users', {
      type: 'User',
      resolve: (_, __, { prisma, select }) =>
        prisma.user.findMany(select),
    });
 
    t.field('user', {
      type: 'User',
      args: { id: nonNull(intArg()) },
      resolve: (_, { id }, { prisma, select }) =>
        prisma.user.findUnique({ where: { id }, ...select }),
    });
  },
});
 
const schema = makeSchema({
  types: [User, Query],
  plugins: [
    paljs({
      includeAdmin: true,
      adminSchemaPath: './prisma/schema.prisma',
    }),
  ],
  outputs: {
    schema: './generated/schema.graphql',
    typegen: './generated/nexus.ts',
  },
});

Using with Generated Types

With @paljs/generator, use the generated Nexus types directly:

typescript
import { makeSchema } from 'nexus';
import { paljs } from '@paljs/nexus';
import * as generatedTypes from './generated/paljs/nexus';
 
const schema = makeSchema({
  types: [generatedTypes],
  plugins: [paljs({ includeAdmin: true })],
});

Command Palette

Search for a command to run...