THpubs
THpubs

Reputation: 8162

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?

Upvotes: 2

Views: 343

Answers (1)

Shivam Pandey
Shivam Pandey

Reputation: 3936

You need to make the ShopLocation with input keyword instead of type,

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
}

Upvotes: 4

Related Questions