15. Follow-along: Connecting data with entities
2m

Goal: Implement the changes needed to resolve our dream .

The dream query
query GetRecipeAndCookwareInformation {
recipe(id: "rec3j49yFpY2uRNM1") {
name
description
ingredients {
text
}
instructions
cookware {
name
description
cleaningInstructions
}
}
}

Referencing an entity

  1. Open up the schema.graphql file in the recipes .

  2. Scroll down to find the Recipe type and add the new at the end.

    schema.graphql
    type Recipe {
    # ... other Recipe fields
    "List of cookware used in the recipe"
    cookware: [Cookware]
    }
  3. Save your changes!

    Which of the following scenarios happen after saving our changes?
  4. In the schema.graphql file, after the full Recipe type definition, add the Cookware type. Specifically, we're going to add a stub of the Cookware , which contains the minimum the recipes needs to reference it.

    schema.graphql
    type Cookware @key(fields: "name") {
    }
  5. Since the doesn't contribute any additional to the (we're only referencing the entity using its name , not contributing fields to it!), we need to add one more thing. Inside the @key , add another for resolvable and set it to false.

    schema.graphql
    type Cookware @key(fields: "name", resolvable: false) {
    name: String!
    }
  6. Save your changes.

    Which of the following scenarios happen after saving our changes?
  7. Jump over back to the Sandbox tab which is connected to our locally-running on http://localhost:4000 and run the dream .

    query GetRecipeAndCookwareInformation {
    recipe(id: "rec3j49yFpY2uRNM1") {
    name
    description
    ingredients {
    text
    }
    instructions
    cookware {
    name
    description
    cleaningInstructions
    }
    }
    }
    What is true about the results of running the dream query?

Adding a resolver function for cookware

  1. In the src/resolvers/Recipe.js file, add the following function for the cookware .
src/resolvers/Recipe.js
cookware(recipe, _, { dataSources }) {
const cookwareNamesList = dataSources.recipesAPI.getRecipeCookware(recipe.id);
if (!cookwareNamesList) return;
return cookwareNamesList.map((c) => ({
name: c,
}));
},

Alright, we've got the code changes to back our schema changes! Jump back to Sandbox and run the again.

query GetRecipeAndCookwareInformation {
recipe(id: "rec3j49yFpY2uRNM1") {
name
description
ingredients {
text
}
instructions
cookware {
name
description
cleaningInstructions
}
}
}

Fantastic, we've got it! 🎉

Optional reading: How does the use entities and the to connect data from multiple ? Explore the details in Lesson 11: Entities and the query plan in the Voyage I: Federation from Day One course.

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.