1

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.

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have this same issue now and the same goal, but now using AWSJSON/[AWSJSON]/[String] with appsync update just replaces all the existing content - I don't understand how can update the model to add an item to an array attribute of the model. Someone?
1

It sounds like you do want a new elements type in your schema, but you should configure the resolver for it to point to an array-like column in whatever table that Note's resolver is pointing to. Keep in mind types in your graphQL schema don't necessarily need to correspond to what is in your data sources one-to-one. What does your database structure look like?

1 Comment

Actually, I want to create an Array of Objects without creating a new table like this: Components: [{object}.{object}.{object}] Can you suggest some resources regarding creating this with amplify cli through appsync
1

Let's say you want to save an Array of String inside the components of the @model below:

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

You can either use AWSJSON and save it in a String format as Rawan mentioned above or you can try this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.