Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用GraphQL在嵌套对象上创建变异?_Node.js_Mongodb_Graphql - Fatal编程技术网

Node.js 如何使用GraphQL在嵌套对象上创建变异?

Node.js 如何使用GraphQL在嵌套对象上创建变异?,node.js,mongodb,graphql,Node.js,Mongodb,Graphql,我试图创建一个graphql变体,用一个其他对象数组更新一个对象字段。这是我的模式: type Guide { _id: ID! first_name: String! last_name: String email: String! phone: String! creator: User! } input GuideInput { _id: ID! first_name:

我试图创建一个graphql变体,用一个其他对象数组更新一个对象字段。这是我的模式:

    type Guide {
      _id: ID!
      first_name: String!
      last_name: String
      email: String!
      phone: String!
      creator: User!
    }

    input GuideInput {
      _id: ID!
      first_name: String!
      last_name: String
      email: String!
      phone: String!
    }

    type Trip {
      _id: ID!
      name: String!
      description: String
      location: String
      start_date: String
      start_time: String
      duration: Int
      creator: User!
      guides: [Guide!]
      guests: [Guest!]
    }

    input TripInput {
      name: String
      description: String
      location: String
      start_date: String
      start_time: String
      duration: Int
      guides: [GuideInput]
    }

    type RootQuery {
      trips: [Trip!]
      guides: [Guide!]
    }

    type RootMutation {
      updateTrip(tripId: ID!, tripInput: TripInput): Trip
      deleteTrip(tripId: ID!): Trip
      createGuide(guideInput: GuideInput): Guide
      deleteGuide(guideId: ID!): Guide
    }

    schema {
      query: RootQuery
      mutation: RootMutation
    }
我的查询如下所示:

const requestBody = {
      query: `
        mutation {
          updateTrip(
            tripId: "${tripId}",
            tripInput: {
              guides: ${guides}
            }
          ) {
            guides {
              first_name
              last_name
            }
          }
        }
      `
    }
执行此请求时出现的错误是:

Expected type GuideInput, found object.
Expected type GuideInput, found Object.


我将一组对象传递到与GuideInput对象形状相同的突变中,因此我被难住了。提前谢谢你

您不能以这种方式将输入传递到查询中。将模板文字与占位符一起使用时,占位符(
${guides}
)内表达式的结果将被视为字符串。如果
guides
是一个对象(如果它是一个数组,那么它就调用了
toString()
,这将导致字符串
[Object Object]
。最终得到的字符串如下所示:

tripInput: {
  guides: [object Object]
}
在查询中替换值的正确方法是使用变量,避免完全使用占位符。您的
requestBody
将如下所示:

const requestBody={
查询:`
突变SomeMutationName($tripId:ID!,$guides:[GuideInput]){
更新(
tripId:$tripId
tripInput:{
指南:$guides
}
) {
向导{
名字
姓
}
}
}
`,
变量:{
特里皮德,
向导,
},
}

有关如何使用变量的更多详细信息,请参阅和。

我还强烈建议使用类似于的客户端,而不是自己将请求放在一起。非常棒-非常有魅力。在使用像Apollo这样的东西之前,我试图对GraphQL的工作原理有一个明确的了解。谢谢你的帮助,丹尼尔!