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년 전880회 조회
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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠