如何解决由数据类型关系引起的“变量“ $ _v0_data”获得无效值”-突变解析器

时间:2019-07-23 10:51:15

标签: javascript graphql prisma prisma-graphql

我正在尝试建立类型之间的关系,并编写了解析器以运行创建列表值但出现以下错误的突变

这是我的突变文件

  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文件

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 个答案:

答案 0 :(得分:0)

考虑使用以下代码

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会意外导致错误。