-
Notifications
You must be signed in to change notification settings - Fork 126
(feat): add support for default query handler #1639
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
Conversation
packages/workflow/src/workflow.ts
Outdated
const activator = assertInWorkflowContext( | ||
'Workflow.setDefaultQueryHandler(...) may only be used from a Workflow Execution.' | ||
); | ||
if (typeof handler === 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You can combine the if
and else if
branches here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
packages/workflow/src/internals.ts
Outdated
let fn = this.queryHandlers.get(queryName)?.handler; | ||
// Fallback to default query handler if no handler is registered. | ||
if (fn === undefined) { | ||
fn = this.defaultQueryHandler; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default query handler will need an extra argument, the first one, that indicates the name of the query being called. See signalWorkflowNextHandler()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, improved the test to catch this case. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default query handler needs the name of the incoming query, just like we do for the default signal handler.
That's the only blocking change.
…andler properly, improve test
packages/workflow/src/interfaces.ts
Outdated
/** | ||
* A handler function accepting query calls for non-registered query names. | ||
*/ | ||
export type DefaultQueryHandler = (queryName: string, ...args: any[]) => any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: That should be unknown
rather than any
(both occurrences).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
packages/workflow/src/internals.ts
Outdated
// Fallback to default query handler if no handler is registered. | ||
if (fn === undefined && this.defaultQueryHandler !== undefined) { | ||
// Use non-null assertion because Typescript can't tell that defaultQueryHandler cannot be undefined here. | ||
fn = (...args) => this.defaultQueryHandler!(queryName, ...args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS is technically right: even though you just checked that this.defaultQueryHandler
is not undefined, you are then using that field inside a function call; there is no guarantee that this.defaultQueryHandler
will still be valid by the time fn
gets called. Well, we know it will, but the compiler can't figure this out.
You can avoid the TS error without a non-null assertion by copying this.defaultQueryHandler
to a temporary variable inside the if
block, or by using this.defaultQueryHandler.bind(...)
instead:
fn = (...args) => this.defaultQueryHandler!(queryName, ...args); | |
fn = this.defaultQueryHandler.bind(this, queryName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address my two minor suggestions before merging.
Added support for to register a default query handler - a handler for queries that do not have registered query names/handlers.
Part of [Feature Request] Support dynamic workflows, activities, signals, queries, and updates #1015
How was this tested:
Added small integration tests.
Any docs updates needed?
Maybe