5. Exercise 1: Featured listings
5m

Exercise 1: Featured listings ( 5 min)

Open up Sandbox at http://localhost:4000 where the local is running.

When you run the below, you'll get an error!

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

Exercise #1 Goal

🎯 Retrieve data for all of the fields under the Listing type when for featuredListings.

https://airlock-listings.demo-api.apollo.dev/featured-listings

After successfully completing the exercise, you should get data back when running the GetFeaturedListings again.

Check your work

Answer the following questions after you've completed the exercise. Lost? Check out the hints and solution below.

Is the 'Cozy yurt in Mraza' closed?
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.)

💭 Hints

Tips:

  • Take it one step at a time! Comment out all under the Listing type except for id. Let's start with the bare minimum.
  • Use the Connectors Debugger panel to examine the response object that comes back.
  • Use the products.graphql schema file as a reference.

Solution

listings.graphql
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@key"])
@link(
url: "https://specs.apollo.dev/connect/v0.2"
import: ["@connect", "@source"]
)
@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
closed: closedForBookings
"""
)
}
Previous