How to efficiently make a recursive table in graphql?

47 views Asked by At

I am writing a graphql schema using amplify. I have a categories table in which I want to relate each category with other categories. Every Category can have multiple parents or children. This is what I have right now.

type Category @model {
  id: ID! @primaryKey
  name: String!
  parents: [Category] @hasMany
  subCategories:[Category] @hasMany
  categoryParentsId: [String] @index(name: "byParent")
  categorySubCategoriesId: [String] @index(name: "byChild")
}

type Query {
  getAllCategories: [Category!]!
}

input CreateCategoryInput {
  name: String!
  categoryParentsId: [String]
  categorySubCategoriesId: [String]
}

I am using the mutation generated by amplify:

export const createCategory = /* GraphQL */
  mutation CreateCategory( $input: CreateCategoryInput! $condition: ModelCategoryConditionInput ) {
    createCategory(input: $input, condition: $condition) {
      id
      name
      parents {
        nextToken
        __typename
      }
      items {
        nextToken
        __typename
      }
      subCategories {
        nextToken
        __typename
      }
      categoryParentsId
      categorySubCategoriesId
      createdAt
      updatedAt
      __typename
    }
  }

I am using a react app in the frontend. Whenever I try to input this error shows up from Amplify.

"One or more parameter values were invalid: Type mismatch for Index Key categoryParentsId Expected: S Actual: L IndexName: gsi-Category.parents (Service: DynamoDb, Status Code: 400"

I understand that it is generating a GSI which are categoryParentsId and categorySubCategoriesId which can't be other than string, number or boolean. So it kind of conflicts with what I stated as an array of strings.

Should I just add another field that stores that array of strings? Is there a better approach?

1

There are 1 answers

1
Michel Floyd On

You don't need to refer to both the parent and child types and the corresponding ids in your GraphQL type:

type Category @model {
  id: ID! @primaryKey
  name: String!
  parents: [Category] @hasMany
  subCategories:[Category] @hasMany
 }

should suffice.

Normally references to related objects are done by referring to the object type and not the id (you can always get the id(s) of the related objects by including them in your query). In your case you have both.

Also please include the definition of the mutation you are attempting. You showed the input type but not the mutation.