【问题标题】:Possibility to only retreive owners data with prisma2 and nexus仅使用 prisma 和 nexus 检索所有者数据的可能性
【发布时间】:2020-06-06 14:01:41
【问题描述】:

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

我使用包含的中间件函数检索并设置userId

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

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

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

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

Addressesuser.id 等于中间件中的一组)

例如这样的事情(这是编造的代码,所以它确实有效)

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

computedFilters 过滤传入查询以匹配where 语句中设置的要求。 所以有了这个集合,它应该只返回adresses和/或relations,其中user.id === ctx.userId

我希望很清楚我想要实现的目标,它不必是这样的,唯一需要相同的是以最高效的方式响应。

我希望有人能提供帮助,过去几周我一直在努力解决当前的问题..

【问题讨论】:

标签: typescript graphql prisma prisma-graphql nexus-prisma


【解决方案1】:

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

通过替换

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
            }
          }
        })
      }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2021-08-08
    • 2021-08-30
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多