Defining relationships between different graphql model types

Defining relationships between different graphql model types


0

The context of our problem: We are new in using AWS DataStore via GraphQl and defined two models, Memory and UserLabel.

type Memory @model
{
  id: ID!
  authorId: String!
  text: String!
  createdOn: AWSDateTime!
  lastModifiedOn: AWSDateTime!
  userLabels: [UserLabel] @hasMany(fields: ["id"])
  mapPin: [MapPin] @hasMany(fields: ["id"])
}

type UserLabel @model {
  id: ID!
  authorId: String!
  name: String! 
  color: Int # color code 
}

Our plan/idea is that a user can create memories and save these in the datastore. Additionally, the user can set labels for each memory. Those labels are created independently, so that one label can be used for multiple memories.
For example: Memory1 has label1 and label2, Memory2 has label1.
label1 is the same label for both memories.

The problem is if we save a memory in the datastore and retrieve it afterwards, our userLabels field is null.

Here is a code example:
We some print statements. In #1 the fields userLabels and MapPin are both not included, but if you look at #2 we can access the field. #3 is after we saved the memory in the datastore and retrieved it, but then is the field null.

Memory entry = Memory(
          authorId: '', // please ignore, will be set later
          createdOn: toTemporalDateTime(_selectedDate),
          lastModifiedOn: toTemporalDateTime(lastAdded),
          text: description,
          userLabels: selectedLabels, // List<UserLabel>
          mapPin: location,
        );
print(entry); // #1
print(entry.userLabels); // #2
await getMemoryStorage().addMemory(entry);
print((await getMemoryStorage().getMemory(entry))!.userLabels); // #3      
#1 Memory {id=19b50fc1-1d81-47af-8048-82c4194535ee, authorId=, text=Hello, createdOn=2023-08-16T08:36:09.048710000Z, lastModifiedOn=2023-08-16T08:36:09.049097000Z, createdAt=null, updatedAt=null}

#2 [UserLabel {id=a4ccb1cb-616b-4c26-af76-4a0521b0122e, authorId=, name=test , color=4281896508, createdAt=null, updatedAt=null}]

#3 null

We believe that something is not correct with the way we defined these model types, but we are unsure what’s the right implementation. We appreciate any help or suggestions

Share

New contributor

JncHrmn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 4 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *