-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquery_update.go
More file actions
39 lines (33 loc) · 1.33 KB
/
query_update.go
File metadata and controls
39 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tempts
import (
"context"
"go.temporal.io/sdk/workflow"
)
// QueryHandler is used for interacting with queries on workflows.
// Currently there's no way to enforce the connection between the query and the workflow it should be valid on.
type QueryHandler[Param, Return any] struct {
name string
}
// NewQueryHandler declares the name and types for a query to a workflow.
func NewQueryHandler[Param, Return any](queryName string) *QueryHandler[Param, Return] {
panicIfNotStruct[Param]("NewQuery")
return &QueryHandler[Param, Return]{name: queryName}
}
// SetHandler should be called by a workflow to define how the query should be handled when sent to this workflow to execute.
func (q *QueryHandler[Param, Return]) SetHandler(ctx workflow.Context, fn func(Param) (Return, error)) {
// This can't error because the type is enforced by the signature of this function
workflow.SetQueryHandler(ctx, q.name, fn)
}
// Query executes the query and returns the response.
func (q *QueryHandler[Param, Return]) Query(ctx context.Context, temporalClient *Client, workflowID, runID string, p Param) (Return, error) {
var value Return
response, err := temporalClient.Client.QueryWorkflow(ctx, workflowID, runID, q.name, p)
if err != nil {
return value, err
}
err = response.Get(&value)
if err != nil {
return value, err
}
return value, nil
}