user3411864
user3411864

Reputation: 644

Nested mutation GraphQL

I'm using AWS Appsync and Amplify. A snippet of my GraphQL schema look like this:

type Recipe
@model
@auth(rules: [{allow: owner}])
{
 id: ID!
 title: String!
 key: String!
 courses: [Course!]!
}

type Course
@model
@auth(rules: [{allow: owner}])
{
  id: ID!
  name: String!
}

On amplify push it creates the DynamoDB tables Recipe and Course

After reading many tutorials I still don't get it how to add a recipe in GraphiQL.

How can i insert a new Recipe that has a reference to a course and avoid duplicates in the Courses table?

Upvotes: 0

Views: 1938

Answers (1)

Victor
Victor

Reputation: 777

To create multiples Recipe referencing the same Course without duplicates in the Course table, you need to design a many-to-many relationship.

So far the relationship you have designed is not enough for AppSync to understand, you are missing @connection attributes. You can read this answer on github to have an explanation of how to design this many-to-many relation in AppSync

After designing the relation, you will use a mutation to insert data, and it's likely that AppSync will generate the mutation code for you (if not, use amplify codegen in the console). You will then be able to create data.

Since you use DynamoDB with multiple tables (default mode for amplify / AppSync), you will have to either :

  • Call multiple mutations in a row
  • Use a custom resolver, as described in this SO answer

Upvotes: 2

Related Questions