Search code examples
amazon-web-servicesgraphqlaws-amplifyaws-appsyncaws-amplify-cli

Store objects into array without creating a new table in AWS AppSync schema


I was using AWS amplify with React, and GraphQL API.that leverages AWS AppSync. I'm very new to graphQL and my schema currently is like this. This is the schema inside the amplify app:

type Note @model {
  id: ID!
  name: String!
  title: String
  movie: String
}

For example, I want to store an array of objects inside components in the Note type like this:

type Note @model {
  id: ID!
  name: String!
  title: String
  movie: String
  components: []
}

Iam aware of that I can create a new table and do this:

type Note @model {
  id: ID!
  name: String!
  title: String
  movie: String
  components: [elements!]!
}
 
 type elements @model {
  id: ID!
  item: String!
}

But, I don't want to create a new table. Is there any possible way to do this?

And I saw similar questions on our platform, they are suggesting the AWSJSON AWS Scalar Types

But there is no actual resources for using them. if that's the solution kindly help me with the procedures how can we write mutation for them.


Solution

  • I have found the solution:

    By Using AWSJSON we can able to create an Array with map without creating a new table in DynamoDB

    Procedure to reproduce:

    First Add the AWSJSON to the Schema Graphql like below,

    type Note @model {
      id: ID!
      name: String!
      title: String
      movie: String
      components: AWSJSON
    }
    

    While using create mutation, Pass the data in JSON format

    Example:

    let input = `[{\"isPending\":false,\"isAccepted\":false}]`
    

    While using update mutation there was an issue with AWSJSON which will not update the map existing instead, it will create a new map in the Array.

    Solution for this issue is Using the _version