Skip to content

Commit c7b3316

Browse files
Expose Temporal Nexus operation info to Temporal Nexus operation (#1896)
1 parent ae4cb9a commit c7b3316

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

packages/nexus/src/context.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ export interface HandlerContext {
3333
taskQueue: string;
3434
}
3535

36+
/**
37+
* Holds information about the current Nexus Operation Execution.
38+
*
39+
* @experimental Nexus support in Temporal SDK is experimental.
40+
*/
41+
export interface OperationInfo {
42+
/**
43+
* Namespace this Nexus Operation is executing in
44+
*/
45+
readonly namespace: string;
46+
47+
/**
48+
* Task Queue this Nexus Operation is executing on
49+
*/
50+
readonly taskQueue: string;
51+
}
52+
3653
// Basic APIs //////////////////////////////////////////////////////////////////////////////////////
3754

3855
/**
@@ -101,3 +118,18 @@ export const metricMeter: MetricMeter = {
101118
export function getClient(): Client {
102119
return getHandlerContext().client;
103120
}
121+
122+
/**
123+
* Get information about the current Nexus Operation.
124+
*
125+
* @return OperationInfo for the current Nexus Operation being executed
126+
*
127+
* @experimental Nexus support in Temporal SDK is experimental.
128+
*/
129+
export function operationInfo(): OperationInfo {
130+
const ctx = getHandlerContext();
131+
return {
132+
namespace: ctx.namespace,
133+
taskQueue: ctx.taskQueue,
134+
};
135+
}

packages/nexus/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export {
99
log,
1010
getClient,
1111
metricMeter,
12+
operationInfo,
13+
type OperationInfo,
1214
} from './context';
1315

1416
export {

packages/test/src/test-nexus-handler.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,45 @@ test('getClient is available in handler context', async (t) => {
588588
});
589589
});
590590

591+
test('operationInfo is available in handler context', async (t) => {
592+
const { env, taskQueue, httpPort, endpointId } = t.context;
593+
594+
const w = await Worker.create({
595+
connection: env.nativeConnection,
596+
namespace: env.namespace,
597+
taskQueue,
598+
nexusServices: [
599+
nexus.serviceHandler(
600+
nexus.service('testService', {
601+
testSyncOp: nexus.operation<void, { namespace: string; taskQueue: string }>(),
602+
}),
603+
{
604+
async testSyncOp() {
605+
const info = temporalnexus.operationInfo();
606+
return {
607+
namespace: info.namespace,
608+
taskQueue: info.taskQueue,
609+
};
610+
},
611+
}
612+
),
613+
],
614+
});
615+
616+
await w.runUntil(async () => {
617+
const res = await fetch(
618+
`http://127.0.0.1:${httpPort}/nexus/endpoints/${endpointId}/services/testService/testSyncOp`,
619+
{
620+
method: 'POST',
621+
}
622+
);
623+
t.true(res.ok);
624+
const output = (await res.json()) as { namespace: string; taskQueue: string };
625+
t.is(output.namespace, 'default');
626+
t.is(output.taskQueue, taskQueue);
627+
});
628+
});
629+
591630
test('WorkflowRunOperationHandler attaches callback, link, and request ID', async (t) => {
592631
const { env, taskQueue, httpPort, endpointId } = t.context;
593632
const requestId1 = randomUUID();

0 commit comments

Comments
 (0)