Search code examples
graphqlrelaypostgraphile

PostGraphile subscriptions - what does "topic" refer to?


I'm using PgPubsub and I'm trying to get my head around listen and topic*:"" vis-a-vis what to put there.

For example, let's say I have a <PostList> component that renders a list of <Post> and I want to update the list when a Post is created or deleted.

I'm not sure how to structure my subscription so I'm listening for changes to PostList. Here's a screenshot of my GraphiQL:

enter image description here


Solution

  • In pubsub (publish-subscribe), messages are published to a "topic" and you can subscribe to that topic to receive the messages that are published there.

    You appear to be using the "simple subscriptions" functionality in PostGraphile, so I'll answer assuming that's the case.

    With the subscription listen(topic: "whatGoesHere?") you have, you need to broadcast to the postgraphile:whatGoesHere? topic to trigger a subscription event. You can do this by issuing the SQL statement NOTIFY "postgraphile:whatGoesHere?", '{"ok": true}';. You can do this with psql:

    $ psql your_database_here
    [your_database_here] # NOTIFY "postgraphile:whatGoesHere?", '{"ok": true}';
    NOTIFY
    [your_database_here] # 
    

    Assuming your GraphQL subscription is running, this should cause the selection set to be evaluated and the results to be sent to GraphiQL.

    You'll probably want to fire this NOTIFY statement from a function or trigger; you can read more about that in the PostGraphile Subscriptions documentation.