Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Extract query creation to generic method #1268

Open
agabidullin opened this issue Aug 2, 2024 · 0 comments
Open

[Question] Extract query creation to generic method #1268

agabidullin opened this issue Aug 2, 2024 · 0 comments

Comments

@agabidullin
Copy link

Hi all!
I'm using pothos and prisma, have some similar crud operations and want to create some generic method for it. But I'm stuck with types.

For example - I have method to querying list.

builder.queryField('groups', (t) =>
  t.prismaField({
    type: ['Group'],
    args: {
      ...createSorterAndFilterArgs(t.arg)
    },
    resolve: async (query, _parent, _args, _ctx, _info) => {
      const { sorter, filter } = _args
      return await prisma.group.findMany({
        ...query,
        orderBy: applySorting(sorter),
        where: {
          name: !!filter?.searchString
            ? { contains: filter.searchString, mode: 'insensitive' }
            : undefined
        }
      })
    }
  })
)

When I'm trying to pass parameter for "type" - there is a trouble with type for "query" param in "resolve" method.

type Options = {
  queryName: string

  type: [keyof PrismaTypes]
}

const common = ({ queryName, type }: Options) => {
  builder.queryField(queryName, (t) =>
    t.prismaField({
      type: type,
      args: {
        ...createSorterAndFilterArgs(t.arg)
      },
      resolve: async (query, _parent, _args, _ctx, _info) => {
        const { sorter, filter } = _args
        return await prisma.group.findMany({
          ...query,
          orderBy: applySorting(sorter),
          where: {
            name: !!filter?.searchString
              ? { contains: filter.searchString, mode: 'insensitive' }
              : undefined
          }
        })
      }
    })
  )
}

common({ queryName: 'groups', type: ['Group'] })
Screenshot 2024-08-02 at 11 59 13

I tried different types to pass as parameter, but cannot resolve this problem. Could you help me with it plz?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant