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

How to extend an arg in query ? #61

Closed victorkardel closed 8 months ago

victorkardel commented 9 months ago

I'm trying to do something like that:

import { QueryFieldBuilder } from '@pothos/core';
import { Prisma } from '@prisma/client';

import { findManyContentQueryObject } from '@glass/taxonomy/modules/graphql/generated/schema/Content';
import * as Inputs from '@glass/taxonomy/modules/graphql/generated/schema/inputs';
import { builder } from '@glass/taxonomy/modules/graphql/schema/builder';
import seedIndexes from '@glass/taxonomy/modules/utils/seedIndexes';

type Types = typeof builder extends PothosSchemaTypes.SchemaBuilder<infer T> ? T : unknown;

interface FindManyContentsOptions {
    t: QueryFieldBuilder<Types, Types['Root']>;
    findManyField: ReturnType<typeof findManyContentQueryObject>;
}

const findManyContentQuery = ({ t, findManyField }: FindManyContentsOptions) =>
    t.prismaField({
        ...findManyField,
        args: {
            ...findManyField.args,
            orderBy: t.arg({
                type: [Inputs.ContentOrderByWithRelationInput | 'String'],
                required: false,
            }),
            seed: t.arg({ type: 'String', required: false }),
        },
        resolve: async (query, root, args, { prisma, ...restContext }, info) => {
            const { take = 1, where, seed, orderBy } = args;
            const random = (orderBy as unknown as string) === 'random';

            if (!random || !seed) {
                return findManyField.resolve(
                    query,
                    root,
                    {
                        ...args,
                        orderBy,
                        take,
                        where,
                    },
                    { prisma, ...restContext },
                    info,
                );
            }

            const allIds = await prisma.content.findMany({
                select: { id: true },
                take: 1000,
            });

            const selectedIds = seedIndexes({
                seed,
                arrSize: take || 1,
                allIds: allIds.map(({ id }) => id),
            });

            const results = await prisma.content.findMany({
                where: {
                    ...(where as Prisma.ContentWhereInput),
                    id: { in: selectedIds },
                },
                take: take || 1,
            });

            return results;
        },
    });

export default findManyContentQuery;

But the types turn into wrong.

Cauen commented 9 months ago

The scope of this library is to generate the default args As you did here:

...findManyField.args

Extra args must follow the Pothos specification: https://pothos-graphql.dev/docs/guide/args

The following line seems to be wrong:

type: [Inputs.ContentOrderByWithRelationInput | 'String'],

You probably need to create a custom input type to handle your case.

victorkardel commented 9 months ago

Thanks! I thought that maybe have an easy way to extend orderBy default args, and only add a new one! But thanks! Will try other way's!