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

Goal: Implement the changes needed to resolve our dream query.

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 entity, which contains the minimum s the recipes needs to reference it.

    schema.graphql
    type Cookware @key(fields: "name") {
    }
  5. Since the doesn't contribute any additional s to the entity (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.

    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 changes! Jump back to Sandbox and run the query 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 query plan to connect data from multiple s? Explore the details in Lesson 11: Entities and the query plan in the Voyage I: Federation from Day One course.

Previous