Cauen / prisma-generator-pothos-codegen

The fastest way to create a fully customizable CRUD Graphql API from Prisma Schema.
https://www.npmjs.com/package/prisma-generator-pothos-codegen
95 stars 16 forks source link

Change the generated queries and mutations #35

Closed jw closed 1 year ago

jw commented 1 year ago

This is a great tool! Thanks a lot!

Is it possible to rename the generated queries/mutations (like findFirstFoo), into something else (like foo)? Or is there a way to change the queries/mutations after generation? Could anybody give an example?

elritz commented 1 year ago

Not exactly sure what you are looking for in terms of format but here is an example: You get the QueryObject and use the properties on the field prop to complete the query It should be noted that you import the Object from the base file

import { findFirstProfileQueryObject } from "@graphql/__generated__/Profile/queries/findFirst.base";

builder.queryFields((t) => {
  const field = findFirstProfileQueryObject(t);
  return {
    **ChangeTheName**: t.prismaField({
      ...field,
      type: Profile,
      args: {
        ...field.args,
      },
      shield: allow,
      resolve: async (query, root, args, context, info) => {
        const profile = await prismaClient.profile.findFirstOrThrow({
          ...query,
          where: {
            id: {
              equals: args.where.id.equals,
            },
          },
        });
         return {
          ...profile,
        };
      },
    }),
  };
});
Cauen commented 1 year ago

@jw We dont generate queries/mutations by default By optionally running autocrud, it generates everything automatically... But if you want, you can exclude some models from generation and define them with custom names like elritz example

Optionally, you can use replace function to change everything in generated code.

jw commented 1 year ago

Thanks!