简体   繁体   中英

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.

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?

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

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM