简体   繁体   English

Prisma API返回关系但客户端返回“不能为非可空字段返回null”。

[英]Prisma API returns relation but client returns “cannot return null for non-nullable field..”

When I try to return fields from a one-to-many relation in Prisma client playground it returns the following error: 当我尝试在Prisma客户端操场中从一对多关系返回字段时,它返回以下错误:

Cannot return null for non-nullable field DeviceConfig.device. 对于不可为空的字段DeviceConfig.device,不能返回null。

What in my resolver or client could be causing this? 我的解析器或客户端可能导致什么?

When running the following query on the backend Prisma API playground it does return the correct data so that tells me my mutations and relationship is good. 在后端Prisma API操场上运行以下查询时,它会返回正确的数据,以便告诉我我的突变和关系是好的。

Datamodel 数据模型

type Device {
  ...
  model: String! @unique
  ...
  configs: [DeviceConfig] @relation(name: "DeviceConfigs", onDelete: CASCADE)
}

type DeviceConfig {
  id: ID! @unique
  device: Device! @relation(name: "DeviceConfigs", onDelete: SET_NULL)
  name: String!
  ...
}

Resolver 分解器

deviceConfig: async (parent, { id }, context, info) => context.prisma.deviceConfig({ id }, info)

Query 询问

{
  deviceConfig(id:"cjqigyian00ef0d206tg116k5"){
    name
    id
    device{
      model
    }
  }
}

Result 结果

{
  "data": null,
  "errors": [
    {
      "message": "Cannot return null for non-nullable field DeviceConfig.device.",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "path": [
        "deviceConfig",
        "device"
      ]
    }
  ]
}

I expect the query to return the model of the device like the backend Prisma API server does Query 我希望查询返回设备的模型,就像后端Prisma API服务器执行查询一样

{
  deviceConfig(where:{id:"cjqigyian00ef0d206tg116k5"}){
    name
    id
    device{
      id
      model
    }
  }
}

Result 结果

{
  "data": {
    "deviceConfig": {
      "name": "Standard",
      "id": "cjqigyian00ef0d206tg116k5",
      "device": {
        "id": "cjqigxzs600e60d20sdw38x7p",
        "model": "7530"
      }
    }
  }
}

I think you are mixing Prisma Bindings syntax with Prisma Client syntax. 我认为您正在将Prisma Bindings语法与Prisma Client语法混合使用。

The info object is something you pass to the bindings to return what the user is asking for. info对象是您传递给绑定以返回用户要求的内容的对象。 However, this feature is not available in the Prisma Client, which you seem to be using. 但是,您似乎正在使用的Prisma客户端中不提供此功能。 If you need that feature then you could try Prisma Bindings. 如果您需要该功能,那么您可以尝试Prisma Bindings。

Otherwise, modify your code to something like context.prisma.deviceConfig({ id }).device() . 否则,将代码修改为context.prisma.deviceConfig({ id }).device() I think it can also accept a fragment context.prisma.deviceConfig({ id }).$fragment('fragment configWithDevice on DeviceConfig { id name device { id model } }') . 我认为它也可以接受片段context.prisma.deviceConfig({ id }).$fragment('fragment configWithDevice on DeviceConfig { id name device { id model } }')

This forum post helped me understand some caveat around Prisma client when it comes to resolvers. 这个论坛帖子帮助我理解了Prisma客户端在解析器方面的一些警告。 Help Understanding Prisma Client's Value Proposition 帮助理解Prisma客户的价值主张

In my case I was missing the following revolvers because I thought they would be implied based on the schema relationship. 在我的情况下,我错过了以下左轮手枪,因为我认为它们将基于模式关系隐含。

const resolvers = {
  // Relationship resolvers
  Device: {
    configs: (parent, args, context) => context.prisma.device({ id: parent.id }).configs(),
  },
  DeviceConfig: {
    device: (parent, args, context) => context.prisma.deviceConfig({ id: parent.id }).device(),
  },
  Query: {
    ...User.Query,
    ...Device.Query,
    ...DeviceConfig.Query,
  },
  Mutation: {
    ...User.Mutation,
    ...Device.Mutation,
    ...DeviceConfig.Mutation,
  },
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQL 查询返回错误“不能为不可为空的字段返回空值” - GraphQL query returns error "Cannot return null for non-nullable field" 无法为 GraphQL 查询中的不可空字段返回 null - Cannot return null for non-nullable field in a GraphQL query graphql 解析器返回 无法从 nodejs 中的异步 function 为非空字段返回 null - graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs 为什么在执行更改时会收到“无法为不可为空的字段返回 null”错误? - Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation? 为什么这会抛出“不能为不可为空的字段 Query.hello 返回 null。”? - Why does this throw "Cannot return null for non-nullable field Query.hello."? GraphQL 错误:无法为不可为空的字段 Mutation.deleteComment 返回 null - GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment 为什么 graphql 返回“不能返回 null of non-nullable field mutation.createAccnt” - why is graphql returning “cannot return null of non-nullable field mutation.createAccnt” 在 graphql 中更新/编辑用户数据(不能为不可空字段 Mutation.updateUser 返回 null) - Updating/editing user data in graphql(Cannot return null for non-nullable field Mutation.updateUser) 在添加新架构字段后,aws cdk nextjs graphql 突变无法为不可空类型返回 null - aws cdk nextjs graphql mutation cannot return null for non-nullable type after adding in new schema field 让TS抱怨与非空字段的空比较? - Getting TS to complain about null comparison to a non-nullable field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM