Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

[Discuss]: document an example of how PubSubEngine or a GraphbackCrudService can be used to achieve async / event bound tasks #2119

Open
machi1990 opened this issue Sep 23, 2020 · 2 comments

Comments

@machi1990
Copy link
Contributor

machi1990 commented Sep 23, 2020

This builds on #1896 but can be documented separately.

With the crud Event emission mechanism we've in place, once can achieve so many event based operations e.g

If we take the Meter business model from an IOT world

""" 
@model
"""
type Meter {
  """
  @id
  """
  id: ID!
  address: String!
  latitude: String!
  longitude: String!
}

One usecase will be to send a notification once:

  • a new meter is up ("created"),
  • a meter is moved position "updated"
  • or a meter goes down / removed "deleted".

On top of being able to subscribe to these changes via GraphQL subscriptions (on-call engineers would see these),
one would also like to perform other business related tasks e.g

  • send an email to notify the admin or an on-call engineer (he might not be looking at the screen for the moment) etc etc

This can be achieved now by subscribing to the PubSub engine passed as an example but it requires one to have the knowledge of Graphback internals e.g

  • topicName for each operation and type,
  • the structure of the event/payload.

Code snippet:

interface Meter {
  id: number;
  address: string;
  latitude: string;
  longitude: string;
}

pubSubEngine.subscribe('CREATE_METER', (event: {
  newMeter: Meter
}) => {
  cons payload = event.newMeter
  // do business logic here after meter creation
})

pubSubEngine.subscribe('UPDATE_METER', (event: {
  updatedMeter: Meter
}) => {
  cons payload = event.updatedMeter
  // do business logic here after meter update
})

pubSubEngine.subscribe('DELETE_METER', (event: {
  deletedMeter: Meter
}) => {
  cons payload = event.deletedMeter
  // do business logic here after meter deletion
})

It will be useful to document the default topicName and especially the event structure things.

Optionally, we could provide a way to subscribe to these changes via the service.

services.Meter.subscribe('CREATE', (payload: Meter) => {
 // do business logic here
})
@machi1990 machi1990 added enhancement New feature or request feature docs labels Sep 23, 2020
@machi1990
Copy link
Contributor Author

/cc @craicoverflow, @wtrocki
Automatically generated comment to notify maintainers

@wtrocki
Copy link
Contributor

wtrocki commented Sep 23, 2020

If we move to streaming platform use case we will naturally diverge from topic being implementation detail and changing it being advanced feature (https://graphback.dev/docs/subscriptions/intro#changing-subscription-topics) to topic creation method being required for each model (or even schema based)

topicName for each operation and type

Solved by extracting topic creation process to template (this is hackend on datasync starter and will require changes in API (or completely different service probably)

the structure of the event/payload.

Solved by strongly typed Meter service

Optionally, we could provide a way to subscribe to these changes via the service.

For me that is not optional but natural requirement once we get into this phase.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants