3. N+1 requests in action
2m

Overview

Project all set up? Let's get going!

In this lesson, we will:

  • Explore the project's schema
  • Use the Connectors Debugger in Sandbox to inspect our Connector requests
  • Identify how the N+1 problem appears in our project

The schema

Let's take a closer look at the current state of our Airlock schema in listings.graphql.

We have one entry point into our : Query.featuredListings, which returns a list of Listing types. The Connector populates that data by making a request to the GET /featured-listings endpoint, and maps the response for a few (id, title, numOfBeds, and costPerNight).

listings.graphql
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
"""
)
}

The Listing type is an with its own Connector, which makes a request to the GET /listings/:id endpoint. It maps the response for all the in the type, more than what the Query.featuredListings Connector by itself can provide.

Note: Check out the Apollo documentation for a refresher on what entities are.

listings.graphql
"A particular intergalactic location available for booking"
type Listing
@connect(
source: "listings"
http: { GET: "/listings/{$this.id}" }
selection: """
id
title
numOfBeds
costPerNight
description
photoThumbnail
latitude
longitude
"""
)
@key(fields: "id") {
# ...Listing fields
}

Let's explore this schema with Sandbox and start sending queries!

N+1 requests

Open up http://localhost:4000 in the browser, where the local Sandbox is running our .

Let's build a that asks for just a few :

query GetFeaturedListings {
featuredListings {
id
title
numOfBeds
costPerNight
}
}

When we run this , we'll get some data back! Let's take a peek behind the scenes at where this data is coming from.

Navigate to the Connectors Debugger by hitting the arrow beside Response.

http://localhost:4000

Connectors Debugger in Sandbox

From here, we can see we have one request to GET /featured-listings. Great! That's exactly what the schema told us would happen.

UI showing the first request to get featured listings

But our UI mockup needs more than just those four . We need the description, photoThumbnail, latitude, and longitude , too. Let's add those to our and see what happens.

query GetFeaturedListings {
featuredListings {
id
title
numOfBeds
costPerNight
description
photoThumbnail
latitude
longitude
}
}

We'll run it again… and the Connectors Debugger shows us more requests!

http://localhost:4000

Connectors Debugger in Sandbox

This time, we've got three more requests: one for each listing object returned by the Query.featuredListings Connector. Because that Connector can't fulfill all of the defined in the , the goes to the Listing Connector for help. The Listing Connector takes care of making a request to GET /listings/:id, for each ID, and returns the rest of the .

UI showing the N requests to get more listing fields

In this case, those three requests represent the N in our N+1 problem. This doesn't look too bad, but we want to get ahead of any potential performance issues before it gets worse! If our featured listings increase without warning, those three additional requests will also grow accordingly.

Practice

Which of the following situations illustrate the N+1 problem?

Key takeaways

  • A Connector for an can retrieve all the for that type.

Up next

Now that we've seen the N+1 problem in action, it's time to do something about it. In the next lesson, you'll dive into batch endpoints and how to define them in a Connector.

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.