hasura-trigger-subscription-server
hasura-trigger-subscription-server copied to clipboard
Enhance hasura with event-based subscriptions.
Hasura trigger subscription server
Hasura trigger subscription server is a simple example of adding trigger event-based subscription support for hasura (see hasura/graphql-engine#2317 and hasura/graphql-engine#1355), using event triggers in hasura.
Overview
We use the following database schema:
scalar Timestamp
type Todo {
id: ID!
created_at: Timestamp!
updated_at: Timestamp!
content: String!
}
expose the following events in hasura console:
enum TodoEvent {
on_todo_created
on_todo_updated
on_todo_deleted
}
and add a subscription in the custom subscription server:
type Subscription {
todoSub(trigger: [TodoEvent]): Todo
}
Customization
This example implementation uses the following two endpoints at port 5000:
/graphql: for exposing the graphql endpoint./webhook: for listening to hasura event triggers.
Both endpoints can be configured in ./index.js
Add event-based subscription to an existing table
Say you added a new table tag to the database.
scalar Timestamp
type Todo {
id: ID!
created_at: Timestamp!
updated_at: Timestamp!
content: String!
tags: [Tag]
}
type Tag {
id: ID!
text: String!
todos: [Todo]
}
To add subscription to an existing table, you need to:
-
Add a trigger in hasura console (say if you want to get notified when
tagget updated).- Click
createinEVENTStab in hasura console. - Add trigger named
on_tag_updated. - choose table
tag, trigger operations toupdate. - set webhook URL to
http://localhost:5000/webhook, if you used the preset parameters in this repository. - Click
Create Event Trigger
- Click
-
Add corresponding schema in ./schema.
- Create a file called
./schema/tag.js. - Customize the typeDefs and resolvers in
./schema/tag.js. You can refer to ./schema/todo.js as a reference, or define your own logic. - Add tag schema to
./schema/index.js, inmergeSchemasfunction.
- Create a file called
Todos
- [ ] This repo does not cover how to authenticate a user over websocket connection.
- [ ] This repo does not cover detailed information about how to filter a subscription with variables in resolvers.