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 theOnPublishEvents 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 typeedfs__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:
- The client will receive a response, where the
successfield isfalse - Returned events are sent to the message providers, if any provided
- The error is logged by the router with details about the mutation, provider, and field name
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 yourOnPublishEvents 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. TheOnPublishEvents 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 usectx.Authentication() to validate that only authorized users can publish events to specific providers.
This is useful for securing mutation operations that trigger event publishing.