7. Arguments
3m

Overview

We have our featured listings, and one of them is really catching our eye, so how do we just one specific listing and all its details?

In this lesson, we will:

  • Learn how to access in a connector

Listing details

Here's what the mockup looks like for a listing's details:

Airlock listing details

Mapping this back to our REST API, we'll need to retrieve this data from the GET /listings/:id endpoint, passing a listing ID into the URL. We can examine the response from that endpoint here: https://airlock-listings.demo-api.apollo.dev/listings/listing-1

https://airlock-listings.demo-api.apollo.dev/listings/listing-1

Airlock listing details

REST API response
{
"costPerNight": 120,
"title": "Cave campsite in snowy MoundiiX",
"locationType": "CAMPSITE",
"description": "Enjoy this amazing cave campsite in snow MoundiiX, where you'll be one with the nature and wildlife in this wintery planet. All space survival amenities are available. We have complementary dehydrated wine upon your arrival. Check in between 34:00 and 72:00. The nearest village is 3AU away, so please plan accordingly. Recommended for extreme outdoor adventurers.",
"id": "listing-1",
"numOfBeds": 2,
"closedForBookings": false,
"photoThumbnail": "https://res.cloudinary.com/apollographql/image/upload/v1644350721/odyssey/federation-course2/illustrations/listings-01.png",
"hostId": "user-1",
"isFeatured": true,
"latitude": 1023.4,
"longitude": -203.4,
"amenities": [
{
"id": "am-2",
"category": "Accommodation Details",
"name": "Towel"
},
{
"id": "am-10",
"category": "Space Survival",
"name": "Oxygen"
}
// more amenities
]
}

The listing connector

  1. In the schema, let's find the Query type and add a new called listing, which takes in an id of type non-nullable ID and returns a nullable Listing type.

    listings.graphql
    type Query {
    # ...other fields
    "A specific listing"
    listing(id: ID!): Listing
    }

    Note: If you're not familiar with how to use , you can learn about them at graphql.com/learn/arguments.

  2. Saving our changes, rover dev will now give us that expected (and helpful!) error. We know how to fix that!

    error[E029]: Encountered 1 build error while trying to build a supergraph.
    Caused by:
    QUERY_FIELD_MISSING_CONNECT: [listing] The field `Query.listing` has no `@connect` directive.
  3. Let's add the @connect to the , auto-completing the parameters by hitting Enter.

    listings.graphql
    listing(id: ID!): Listing
    @connect(
    source: ""
    http: { GET: "" }
    selection: """
    """
    )
  4. We'll use the same source as before: listings.

    listings.graphql
    source: "listings"
  5. And we're doing an HTTP GET call to /listings/:id.

    listings.graphql
    http: { GET: "/listings/ID???" }

But what do we put for the id? We can't hard-code an id like we did in the browser. We want to make it dynamic, pulling from the id the client would provide. So how do we get access to the value of that listing id here in the @connect ?

listings.graphql
listing(id: ID!): Listing # ⬅️ Here's the id value we need!
@connect(
source: ""
http: { GET: "/listings/ID???" } # ⬅️ Here's where we need it to end up!
selection: """
"""
)

Accessing GraphQL arguments in connectors

args to the rescue! args gives us access to all of the 's . But we'll need to prefix it with the dollar sign symbol ($), which indicates a in .

Taking this schema as an example:

Example schema
type Query {
spaceCats(name: String!, team: String!, mission: ID!): SpaceCat
}

We can access the 's with $args.name, $args.team and $args.mission.

args syntax

And to interpolate these values into the path in our connector, we'll need to wrap them in curly braces ({ }).

Let's try it out!

Using $args

  1. We have the start of the endpoint with the path /listings/.

    listings.graphql
    http: { GET: "/listings/" }
  2. Then, because we want to access the 's id , we'll use $args.id, making sure to wrap it in curly braces ({}) to include it in the path.

    listings.graphql
    http: { GET: "/listings/{$args.id}" }

    Watch out for the syntax here: the dollar sign ($) is inside the curly braces.

  3. Great! All that's left to do is fill in our selection. The JSON response for this endpoint is actually very similar to the featuredListings connector, a mostly one-to-one mapping, with the exception of the closedForBooking . The mockup also shows a list of amenities that belong to the listing, but we'll tackle that in the next lesson. For now, let's keep going.

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

Checking our work

You know the drill! Save your changes and open up Sandbox.

Here we'll build a new , so open up a new tab, and we'll retrieve a specific listing and all of its .

http://localhost:4000

Variables panel

We can hit the plus button to add all the available in one go.

http://localhost:4000

Variables panel

We'll give this a new name: GetListingDetails.

query GetListingDetails($listingId: ID!) {
listing(id: $listingId) {
id
title
numOfBeds
costPerNight
closedForBooking
}
}

Under the Variables section, we'll add the listingId value.

Variables
{
"listingId": "listing-1"
}
http://localhost:4000

Variables panel

Hit Run and... we can see the details of one specific listing now! Awesome!

http://localhost:4000

GetListingDetails response

Response
{
"data": {
"listing": {
"id": "listing-1",
"title": "Cave campsite in snowy MoundiiX",
"numOfBeds": 2,
"costPerNight": 120,
"closedForBooking": false
}
}
}
Task!

Practice

Answer the following questions using the schema below:

Example schema
type Query {
allListings(limit: Int = 10, offset: Int): [Listing!]!
}
Based on this schema, how would you access the limit argument in the allListings field's connector?

Use the REST API JSON response below to complete the code challenge.

REST API JSON response
{
"id": "px-m012",
"officialName": "Mraza",
"mass": 6.42
}
Code Challenge!

Replace the ??? instances in the http.GET path with the correct values from the field arguments. Refer to the resulting JSON object above to define the selection.

Loading...
Loading progress

Key takeaways

  • To access a 's values from within its connector, we can use $args. This is an object that contains the values of all of the that are passed into a when a is run.
  • To interpolate the value of an as a URL parameter in the path, we can use curly braces. For example: http: { GET: "/listings/{$args.id}"}

Up next

We're on a roll! But we didn't quite finish up our listing details, we're still missing a listing's amenities. Let's tackle that next.

Previous

Share your questions and comments about this lesson

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.