6. Arguments
2m

Overview

We've got our list of listings, but how can we go about one specific listing? Let's explore how we can use connectors to get one object in particular.

In this lesson, we will:

  • Learn how to access in a connector

Listing details

Onto the next page we need to implement: a listing's details.

Airlock listing details

We'll need to add a new entry point to our schema to retrieve a specific listing. This data will be powered by the listings/:id endpoint from our Listings REST API. We can examine the response from that endpoint here: https://rt-airlock-services-listing.herokuapp.com/listings/listing-1

Connector for a listing

  1. Open up the listings.graphql file and find the Query type.

  2. We'll add a new to Query called listing, which takes in an id of type non-nullable ID and returns a nullable Listing type.

    listings.graphql
    "A specific listing"
    listing(id: ID!): Listing
  3. Saving our changes, rover dev will now give us an expected (and helpful!) error:

    error[E029]: Encountered 1 build error while trying to build a supergraph.
    Caused by:
    QUERY_FIELD_MISSING_CONNECT: The field `Query.listing` has no `@connect` directive.

    We know how to fix that!

  4. Let's add a @connect to the .

    listings.graphql
    listing(id: ID!): Listing
    @connect(
    source: ""
    http: { GET: "" }
    selection: """
    """
    )
  5. Again, we'll first provide source along with the name we set earlier on: v1.

    listings.graphql
    source: "v1"
  6. As we saw in the REST endpoint above, this will be an HTTP GET call.

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

The endpoint we want is listings/:id, where :id is the ID of the listing... but how do we get access to the value of that here in the @connect ?

Accessing GraphQL arguments in connectors

$args to the rescue! $args gives us access to all the for a .

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

Taking the schema above as an example, we could access the 's within the @connect using $args.name, $args.team and $args.mission. And to interpolate it into the HTTP path in our connector, we'll need to wrap it in curly brackets { }.

Let's try it out!

  1. We'll jump back into the listings.graphql file where we left off with our listing(id: ID!) connector.

    listings.graphql
    listing(id: ID!): Listing
    @connect(
    source: "v1"
    http: { GET: "???" }
    selection: """
    """
    )
  2. For the value of GET, we'll start with a string /listings/.

    listings.graphql
    http: { GET: "/listings/" }
  3. Then, because we want to access the id from the , we'll use $args.id. We'll wrap it in curly brackets ({}) to interpolate it with the preceding endpoint string.

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

    Watch out for the syntax here: the $ is inside the curly braces { }.

  4. Great! All that's left to do is fill in our selection. The JSON response for this endpoint is very similar to the featured listings' connector, so we can add the same selection mapping.

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

Check your work

You know the drill! Open up http://localhost:4000 where the local is running.

Let's build an that retrieves a specific listing and all of its .

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

And for the Variables section:

Variables
{
"listingId": "listing-9"
}

You should get data back!

Response
{
"data": {
"listing": {
"id": "listing-9",
"title": "The Nostromo in LV-426",
"numOfBeds": 4,
"costPerNight": 474,
"closed": false
}
}
}

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?

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! Next up on the implementation list: a listing's amenities.

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.