GraphQL nested nodes in mutation - Object literal may only specify known properties, and 'days' does not exist in type

Viewed 228

The general structure of my schema is this:

  • A routine can have multiple days.
  • A day can have multiple exercises.

I have a GraphQL mutation generated by the AWS Amplify CLI.

The mutation looks like this:

export const createRoutine = /* GraphQL */ `
  mutation CreateRoutine(
    $input: CreateRoutineInput!
    $condition: ModelRoutineConditionInput
  ) {
    createRoutine(input: $input, condition: $condition) {
      id
      name
      description
      days {
        items {
          id
          routineID
          name
          description
          exercises {
            items {
              id
              dayID
              name
              description
              reps
              sets
            }
          }
        }
      }
    }
  }
`;

When I go to make a GraphQL mutation like this:

const inp = {
      id: id,
      name: "Test Routine Dynamic",
      description: "Description for TRD",
      days: {
        items: {
          id: id,
          routineID: id,
          name: "Day 1 of TRD",
          description: "Description for Day 1 of TRD",
          exercises: {
            items: {
              id: id,
              dayID: id,
              name: "Bench Press",
              description: "Push the bar",
              reps: "3",
              sets: "5",
            },
          },
        },
      },

I get an error from my IDE saying: Object literal may only specify known properties, and 'days' does not exist in type

Because the type of input is CreateRoutineInput which looks like this:

export type CreateRoutineInput = {
  id?: string | null,
  name: string,
  description?: string | null,
};

Why does the generated type of CreateRoutineInput not allow me to specify the nested items "Days"? I am a bit new to GraphQL so maybe this is something that is not meant to be done.

Would really appreciate any help, thanks.

1 Answers

Alright I think I found the solution, hopefully if anyone stumbles across this thread in the future this might help:

The problem was actually my schema. The way I had it set up was that each "object" was an @model. Which I think actually means they all have their own respective auth rules and their own tables as well. Not 100% sure on that though!

I didn't actually really need them to be separated that much as they are all hierarchally related to one another.

So my schema looked like this before hand:

type Routine
  @model
  @auth(...) {
  id: ID!
  name: String!
  description: String
  days: [Day]! @connection(keyName: "byRoutine", fields: ["id"])
}

# day = post
type Day
  @model
  @key(name: "byRoutine", fields: ["routineID"])
  @auth(...) {
  id: ID!
  routineID: ID!
  name: String
  description: String
  exercises: [Exercise] @connection(keyName: "byDay", fields: ["id"])
}

type Exercise
  @model
  @key(name: "byDay", fields: ["dayID"])
  @auth(...) {
  id: ID!
  dayID: ID!
  name: String!
  description: String
  reps: String!
  sets: String!
}

As you can see, they are all tagged with @model. So after many other attempts, I just removed all the tags from the nested types, and just made them types. Then they are simply used as types for the fields of the parent types.

So it looks like this now:

type Routine
  @model
  @auth(...) {
  id: ID!
  name: String!
  description: String
  days: [Day]!
}

type Day {
  id: ID!
  routineID: ID!
  name: String
  description: String
  exercises: [Exercise]
}

type Exercise {
  id: ID!
  dayID: ID!
  name: String!
  description: String
  reps: String!
  sets: String!
}

And it works! Now I can create nested mutations. Looks like this now: Amplify Console API Mutation

Hope this helps you if you are stuck! It was a big relief for me when I fixed it.

Related