|
3 | 3 | :page-aliases: subscriptions/plugins/index.adoc, subscriptions/plugins/amqp.adoc, subscriptions/plugins/single-instance.adoc
|
4 | 4 | :description: This page describes how to customize the behavior of subscriptions.
|
5 | 5 |
|
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). |
7 | 7 | Its behavior can be configured by passing an instance of `Neo4jGraphQLSubscriptionsCDCEngine`.
|
8 | 8 |
|
9 | 9 | == Neo4jGraphQLSubscriptionsCDCEngine
|
@@ -44,3 +44,49 @@ const neoSchema = new Neo4jGraphQL({
|
44 | 44 | });
|
45 | 45 | ----
|
46 | 46 |
|
| 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