8. Exercise: Referencing an entity
1m

Exercise: Referencing an entity

🎯 Goal: Get the most recent reviews. For each review, show the listing's title, number of beds and cost per night.

Open up Explorer. When you run the below, you'll get an error!

query GetRecentReviews {
recentReviews {
text
rating
listing {
title
numOfBeds
costPerNight
}
}
}

After successfully completing the exercise, you should get data back when running the GetRecentReviews again.

Hints

✅ Solution

Expand the sections below for a quick copy-paste, or follow the instructions to go step-by-step.

  1. In subgraph-reviews/reviews.graphql, add a new to the Review type called listing, which returns a Listing type.

    subgraph-reviews/reviews.graphql
    type Review {
    id: ID!
    "Comment the author has written"
    text: String!
    "The numerical rating for the review target, on a scale of 1-5, with 5 being excellent."
    rating: Float!
    "The listing associated with the review"
    listing: Listing
    }
  2. Find the Review.listing in subgraph-reviews/src/resolvers.js.

    subgraph-reviews/src/resolvers.js
    Review: {
    listing: () => {
    // TODO
    },
    },
  3. Inside the , return an representation of a Listing .

    subgraph-reviews/src/resolvers.js
    Review: {
    listing: (review) => {
    return { id: review.listingId };
    },
    },

    Remember that the first parameter of a function is the parent, which in this case is the Review type returned by the Query.recentReviews . Feel free to add a console.log before returning the representation to find out what is available in the review object.

    To return an representation, we need to return an object with the id property set to the listingId value. We could also add a __typename, but takes care of that for us automatically.

BONUS: Examine the query plan

Here's the we're running:

query GetRecentReviews {
recentReviews {
text
rating
listing {
title
numOfBeds
costPerNight
}
}
}

Examine the for the .

In the query plan, what does the router ask for from the reviews subgraph?
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.