5. Debugging errors with selection mapping
2m

Overview

Let's continue to build up the Listing type.

In this lesson, we will:

  • Add new to the schema
  • Use the Connectors Debugger panel in the Explorer
  • Learn how to map JSON responses to s

Adding new fields

The Airlock homepage displaying featured listings needs a little more than the id and title.

Airlock featured listings

Let's add three more to the type for the number of beds available, the cost per night and whether or not the listing is currently closed for bookings:

  • numOfBeds of type Int
  • costPerNight of type Float
  • closedForBookings of type Boolean
  1. Open up the listings.graphql schema file and find where the Listing type is defined.

  2. Add the three new along with descriptions.

    listings.graphql
    type Listing {
    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 bookings (on hiatus)"
    closed: Boolean
    }
  3. When we save our changes, rover dev will restart automatically. Uh-oh, what errors do we see in the terminal?

    error[E029]: Encountered 3 build errors while trying to build a supergraph.
    Caused by:
    CONNECTORS_UNRESOLVED_FIELD: No connector resolves field `Listing.numOfBeds`. It must have a `@connect` directive or appear in `@connect(selection:)`.
    CONNECTORS_UNRESOLVED_FIELD: No connector resolves field `Listing.costPerNight`. It must have a `@connect` directive or appear in `@connect(selection:)`.
    CONNECTORS_UNRESOLVED_FIELD: No connector resolves field `Listing.closed`. It must have a `@connect` directive or appear in `@connect(selection:)`.

Let's fix those! The error gives a helpful hint: "It must have a @connect or appear in @connect(selection:)".

The needs to know where these new are coming from. Do we need to make another network call to retrieve a listing's number of beds? Or is that data available in the response from the connector connected to the featuredListings ? Let's have a look!

Adding to the selection

We can always check the JSON response for the /featured-listings endpoint but for now let's go ahead and add those three to our selection.

  1. Jumping back to the listings.graphql schema file, find the Query.featuredListings .

    listings.graphql
    type Query {
    "A curated array of listings to feature on the homepage"
    featuredListings: [Listing!]!
    @connect(
    source: "v1"
    http: { GET: "/featured-listings" }
    selection: """
    id
    title
    """
    )
    }
  2. In the selection, we'll add the three new : numOfBeds, costPerNight, and closed.

    listings.graphql
    selection: """
    id
    title
    numOfBeds
    costPerNight
    closed
    """

Checking our work

Let's save our changes and jump over to http://localhost:4000 where our local is running.

We can add the new to our existing GetFeaturedListings .

query GetFeaturedListings {
featuredListings {
id
title
numOfBeds
costPerNight
closed
}
}

And we get data back!

{
"data": {
"featuredListings": [
{
"id": "listing-1",
"title": "Cave campsite in snowy MoundiiX",
"costPerNight": 120,
"numOfBeds": 2,
"closed": null
},
{
"id": "listing-2",
"title": "Cozy yurt in Mraza",
"costPerNight": 592,
"numOfBeds": 1,
"closed": null
},
{
"id": "listing-3",
"title": "Repurposed mid century aircraft in Kessail",
"costPerNight": 313,
"numOfBeds": 5,
"closed": null
}
]
}
}

Looking more closely though, closed seems to be returning null for all the listings. null is a perfectly valid value since the is nullable... but let's dig into the errors!

Debugging mapping errors

To find out more about what's happening with each network call under the hood and debug any connector errors, we can use Explorer's Connectors Debugger.

  1. Click on the arrow beside Response and select Connectors Debugger.

    studio.apollographql.com/sandbox

    Connectors Debugger

  2. We can see that our network request call to featured-listings has one mapping error. Let's click on it to see more details.

    studio.apollographql.com/sandbox

    Connectors Debugger

  3. This opens the Mapping tab for the specific request we made, which shows more details like the error message: "Property .closed not found in object".

    studio.apollographql.com/sandbox

    Connectors Debugger

  4. Let's click over to the Response body tab and examine the JSON response.

    studio.apollographql.com/sandbox

    Connectors Debugger

Hmm, that's right, we can't find any JSON property called closed! However, we do see closedForBookings, which is the property we want.

Mapping JSON properties to GraphQL fields

When a JSON property doesn't quite match what we want to name it in our , we'll need to take one extra step to map it.

The syntax looks like this:

selection: """
fieldName: jsonName
"""

The value we set as fieldName is how we've defined the in our schema. It's followed by a colon (:) then the name of the corresponding in the JSON response.

  1. Jumping back to our schema file, we can update our selection with the closed mapped to the closedForBookings property.

    listings.graphql
    selection: """
    id
    title
    numOfBeds
    costPerNight
    closed: closedForBookings
    """

Checking our work - part two!

Let's save our changes again and jump over to http://localhost:4000.

Re-run the GetFeaturedListings ...

query GetFeaturedListings {
featuredListings {
id
title
numOfBeds
costPerNight
closed
}
}

And we get some true and false values for closed! 👏

{
"data": {
"featuredListings": [
{
"id": "listing-1",
"title": "Cave campsite in snowy MoundiiX",
"costPerNight": 120,
"numOfBeds": 2,
"closed": false
},
{
"id": "listing-2",
"title": "Cozy yurt in Mraza",
"costPerNight": 592,
"numOfBeds": 1,
"closed": true
},
{
"id": "listing-3",
"title": "Repurposed mid century aircraft in Kessail",
"costPerNight": 313,
"numOfBeds": 5,
"closed": false
}
]
}
}

Practice

What happens when you include a field in the selection that is NOT defined in the schema? (For example, try adding latitude to the selection.)
Which tab in the Connectors Debugger panel shows you errors if there are discrepancies in your selection and your schema?

Key takeaways

  • To troubleshoot why our are not populating the right values from the REST API, we can use the Connectors Debugger. This tool lets us take a deeper look at each network call to see where any errors might have occurred.
  • When there's a name mismatch between our name and JSON property, we can use the selection parameter to map them manually. To do this, we use the syntax fieldName: jsonName.

Up next

We've got featured listings running, now it's time to dig into a listing's details!

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.