Overview
We've got our list of listings, but how can we go about querying 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 GraphQL arguments in a connector
Listing details
Onto the next page we need to implement: a listing's 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
Open up the
listings.graphql
file and find theQuery
type.We'll add a new field to
Query
calledlisting
, which takes in anid
argument of type non-nullableID
and returns a nullableListing
type.listings.graphql"A specific listing"listing(id: ID!): ListingSaving 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!
Let's add a
@connect
directive to the field.listings.graphqllisting(id: ID!): Listing@connect(source: ""http: { GET: "" }selection: """""")Again, we'll first provide
source
along with the name we set earlier on:v1
.listings.graphqlsource: "v1"As we saw in the REST endpoint above, this will be an HTTP
GET
call.listings.graphqlhttp: { GET: "???" }
The endpoint we want is listings/:id
, where :id
is the ID field of the listing... but how do we get access to the value of that GraphQL argument here in the @connect
directive?
Accessing GraphQL arguments in connectors
$args
to the rescue! $args
gives us access to all the GraphQL arguments for a field.
type Query {spaceCats(name: String!, team: String!, mission: ID!): SpaceCat}
Taking the schema above as an example, we could access the field's arguments within the @connect
directive 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!
We'll jump back into the
listings.graphql
file where we left off with ourlisting(id: ID!)
connector.listings.graphqllisting(id: ID!): Listing@connect(source: "v1"http: { GET: "???" }selection: """""")For the value of
GET
, we'll start with a string/listings/
.listings.graphqlhttp: { GET: "/listings/" }Then, because we want to access the
id
argument from the field, we'll use$args.id
. We'll wrap it in curly brackets ({}
) to interpolate it with the preceding endpoint string.listings.graphqlhttp: { GET: "/listings/{$args.id}" }Watch out for the syntax here: the
$
is inside the curly braces{ }
.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.graphqlselection: """idtitlenumOfBedscostPerNightclosed: closedForBookings"""
Check your work
You know the drill! Open up http://localhost:4000 where the local router is running.
Let's build an operation that retrieves a specific listing and all of its fields.
query GetListingDetails($listingId: ID!) {listing(id: $listingId) {idtitlenumOfBedscostPerNightclosed}}
And for the Variables section:
{"listingId": "listing-9"}
You should get data back!
{"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:
type Query {allListings(limit: Int = 10, offset: Int): [Listing!]!}
limit
argument in the allListings
field's connector?Key takeaways
- To access a field's argument values from within its connector, we can use
$args
. This is an object that contains the values of all of the arguments that are passed into a field when a query is run. - To interpolate the value of an argument 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.
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.