Issue with mutation

I am constantly getting this error - ==> Schema change detected for subgraph: listings
error: Composition failed.
error[E029]: Encountered 1 build error while trying to build a supergraph.

Caused by:
INVALID_BODY: [listings] @connect(http: {body:}) on Mutation.createListing is not valid: nom::error::ErrorKind::Eof: : $args.input.title
numOfBeds: $args.input.numOfBeds
costPerNight: $args.input.costPerNight

I have changed the type Mutation to - type Mutation {
“Create a new listing”
createListing(input: CreateListingInput!): CreateListingResponse @connect(
source: “listings”,
http: {
POST: “/listings?listing[title]={$args.input.title}&listing[numOfBeds]={$args.input.numOfBeds}&listing[costPerNight]={$args.input.costPerNight}”,
headers: [{ name: “Api-Key”, from: “api-key” }]
}
selection: “”"
listing {
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings
description
photoThumbnail
latitude
longitude
amenities {
id
category
name
}
}
“”"
)
}

But I get back this - {
“data”: null,
“errors”: [
{
“message”: “Request failed”,
“path”: [
“createListing”
],
“extensions”: {
“service”: “listings”,
“http”: {
“status”: 400
},
“connector”: {
“coordinate”: “listings:Mutation.createListing@connect[0]”
},
“code”: “CONNECTOR_FETCH”
}
}
]
}

I need your help

Hi, thanks for the question! If I’m reading correctly, you have two problems:

  1. A parsing error on the body mapping. Unfortunately it’s hard to tell what the problem is from the error (something we need to improve!) — can you provide the code that causes this?
  2. When trying to use query parameters, the endpoint is returning a 400 Bad Request. I don’t think this endpoint supports query parameters, so that makes sense. You can confirm this with the debugging UI in Apollo Sandbox.

For #1, you could try this:

body: """
# this skips past the outer { $args: { input: ... } }
$args.input { 
  # to create an object with these properties:
  title
  numOfBeds
  costPerNight
}
"""

Thanks so much for your response, I have added your code, but still getting bad response 400 error

The complete code block - extend schema
@link(url: “https://specs.apollo.dev/federation/v2.10”, import: [“@key”])
@link(url: “Apollo Connectors Directives - Apollo GraphQL Docs”, import: [“@source”,“@connect”])
@source(
name: “listings”
http: { baseURL: “https://airlock-listings.demo-api.apollo.dev” }
)

type Query {
“A curated array of listings to feature on the homepage”
featuredListings: [Listing!]! @connect(
source: “listings”,
http: { GET: “/featured-listings”}
selection: “”"
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings

"""

)
“A listing by its ID”
listing(id: ID!): Listing @connect(
source: “listings”,
http: { GET: “/listings/{$args.id}”}
# The selection is the fields we want to return from the API
selection: “”"
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings
description
photoThumbnail: thumbnail
latitude
longitude
“”"
entity: true
)
}

“A particular intergalactic location available for booking”
type Listing @key(fields: “id”) {
id: ID!
“The listing’s title”
title: String!
“The number of Beds Available”
numOfBeds: Int
“The cost per night”
costPerNight: Float
“Indicates whether listing is closed for booking (on hiatus)”
closedForBooking: Boolean
“The listing’s description”
description: String
“The listing’s thumbnail image URL”
photoThumbnail: String
“Latitude coordinates for the destination”
latitude: Float
“Longitude coordinates for the destination”
longitude: Float
“The Amenities available for this listing”
amenities: [Amenity!]! @connect(
source: “listings”,
http: { GET: “/listings/{$this.id}/amenities”}
# The selection is the fields we want to return from the API
selection: “”"
id
category
name
“”"
)

}

type Amenity {
id: ID!
“The amenity catgory the amenity belongs to”
category: String!
“The amenity name”
name: String!
}

input CreateListingInput {
title: String!
numOfBeds: Int!
costPerNight: Float!

}

type CreateListingResponse {
listing: Listing
}

type Mutation {
“Create a new listing”
createListing(input: CreateListingInput!): CreateListingResponse @connect(
source: “listings”,
http: {
POST: “/listings”,
headers: [
{ name: “Api-Key”, from: “Api-Key” }
],
body: “”"

this skips past the outer { $args: { input: … } }

$args.input {

to create an object with these properties:

title
numOfBeds
costPerNight
}
“”"
}
selection: “”"
listing {
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings
description
photoThumbnail
latitude
longitude
amenities {
id
category
name
}
}
“”"
)
}

Hi! Could you put your code in a code block? It’s difficult to spot the problem. You can use three backticks above and below to wrap it in a block.

Did you try the debugger in Apollo Sandbox? Troubleshooting Connectors - Apollo GraphQL Docs

The server starts and runs and I am able to execute all the query functions. The function I have troubles with is the Mutation. I kept getting the 400 error I specified earlier. The code block is

`extend schema
@link(url: “https://specs.apollo.dev/federation/v2.10”, import: [“@key”])
@link(url: “Apollo Connectors Directives - Apollo GraphQL Docs”, import: [“@source”,“@connect”])
@source(
name: “listings”
http: { baseURL: “https://airlock-listings.demo-api.apollo.dev” }
)

type Query {
“A curated array of listings to feature on the homepage”
featuredListings: [Listing!]! @connect(
source: “listings”,
http: { GET: “/featured-listings”}
selection: “”"
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings

"""

)
“A listing by its ID”
listing(id: ID!): Listing @connect(
source: “listings”,
http: { GET: “/listings/{$args.id}”}
# The selection is the fields we want to return from the API
selection: “”"
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings
description
photoThumbnail: thumbnail
latitude
longitude
“”"
entity: true
)
}

“A particular intergalactic location available for booking”
type Listing @key(fields: “id”) {
id: ID!
“The listing’s title”
title: String!
“The number of Beds Available”
numOfBeds: Int
“The cost per night”
costPerNight: Float
“Indicates whether listing is closed for booking (on hiatus)”
closedForBooking: Boolean
“The listing’s description”
description: String
“The listing’s thumbnail image URL”
photoThumbnail: String
“Latitude coordinates for the destination”
latitude: Float
“Longitude coordinates for the destination”
longitude: Float
“The Amenities available for this listing”
amenities: [Amenity!]! @connect(
source: “listings”,
http: { GET: “/listings/{$this.id}/amenities”}
# The selection is the fields we want to return from the API
selection: “”"
id
category
name
“”"
)

}

type Amenity {
id: ID!
“The amenity catgory the amenity belongs to”
category: String!
“The amenity name”
name: String!
}

input CreateListingInput {
title: String!
numOfBeds: Int!
costPerNight: Float!

}

type CreateListingResponse {
listing: Listing
}

type Mutation {
“Create a new listing”
createListing(input: CreateListingInput!): CreateListingResponse @connect(
source: “listings”,
http: {
POST: “/listings”,
headers: [
{ name: “Api-Key”, from: “Api-Key” }
],
body: “”"

this skips past the outer { $args: { input: … } }

$args.input {

to create an object with these properties:

title
numOfBeds
costPerNight
}
“”"
}
selection: “”"
listing {
id
title
numOfBeds
costPerNight
closedForBooking: closedForBookings
description
photoThumbnail
latitude
longitude
amenities {
id
category
name
}
}
“”"
)
}`

We solved this yesterday through comments in-lesson!

The issue was the connector’s selection mapping. There needs to be a colon after listing (:) to signify we’re defining a top-level listing property, with the value set to an object { }, with all the properties we need.

1 Like

This appears to be a 400 Bad Request error when attempting to send a POST request using the @connect directive. This may be due to a number of reasons, like improper formatting of the body of the request or omitted parameters. In your @connect directive, the URL building with query parameters (listing[title]={$args.input.title}) could be not interpolating the variables correctly, or the body could be not formatted as expected by the endpoint. I would recommend you to check a few things: ensure the API endpoint is accepting query parameters in the URL and not in the body, confirm that all mandatory fields are being sent correctly, and double-check the headers, especially the Api-Key format. You could also log the actual request that is being sent to identify where the failure is occurring. If possible, test the request in isolation (e.g., via Postman) to ensure the API is behaving as expected outside of the GraphQL layer.