Skip to content

Commit fad2b5b

Browse files
committed
Add Listen to subscription events section
1 parent 5727bc3 commit fad2b5b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

modules/ROOT/pages/subscriptions/customize-cdc.adoc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:page-aliases: subscriptions/plugins/index.adoc, subscriptions/plugins/amqp.adoc, subscriptions/plugins/single-instance.adoc
44
:description: This page describes how to customize the behavior of subscriptions.
55

6-
GraphQL subscriptions to Neo4j use [Change Data Capture](https://neo4j.com/docs/cdc/current/) (CDC).
6+
GraphQL subscriptions to Neo4j use link:https://neo4j.com/docs/cdc/current/[Change Data Capture] (CDC).
77
Its behavior can be configured by passing an instance of `Neo4jGraphQLSubscriptionsCDCEngine`.
88

99
== Neo4jGraphQLSubscriptionsCDCEngine
@@ -44,3 +44,49 @@ const neoSchema = new Neo4jGraphQL({
4444
});
4545
----
4646

47+
== Listen to subscription events
48+
49+
The class `Neo4jGraphQLSubscriptionsCDCEngine` exposes the `events` property, an instance of link:https://nodejs.org/api/events.html#class-eventemitter[EventEmitter], which emits an event for each CDC event. This is useful for scenarios such as forwarding CDC events to an external queue for processing.
50+
51+
[NOTE]
52+
====
53+
It is generally recommended to link:https://neo4j.com/docs/cdc/current/[use CDC directly], outside of the GraphQL library, for event listening.
54+
====
55+
56+
To listen to GraphQL subscription events, use the `.on` method on the `events` property:
57+
58+
[source, javascript, indent=0]
59+
----
60+
engine.events.on("create", (payload) => {
61+
console.log("Node Created!", payload)
62+
});
63+
----
64+
65+
The following CDC-triggered events can be listened to:
66+
67+
* `create`: A node has been created.
68+
* `update`: A node has been updated.
69+
* `delete`: A node has been deleted.
70+
71+
=== Event payload
72+
73+
Each event includes a JSON payload with event details:
74+
75+
* `event`: The event type, matching the listener event name.
76+
* `typename`: The GraphQL type name.
77+
* `properties`: Contains `old` and `new` objects representing node properties before and after the mutation. These may be `undefined` if the node did not exist before or after the mutation.
78+
* `id`: The node ID (not exposed through the GraphQL API).
79+
* `timestamp`: The timestamp of the mutation that triggered the event.
80+
81+
For example, a node creation event would look like this:
82+
83+
[source, javascript, indent=0]
84+
----
85+
{
86+
event: 'create',
87+
typename: 'Movie',
88+
properties: { old: undefined, new: { title: 'The Matrix' } },
89+
id: '4:70bade62-2121-4808-9348-3ab77859e164:510',
90+
timestamp: 1739286926054
91+
}
92+
----

0 commit comments

Comments
 (0)