4. The @connect directive
3m

Overview

The Query.featuredListings needs to know what data it should return!

In this lesson, we will:

  • Learn about the @connect syntax
  • Use @connect to retrieve data for a Query

Featured listings

Let's take a step back from our schema and examine the project we're building. Airlock's homepage needs to display an array of featured listings along with a few properties. In this lesson, we'll start small and tackle the id and title first.

Airlock featured listings

This data will be powered by the GET /featured-listings endpoint from our Listings REST API. We can examine the response from that endpoint here: https://rt-airlock-services-listing.herokuapp.com/featured-listings

Now how do we take this data and connect it to our ?

The @connect directive

The @connect describes how to get the data for a from a REST endpoint. We refer to each instance of @connect as a connector.

Every on the Query and Mutation types must have a @connect . (As we saw in our build errors, we need to provide one for featuredListings!)

@connect takes in four parameters:

  • source: A unique identifier for the , mapping back to the name value defined in an @source instance.
  • http: An object that defines the HTTP methods and URL parameters for the .
  • selection: Maps an API's JSON response to s
  • entity: If set to true, the acts as an in . We won't be using this in the course.

Importing the @connect directive

To start, we'll need to let our schema know about the .

  1. At the top of the listings.graphql schema, find the line where we imported the @source from the connectors spec.

    listings.graphql
    @link(
    url: "https://specs.apollo.dev/connect/v0.1"
    import: ["@source"]
    )
  2. Add the @connect to the array of imports.

    listings.graphql
    import: ["@source", "@connect"]

@connect and featured listings

  1. Find the Query.featuredListings in the schema.

    listings.graphql
    type Query {
    "A curated array of listings to feature on the homepage"
    featuredListings: [Listing!]!
    }
  2. Append the @connect to the .

    Again, let's take advantage of the auto-complete the Apollo VS Code extension gives us. Hit Enter and get all the @connect parameters ready to be filled in!

    listings.graphql
    featuredListings: [Listing!]!
    @connect(
    source: ""
    http: { GET: "" }
    selection: """
    """
    )
  3. First, the source parameter. Let's set the value to v1 as we defined in the previous lesson when using @source.

    listings.graphql
    source: "v1"
  4. Next, the http.GET parameter should send a request to the /featured-listings endpoint, as we saw earlier.

    listings.graphql
    http: { GET: "/featured-listings" }

Finally, we just have selection left!

The selection mapping

The selection property contains a mapping for the defined in our schema to the data properties returned from the JSON response of the endpoint.

In our case, our schema needs the Listing.id and Listing.title to be mapped to the JSON response.

  1. Open up the REST API endpoint for /featured-listings: https://rt-airlock-services-listing.herokuapp.com/featured-listings

  2. In the JSON response, we can see the properties for id and title are returned for each item in the array. These are exactly the properties we need! Let's add them to the selection.

    listings.graphql
    selection: """
    id
    title
    """

The Query.featuredListings should now look like this:

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
"""
)
}

Let's recap what this code is doing.

  • To resolve our Query.featuredListings , we've set up our schema with our REST API source, named "v1". We've used the @connect to "connect" this to that source.
  • Next, we've used the http property to describe the and endpoint that retrieves the data needed for this . In this case, that's GET: "/featured-listings". (Because we already defined baseUrl in our initial source definition, we can just reference the specific endpoint we want to use here.)
  • Finally, we've defined the selection of properties we want to pluck from each item in the response: namely, id and title.

Checking our work

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

Let's build an asking for our featuredListings, their id and title. You can build this yourself using the Explorer's Documentation panel and clicking the + buttons, or by copying and pasting the below:

query GetFeaturedListings {
featuredListings {
id
title
}
}

Run the operation and... you should get data back! ๐ŸŽ‰

{
"data": {
"featuredListings": [
{
"id": "listing-1",
"title": "Cave campsite in snowy MoundiiX"
},
{
"id": "listing-2",
"title": "Cozy yurt in Mraza"
},
{
"id": "listing-3",
"title": "Repurposed mid century aircraft in Kessail"
}
]
}
}

With just a few lines added to our schema, we've used Apollo Connectors to plug into an existing REST APIโ€”and we didn't write a single line of resolver code! ๐Ÿ‘

Practice

Which of the following @connect directive parameters should you use to map the endpoint's specific JSON properties to your GraphQL schema?

Key takeaways

  • The @connect describes how to get the data for a particular using a defined source.
  • We pass the http parameter to define the HTTP methods and URL parameters on the source that should be used to retrieve data for a .
  • We can map the REST API's JSON response to our by passing the selection parameter, which lets us define the specific properties on the response that we want.
  • The fourth parameter we can pass to @connect, entity, enables the to act as an in .

Up next

We started small with id and title, but there's more to show for a listing! Let's add more and see what happens when our mapping doesn't quite match one-to-one.

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.