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年前874ビュー
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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ