Ger*_*tVK 5 javascript graphql prisma
当我尝试从 Prisma 客户端操场中的一对多关系返回字段时,它返回以下错误:
无法为不可为 null 的字段 DeviceConfig.device 返回 null。
我的解析器或客户端中的什么可能导致这种情况?
在后端 Prisma API playground 上运行以下查询时,它确实返回了正确的数据,以便告诉我我的突变和关系良好。
数据模型
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!
...
}
Run Code Online (Sandbox Code Playgroud)
解析器
deviceConfig: async (parent, { id }, context, info) => context.prisma.deviceConfig({ id }, info)
Run Code Online (Sandbox Code Playgroud)
询问
{
deviceConfig(id:"cjqigyian00ef0d206tg116k5"){
name
id
device{
model
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
{
"data": null,
"errors": [
{
"message": "Cannot return null for non-nullable field DeviceConfig.device.",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"deviceConfig",
"device"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我希望查询返回设备的模型,就像后端 Prisma API 服务器所做的 Query
{
deviceConfig(where:{id:"cjqigyian00ef0d206tg116k5"}){
name
id
device{
id
model
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
{
"data": {
"deviceConfig": {
"name": "Standard",
"id": "cjqigyian00ef0d206tg116k5",
"device": {
"id": "cjqigxzs600e60d20sdw38x7p",
"model": "7530"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这篇论坛帖子帮助我了解了有关解析器时有关 Prisma 客户端的一些注意事项。帮助理解 Prisma 客户的价值主张
在我的情况下,我错过了以下左轮手枪,因为我认为它们会根据模式关系隐含。
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,
},
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1847 次 |
| 最近记录: |