Skip to main content
The OnPublishEvents handler is a custom module hook that allows you to intercept and process events before they are sent to providers through GraphQL mutations. This handler is called whenever a batch of events is about to be published to a provider, giving you the opportunity to filter, transform, enrich, or validate events before they are sent to the provider. This handler is particularly useful for:
  • Event validation: Ensure events meet specific criteria before publishing
  • Data transformation: Modify event payloads to match provider expectations
  • Event enrichment: Add additional metadata or context to events
  • Authentication and authorization: Filter events based on user permissions
  • Monitoring and analytics: Log or track outgoing events for observability
This handler is executed only when a GraphQL mutation triggers event publishing. Unlike OnReceiveEvents, this handler processes outgoing events to providers, not incoming events from subscriptions.

Handler Interface

In order to use the OnPublishEvents handler you need to create a Custom Module which implements the StreamPublishEventHandler interface.

Error Handling

As mentioned in the Publish Overview Section the return type of a Cosmo Streams mutation must use the type edfs__PublishResult. This type declares a boolean success field.
Implementations of OnPublishEvents handlers return two fields: events and error. When error is not nil, the client’s response will have the success field set to false. Also the error will be logged on the routers console output. When events are returned, these will always be sent to the provider, even if you return an error. This can be useful in case you partially processed data but hit an error along the way. In case you don’t wont to sent any events to the provider, you can return datasource.NewStreamEvents(nil). See code examples below for a demonstration.
When the OnPublishEvents handler returns an error, the router takes the following actions:
  1. The client will receive a response, where the success field is false
  2. Returned events are sent to the message providers, if any provided
  3. The error is logged by the router with details about the mutation, provider, and field name
Returning events alongide an error from OnPublishEvents will send these events to the provider. In case you don’t want to send any you need to return an empty list of events. Refer to the examples down below to see how this can be done.
Here is an example of proper error handling:

Usage Example

Complete Custom Module with Event Bypass

The following example contains a complete Custom Module implementation, including handler registration, with a handler that will simply pass events through unchanged. This demonstrates how to register your OnPublishEvents Custom Module.

Restrict Handler to run on certain mutations and providers

Most of the time you want your hook to only deal with certain mutations. The OnPublishEvents Handler is run for every mutation configured for Cosmo Streams. You can access the name of the mutation you care for and return early if it’s not the right one.

Prevent unauthorized users from sending Cosmo Streams mutation events to providers

You can use ctx.Authentication() to validate that only authorized users can publish events to specific providers. This is useful for securing mutation operations that trigger event publishing.

Attach headers to Kafka events

You can attach headers to Kafka events before sending them to providers.

Transform events

You can change event data. For example you could change the id of the entity before you emit the event. Suppose you have a Cosmo Streams mutation, which lets you create a new user. You can change the name of the user before it get’s sent as an event to the broker.