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")  
}
posta 5 anni fa886 visualizzazioni
2 Risposte
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 } } } }
  }
}
con risposta 5 anni fa
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.

con risposta 5 anni fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande