paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
680 stars 55 forks source link

PrismaSelect - better typescript support #260

Open skworden opened 2 years ago

skworden commented 2 years ago

I'd like a way to pass our own defaultOptions type for PrismaSelect defaultOptions.

The select function is any, and the object keys are generic string.

For reference, here's an example that is fully typed for nexus autogenerated types. Nexus autogenerated types are dynamic nested objects.

import { NexusGenFieldTypes } from "/my/nexus-types.generated";

// Can be the current default option
type AllowedNexusTypes = Partial<
  Omit<NexusGenFieldTypes, "Mutation" | "Query">
>;

type DefaultFields<T = AllowedNexusTypes> = Required<{
  [K in keyof T]:
    | Partial<{ [P in keyof Required<T>[K]]: boolean }>
    | ((select: Required<T>[K]) => Record<string, boolean>);
}>;

const defaultFields: TDefaultFields = {
  Category: { id: true }, // key and options are typed
  Page: (select) => ({ // key, options, and select are typed
    id: true,
    categoryId: !!select.category,
  }),
  Tag: { id: true },
}