简体   繁体   English

如何设置Apollo GraphQL服务器接受对象作为突变变量?

[英]How to set the Apollo GraphQL server to accept an object as a variable to a mutation?

Currently, I'm trying to pass an object as a variable to a mutation as shown below: 当前,我正在尝试将对象作为变量传递给突变,如下所示:

type ShopLocation {
  lane1: String
  lane2: String
  city: String
  postalCode: String
  country: String
}

type ShopResponse {
  statusCode: Int
  messageCode: String
  data: String
}

type Mutation {
  createShop(
    name: String
    email: String
    location: ShopLocation
  ): ShopResponse
}

But I get the following error: 但是我收到以下错误:

{
  "error": {
    "errors": [
      {
        "message": "The type of Mutation.createShop(location:) must be Input Type but got: ShopLocation.",
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "Error: The type of Mutation.createShop(location:) must be Input Type but got: ShopLocation.",
              "    at assertValidSchema (.../node_modules/graphql/type/validate.js:71:11)",
              "    at Object.validate (.../node_modules/graphql/validation/validate.js:55:35)",
              "    at Promise.resolve.then (.../node_modules/apollo-server-core/src/runQuery.ts:188:30)",
              "    at <anonymous>",
              "    at process._tickDomainCallback (internal/process/next_tick.js:228:7)"
            ]
          }
        }
      }
    ]
  }
}

Any idea how to properly do this? 任何想法如何正确地做到这一点?

You need to make the ShopLocation with input keyword instead of type , 您需要使用input关键字而不是type来创建ShopLocation

input ShopLocationInput {
  lane1: String
  lane2: String
  city: String
  postalCode: String
  country: String
}

type ShopResponse {
  statusCode: Int
  messageCode: String
  data: String
}

type Mutation {
  createShop(
    name: String
    email: String
    location: ShopLocationInput
  ): ShopResponse
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM