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.graphqlfile in therecipessubgraph.Scroll down to find the
Recipetype 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.graphqlfile, after the fullRecipetype definition, add theCookwaretype. Specifically, we're going to add a stub of theCookwareentity, which contains the minimum fields therecipessubgraph 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
namefield, not contributing fields to it!), we need to add one more thing. Inside the@keydirective, add another argument forresolvableand 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.jsfile, add the following resolver function for thecookwarefield.
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.