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
100 stars 19 forks source link

Graphql-Shield plugin (Shield is declared here) #18

Closed elritz closed 1 year ago

elritz commented 1 year ago

there should be a way to select a default shield function in the config and a way to pass a different one through the props of calling the object.

And just thinking more agnostic to the rest of plugins there should just be a prop like this for them all

example:

const field = findManyProfileQueryObject(t, { shield: isPublic }); return { findManyProfile: t.prismaField({ ...field, args: { ...field.args, }, shield: isPublic, resolve: async (query, root, args, context, info) => { return field.resolve(query, root, args, context, info); }, }), };

Cauen commented 1 year ago

@elritz

You can run plugins like this:

import { builder } from "../../builder";
import { findFirstOrderQueryObject } from "../../__generated__/Order";

export const findFirstOrderTest = builder.queryFields((t) => {
  const field = findFirstOrderQueryObject(t);
  return {
    findFirstOrderTest: t.prismaField({
      ...field,
      args: {
        testParam: t.arg({ type: "String", required: false }),
      },
      resolve: async (...args) => {
        const [include, root, { testParam }, { response, prisma }, info] = args;
        if (testParam === "123") throw new Error("Invalid");
        return field.resolve(...args);
      },
      authScopes: { // This is a custom plugin from Pothos
        employee: true,
      },
    }),
  };
});

Are you trying to run a plugin for all resolvers generated in crud?

elritz commented 1 year ago

Just to give more scope the shield is actually setup to be required to be on all mutation and queries. . Although what you suggested does work. I fixed my shield to be optional which is fine for now. What i noticed was my prisma client wasnt actually being imported into the generated files and I was using the custom config to do it

This is what i landed on for my config which is working

/** @type {import('prisma-generator-pothos-codegen').Config} */
module.exports = {
  inputs: {
    outputFilePath: "./src/graphql/__generated__/inputs.ts",
    prismaImporter: "import prismaClient from '@pclient';",
    prismaCaller: "prismaClient",
    resolverImports: "import prismaClient from '@pclient';",
  },
  crud: {
    outputDir: "./src/graphql/__generated__/",
    inputsImporter: "import * as Inputs from '@graphql/__generated__/inputs';",
    prismaCaller: "prismaClient",
    resolverImports: "import prismaClient from '@pclient';",
  },
  global: {
    builderImporter: `import builder from '@builder';`,
  },
};
Cauen commented 1 year ago

@elritz

In the new 0.5.1 version you can apply plugins to all crud resolvers

Please, see example

Does this solve your problem? :)

elritz commented 1 year ago

Okay I got to it and it looks good Thank you for your time