Mutation with relationships

How do I write a mutation for a type that includes an array of a subtype?

Here’s my current mutation

    const AddRecipeMutation = gql`
      mutation AddRecipe($recipe: RecipeInsertInput!) {
        addedRecipe: insertOneRecipe(data: $recipe) {
          _id
          author
          title
        }
      }`;

But, when I add a recipe, I also want to add an array of ingredients, which have their own type (Ingredient).
So one Recipe has a one-to-many relationship with its Ingredients which have keys [_id, name]. How do I build the AddRecipeMutation?

The solution ended up being…

    const AddRecipeMutation = gql`
      mutation AddRecipe($recipe: RecipeInsertInput!) {
        addedRecipe: insertOneRecipe(data: $recipe) {
          _id
          author
          title
          ingredients {
            _id
            author
            name
          }
        }
      }
    `;

The more difficult part though was figuring out how to pass the parameters. I had to figure out that a recipe needed to be passed in this format.

    {
          _id: id,
          author: currentRealmUser,
          title: title,
          ingredients: [{
            create: [ingredient, ingredient...],
            link: [id]
          }]
    }

where the create field contains an array of ingredient objects.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.