Goal: Implement the subgraph changes needed to resolve our dream query.
query GetRecipeAndCookwareInformation {recipe(id: "rec3j49yFpY2uRNM1") {namedescriptioningredients {text}instructionscookware {namedescriptioncleaningInstructions}}}
Referencing an entity
Open up the
schema.graphql
file in therecipes
subgraph.Scroll down to find the
Recipe
type and add the new field at the end.schema.graphqltype Recipe {# ... other Recipe fields"List of cookware used in the recipe"cookware: [Cookware]}Save your changes!
Which of the following scenarios happen after saving our changes?In the
schema.graphql
file, after the fullRecipe
type definition, add theCookware
type. Specifically, we're going to add a stub of theCookware
entity, which contains the minimum fields therecipes
subgraph needs to reference it.schema.graphqltype Cookware @key(fields: "name") {}Since the subgraph doesn't contribute any additional fields to the entity (we're only referencing the entity using its
name
field, not contributing fields to it!), we need to add one more thing. Inside the@key
directive, add another argument forresolvable
and set it tofalse
.schema.graphqltype Cookware @key(fields: "name", resolvable: false) {name: String!}Save your changes.
Which of the following scenarios happen after saving our changes?Jump over back to the Sandbox tab which is connected to our locally-running router on http://localhost:4000 and run the dream query.
query GetRecipeAndCookwareInformation {recipe(id: "rec3j49yFpY2uRNM1") {namedescriptioningredients {text}instructionscookware {namedescriptioncleaningInstructions}}}What is true about the results of running the dream query?
Adding a resolver function for cookware
- In the
src/resolvers/Recipe.js
file, add the following resolver function for thecookware
field.
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 query again.
query GetRecipeAndCookwareInformation {recipe(id: "rec3j49yFpY2uRNM1") {namedescriptioningredients {text}instructionscookware {namedescriptioncleaningInstructions}}}
Fantastic, we've got it! 🎉
Optional reading: How does the router use entities and the query plan to connect data from multiple subgraphs? Explore the details in Lesson 11: Entities and the query plan in the Voyage I: Federation from Day One course.
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.