如何修复“变量"$ _v0_data"由数据类型关系引起的无效值"-Mutation Resolver [英] How to fix 'Variable "$_v0_data" got invalid value' caused from data types relation - Mutation Resolver
问题描述
我正在尝试建立类型之间的关系,并编写了解析器以运行创建列表值但出现以下错误的突变
I am trying to setup relations between types and wrote a resolver to run a mutation that create the list values but getting the below error
这是我的突变文件
async createList(parent, args, ctx, info) {
const list = await ctx.db.mutation.createList(
{
data: {
project: {
connect: {
id: args.projectId
}
},
...args
}
},
info
);
return list;
}
这是我的数据模型
type Board {
id: ID! @id
title: String!
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
lists: [List]!
}
type List {
id: ID! @id
title: String!
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
project: Board!
}
我的模式是
type Mutation {
createList(title: String!, projectId: ID!): List!
}
和生成的pyramida文件
and the generated prisma file
type Mutation {
createList(data: ListCreateInput!): List!
}
input ListCreateInput {
id: ID
title: String!
project: BoardCreateOneWithoutListsInput!
}
我希望此突变能够运行并创建值,但是却出现了此错误
I expected this mutation to run and create the values but got this error instead
Error: Variable "$_v0_data" got invalid value { project: { connect: [Object] }, title: "to do", projectId: "cjyey7947hh6x0b36m231qhbc" }; Field "projectId" is not defined by type ListCreateInput. Did you mean project?
at new CombinedError (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/errors.js:82:28)
at Object.checkResultAndHandleErrors (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/errors.js:98:15)
at CheckResultAndHandleErrors.transformResult (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/CheckResultAndHandleErrors.js:9:25)
at /Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/transforms.js:18:54
at Array.reduce (<anonymous>)
at applyResultTransforms (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/transforms.js:17:23)
at /Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:97:50
at step (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:31:23)
at Object.next (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:12:53)
at fulfilled (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:3:58)
推荐答案
考虑使用以下代码
async function createList(parent, { title, projectId }, ctx, info) {
const list = await ctx.db.mutation.createList(
{
data: {
project: {
connect: {
id: projectId,
},
},
title,
},
},
info,
)
return list
}
出现错误的原因是因为使用了 ... args
,所以 args
中的所有属性都将传递给 data
如下
The reason for getting the error is because ...args
is used, so all the attributes in args
will be passed to data
as follows
data:{
project:{...},
title:'',
projectId:'',
}
ListCreateInput
仅需要 title
和 project
.多余的 projectId
会意外导致错误.
ListCreateInput
only needs title
and project
. The extra projectId
becomes accidentally causing an error.
这篇关于如何修复“变量"$ _v0_data"由数据类型关系引起的无效值"-Mutation Resolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!