Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows for publishing extra attributes in google pubsub #40

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/poor-onions-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphql-eventbus": minor
"graphql-eventbus-google-pubsub": minor
---

allows for publishing extra attributes in google pubsub
37 changes: 37 additions & 0 deletions packages/core/src/GraphQLEventbus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ test("Publishing fails if bus is not initialized", async () => {
).rejects.toThrow();
});

test("extra payload is passed to the publish cb", async () => {
const publishCb = jest.fn();
const bus = new GraphQLEventbus({
publisher: {
schema: pubSchema,
publish: async (d) => {
publishCb(d);
},
allowInvalidTopic: true,
},
});
await bus.init();
await bus.publish({
topic: "TestEvent",
payload: {
id: "123",
name: "name",
},
extra: {
a: 1,
},
});
expect(publishCb).toBeCalledTimes(1);
expect(publishCb.mock.calls[0][0]).toMatchObject({
baggage: {
payload: {
id: "123",
name: "name",
},
},
topic: "TestEvent",
extra: {
a: 1,
},
});
});

test("valid events are consumed and hooks are called", async () => {
const consumeCb = jest.fn();
let cbRef!: DataCb;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/GraphQLEventbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class GraphQLEventbus {
publish: (args: {
topic: string;
baggage: Baggage;
extra?: Record<string, unknown>;
}) => Promise<unknown>;
allowInvalidTopic?: boolean;
};
Expand Down Expand Up @@ -246,6 +247,7 @@ export class GraphQLEventbus {
topic: string;
payload: {};
metadata?: Partial<GraphQLEventbusMetadata>;
extra?: Record<string, unknown>;
}) => {
if (!this.isInitialized) {
throw new Error("The eventbus must be initialized before publishing.");
Expand Down Expand Up @@ -294,6 +296,7 @@ export class GraphQLEventbus {
metadata,
payload: props.payload,
},
extra: props.extra,
});
if (publishSuccessHooks.length) {
await Promise.all(
Expand Down
10 changes: 10 additions & 0 deletions packages/google-pubsub/src/PubSubEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class PubSubEventBus {
publish: async (a) => {
await this.publishTopics[a.topic].publishMessage({
data: Buffer.from(JSON.stringify(a.baggage)),
...a.extra,
});
},
}
Expand Down Expand Up @@ -153,11 +154,20 @@ export class PubSubEventBus {
topic: string;
payload: Record<string, unknown>;
metadata?: Partial<GraphQLEventbusMetadata>;
attributes?:
| {
[k: string]: string;
}
| null
| undefined;
}) => {
await this.bus.publish({
payload: a.payload,
topic: a.topic,
metadata: a.metadata,
extra: {
atttibutes: a.attributes,
},
});
};
}
Loading