paljs / prisma-tools

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

Default fields for nested entities #129

Closed kuksik closed 3 years ago

kuksik commented 3 years ago

Whether it exists common approach how to add default fields to the entities? I have implemented something like:

`

schema.prisma

model User {
    id                                    string                 @id @default(cuid())
   organization                   Organization     @relation(fields: [organizationId], references: [id])
   organizationId                string
}
model Organization {
    id               string            @id @default(cuid())
    user           User
    type           Type             @relation(fields: [typeId], references: [id])
    typeId        String
}
model Type {
    id                          string                 @id @default(cuid())
    descriptionRaw   string
}

`

`

resolvers.ts

Query: {
    types(parent: any, params: any, ctx: Context, info: any) {
        const modifiedSelect = { id:true, descriptionRaw: true }
        const select = new ctx.PrismaSelect(info, { select: modifiedSelect }).value;
        const prisma2Params = ctx.transforParamsToPrisma2(params);
        return ctx.prisma.types.findMany({ ...select, ...prisma2Params });
     },
    organization(parent: any, params: any, ctx: Context, info: any) {
        const modifiedSelect = {
            id: true,
            type: { select: { id: true, descriptionRaw: true } }
         }
        const select = new ctx.PrismaSelect(info, { select: modifiedSelect }).value;
        const prisma2Params = ctx.transforParamsToPrisma2(params);
        return ctx.prisma.types.findMany({ ...select, ...prisma2Params });
     },
     user(parent: any, params: any, ctx: Context, info: any) {
        const modifiedSelect = {
            id: true,
            organization: {
                id: true, 
                type: { select: { id: true, descriptionRaw: true } }
            }
         };
         ...
     }
 },
 Types: {
       description: async (parent: any, params: any, ctx: Context, info: any) => modifyDesc(parent.descriptionRaw);
  }

` It works but looks very mass in case of complex schema. What to do if you need (for some reasons) id field for every entity? Do we need describe "type" select in every resolver? Will be cool have some approach which allow do reduce code and avoid repeats.

AhmedElywa commented 3 years ago

@kuksik, Thanks for your issue. It explains a good idea.

we can make an object have your customization for all models like

{
  User: ['id', 'name'],
  Type: ['id', 'descriptionRaw'],
  Post: ['id', 'body'],
}

and you can pass it to PrismaSelect class, and when every time we need to select this model, we look to this schema and add default fields in your custom object

I will start on this feature now. wait new release

kuksik commented 3 years ago

@AhmedElywa cool! I thought about the same approach. Can i help with something?

AhmedElywa commented 3 years ago

I changed my approach. it will be like

const defaultFields = {
  User: { id: true, name: true },
  Type: { id: true, descriptionRaw: true },
  Post: { id: true, body: true },
}
AhmedElywa commented 3 years ago

Please try version 1.5.0