如何修复由数据类型关系引起的“变量“$_v0_data”得到无效值' - Mutation Resolver

Posted

技术标签:

【中文标题】如何修复由数据类型关系引起的“变量“$_v0_data”得到无效值\' - Mutation Resolver【英文标题】:How to fix 'Variable "$_v0_data" got invalid value' caused from data types relation - Mutation Resolver如何修复由数据类型关系引起的“变量“$_v0_data”得到无效值' - Mutation Resolver 【发布时间】:2019-12-01 09:35:33 【问题描述】:

我正在尝试设置类型之间的关系并编写了一个解析器来运行一个创建列表值但得到以下错误的突变

这是我的变异文件

  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!

以及生成的棱镜文件

type Mutation 
  createList(data: ListCreateInput!): List!


input ListCreateInput 
  id: ID
  title: String!
  project: BoardCreateOneWithoutListsInput!


我希望这个突变能够运行并创建值,但得到了这个错误

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)

【问题讨论】:

【参考方案1】:

考虑使用以下代码

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如下

data:
  project:...,
  title:'',
  projectId:'',

ListCreateInput 只需要titleproject。多余的projectId 会意外地导致错误。

【讨论】:

谢谢。你。哇,现在太明显了。我有这个确切的问题,我也在传播 args。我无法弄清楚我一生中缺少什么。

以上是关于如何修复由数据类型关系引起的“变量“$_v0_data”得到无效值' - Mutation Resolver的主要内容,如果未能解决你的问题,请参考以下文章

如何修复由 lambda 事件处理程序引起的 GC 周期?

如何修复由 SpringSecurity 引起的 Forbidden 403 错误?

如何修复由 CSS 动画引起的奇怪色带?

如何修复由 react-redux 中的循环依赖引起的“不变违规”

如何修复由目的地引起的膨胀错误不是此 NavGraph 的直接子级

如何修复由 Caused by: java.lang.NumberFormatException: For input string: "pets" 引起的 Asynctask 错