Many-to-Many relations and subscriptions in Amplify

0

Hello,

How can I have a subscription notification message (upon an update) include the full relation data in a many-to-many relation?

I have a many-to-many relations using the Amplify @model and @connection directives, as it is documented in the Amplify docs. In this case, I have a Person and City entities, where one person can belong to many cities, and one city can belong to many persons. To model this, I have a PersonCity connecting entity.

For queries, I am able to fetch all the cities for a person with a custom GraphQL query, which I pass to the graphqlOperation() function.

My problem is that I also want the cities returned via subscription messages when the Person is updated (for instance, a new city is added). Currently, I only receive City ID in the cities array inside Person. However, I want the subscription message to return all the City fields as well, e.g. name.

Any help would be greatly appreciated.

This is the general schema I'm working with:

type Person @model
{
  id: ID!,
  cities: [PersonCity] @connection(name: "PersonCities")
}

type City @model 
  {
  id: ID!
  name: String!
  persons: [PersonCity] @connection(name: "CityPersons")
}

type PersonCity @model(queries: null) 
 {
  id: ID!
  person: Person! @connection(name: "PersonCities")
  city: City! @connection(name: "CityPersons")  
}
已提问 5 年前884 查看次数
2 回答
0

AppSync subscriptions publish the object that is returned by the mutation (after filtering subscription selection set fields). That means that if you include the related field in the selection set of the mutations and the subscription query then you will be able to subscribe to the connected information.

E.G.

mutation {
  updatePerson(input: { ... }) {
    id
    cities { items { id name persons { items { id } } } }
  }
}
subscription {
  onUpdatePerson {
    id
    cities { items { id name persons { items { id } } } }
  }
}
已回答 5 年前
0

Thanks a lot! That explains it, and it makes sense. I'd be happy to contribute this to the Amplify docs, if that's possible. I'll see if it's open for edits.

BTW, Amplify is a fantastic service, kudos for the great work.

已回答 5 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则