温馨提示:本文翻译自stackoverflow.com,查看原文请点击:typescript - Possibility to only retreive owners data with prisma2 and nexus
graphql prisma prisma-graphql typescript nexus-prisma

typescript - 只可能通过prisma2和nexus来获取所有者的数据

发布于 2020-05-12 01:03:11

发布查询时,我只想检索分配给当前用户(id)的数据。

我检索并设置了userId一个包含以下内容的中间件函数:

const userId = tokenPayload['claims']['uid'];
ctx.userId = userId;

基于userIdi,我需要从这样的模型中过滤数据;

model Address {
  id                   String      @id @default(uuid())
  name                 string
  ...
  user                 User
}

所以我想返回等价于 Addresses.filter(address => address.user.id === ctx.userId)

Addresses其中user.id等于中间件中的那个)

例如这样的东西(这是编成的代码,因此可以正常工作)

plugins: [nexusPrismaPlugin(
    {
      computedInputs: {
      ...
      },
      computedFilters: {
        addresses: ({ ctx }) => ({ where: { user: { id: ctx.userId } } }),
        relations: ({ ctx }) => ({ where: { user: { id: ctx.userId } } }),
        ...
      }
    }
  )],

在其中computedFilters筛选传入查询以匹配where语句中设置的要求的位置因此,使用此设置,它应该仅返回adressesand和/或relationswhere user.id === ctx.userId

我希望我想实现的目标很明确,不必一定是这样,唯一需要保持一致的就是以最高效的方式做出响应。

我希望有人能提供帮助,最近两周我一直在努力解决当前问题。

查看更多

提问者
RMCS
被浏览
5
RMCS 2020-02-25 23:41

我通过编辑生成的解析器解决了该问题;

通过替换

t.crud.addresses({filtering: true, ordering: true});

t.list.field('addresses', {
      type: 'Address',
      resolve: (_parent, _args, ctx) => {
        return ctx.prisma.address.findMany({
          where: {
            user: {
              id: ctx.userId
            }
          }
        })
      }
    });