-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnector.generated.go
280 lines (239 loc) · 9.25 KB
/
connector.generated.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Code generated by github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go, DO NOT EDIT.
package main
import (
"context"
"fmt"
"os"
"strconv"
"sync"
"encoding/json"
"github.com/hasura/ndc-codegen-example/functions"
"github.com/hasura/ndc-codegen-example/types"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"golang.org/x/sync/errgroup"
)
var loadGlobalEnvOnce sync.Once
var schemaResponse *schema.RawSchemaResponse
var connectorQueryHandlers = []ConnectorQueryHandler{functions.DataConnectorHandler{}}
var connectorMutationHandlers = []ConnectorMutationHandler{functions.DataConnectorHandler{}}
// ConnectorQueryHandler abstracts the connector query handler
type ConnectorQueryHandler interface {
Query(ctx context.Context, state *types.State, request *schema.QueryRequest, arguments map[string]any) (*schema.RowSet, error)
}
// ConnectorMutationHandler abstracts the connector mutation handler
type ConnectorMutationHandler interface {
Mutation(ctx context.Context, state *types.State, request *schema.MutationOperation) (schema.MutationOperationResults, error)
}
func init() {
rawSchema, err := json.Marshal(GetConnectorSchema())
if err != nil {
panic(err)
}
schemaResponse, err = schema.NewRawSchemaResponse(rawSchema)
if err != nil {
panic(err)
}
}
// GetSchema gets the connector's schema.
func (c *Connector) GetSchema(ctx context.Context, configuration *types.Configuration, _ *types.State) (schema.SchemaResponseMarshaler, error) {
return schemaResponse, nil
}
// Query executes a query.
func (c *Connector) Query(ctx context.Context, configuration *types.Configuration, state *types.State, request *schema.QueryRequest) (schema.QueryResponse, error) {
if len(connectorQueryHandlers) == 0 {
return nil, schema.UnprocessableContentError(fmt.Sprintf("unsupported query: %s", request.Collection), nil)
}
requestVars := request.Variables
if len(requestVars) == 0 {
requestVars = []schema.QueryRequestVariablesElem{make(schema.QueryRequestVariablesElem)}
}
concurrencyLimit := getGlobalEnvironments().GetQueryConcurrencyLimitByName(request.Collection)
if concurrencyLimit <= 1 || len(request.Variables) <= 1 {
return c.execQuerySync(ctx, state, request, requestVars)
}
return c.execQueryAsync(ctx, state, request, requestVars, concurrencyLimit)
}
func (c *Connector) execQuerySync(ctx context.Context, state *types.State, request *schema.QueryRequest, requestVars []schema.QueryRequestVariablesElem) (schema.QueryResponse, error) {
rowSets := make([]schema.RowSet, len(requestVars))
for i, requestVar := range requestVars {
result, err := c.execQuery(ctx, state, request, requestVar, i)
if err != nil {
return nil, err
}
rowSets[i] = *result
}
return rowSets, nil
}
func (c *Connector) execQueryAsync(ctx context.Context, state *types.State, request *schema.QueryRequest, requestVars []schema.QueryRequestVariablesElem, concurrencyLimit int) (schema.QueryResponse, error) {
rowSets := make([]schema.RowSet, len(requestVars))
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(concurrencyLimit)
for i, requestVar := range requestVars {
func(index int, vars schema.QueryRequestVariablesElem) {
eg.Go(func() error {
result, err := c.execQuery(ctx, state, request, vars, index)
if err != nil {
return err
}
rowSets[index] = *result
return nil
})
}(i, requestVar)
}
if err := eg.Wait(); err != nil {
return nil, err
}
return rowSets, nil
}
func (c *Connector) execQuery(ctx context.Context, state *types.State, request *schema.QueryRequest, variables map[string]any, index int) (*schema.RowSet, error) {
ctx, span := state.Tracer.Start(ctx, fmt.Sprintf("Execute Function %d", index))
defer span.End()
rawArgs, err := utils.ResolveArgumentVariables(request.Arguments, variables)
if err != nil {
span.SetStatus(codes.Error, "failed to resolve argument variables")
span.RecordError(err)
return nil, schema.UnprocessableContentError("failed to resolve argument variables", map[string]any{
"cause": err.Error(),
})
}
for _, handler := range connectorQueryHandlers {
result, err := handler.Query(ctx, state, request, rawArgs)
if err == nil {
return result, nil
}
if err != utils.ErrHandlerNotfound {
span.SetStatus(codes.Error, fmt.Sprintf("failed to execute function %d", index))
span.RecordError(err)
return nil, err
}
}
errorMsg := fmt.Sprintf("unsupported query: %s", request.Collection)
span.SetStatus(codes.Error, errorMsg)
return nil, schema.UnprocessableContentError(errorMsg, nil)
}
// Mutation executes a mutation.
func (c *Connector) Mutation(ctx context.Context, configuration *types.Configuration, state *types.State, request *schema.MutationRequest) (*schema.MutationResponse, error) {
if len(connectorMutationHandlers) == 0 {
return nil, schema.UnprocessableContentError("unsupported mutation", nil)
}
concurrencyLimit := getGlobalEnvironments().mutationConcurrencyLimit
if len(request.Operations) <= 1 || concurrencyLimit <= 1 {
return c.execMutationSync(ctx, state, request)
}
return c.execMutationAsync(ctx, state, request, concurrencyLimit)
}
func (c *Connector) execMutationSync(ctx context.Context, state *types.State, request *schema.MutationRequest) (*schema.MutationResponse, error) {
operationResults := make([]schema.MutationOperationResults, len(request.Operations))
for i, operation := range request.Operations {
result, err := c.execMutation(ctx, state, operation, i)
if err != nil {
return nil, err
}
operationResults[i] = result
}
return &schema.MutationResponse{
OperationResults: operationResults,
}, nil
}
func (c *Connector) execMutationAsync(ctx context.Context, state *types.State, request *schema.MutationRequest, concurrencyLimit int) (*schema.MutationResponse, error) {
operationResults := make([]schema.MutationOperationResults, len(request.Operations))
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(concurrencyLimit)
for i, operation := range request.Operations {
func(index int, op schema.MutationOperation) {
eg.Go(func() error {
result, err := c.execMutation(ctx, state, op, index)
if err != nil {
return err
}
operationResults[index] = result
return nil
})
}(i, operation)
}
if err := eg.Wait(); err != nil {
return nil, err
}
return &schema.MutationResponse{
OperationResults: operationResults,
}, nil
}
func (c *Connector) execMutation(ctx context.Context, state *types.State, operation schema.MutationOperation, index int) (schema.MutationOperationResults, error) {
ctx, span := state.Tracer.Start(ctx, fmt.Sprintf("Execute Procedure %d", index))
defer span.End()
span.SetAttributes(
attribute.String("operation.type", string(operation.Type)),
attribute.String("operation.name", string(operation.Name)),
)
switch operation.Type {
case schema.MutationOperationProcedure:
result, err := c.execProcedure(ctx, state, &operation)
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf("failed to execute procedure %d", index))
span.RecordError(err)
return nil, err
}
return result, nil
default:
errorMsg := fmt.Sprintf("invalid operation type: %s", operation.Type)
span.SetStatus(codes.Error, errorMsg)
return nil, schema.UnprocessableContentError(errorMsg, nil)
}
}
func (c *Connector) execProcedure(ctx context.Context, state *types.State, operation *schema.MutationOperation) (schema.MutationOperationResults, error) {
for _, handler := range connectorMutationHandlers {
result, err := handler.Mutation(ctx, state, operation)
if err == nil {
return result, nil
}
if err != utils.ErrHandlerNotfound {
return nil, err
}
}
return nil, schema.UnprocessableContentError(fmt.Sprintf("unsupported procedure operation: %s", operation.Name), nil)
}
type globalEnvironments struct {
queryConcurrency map[string]int
queryConcurrencyLimit int
mutationConcurrencyLimit int
}
// GetQueryConcurrencyLimitByName gets the concurrency limit of the query by name
func (ge globalEnvironments) GetQueryConcurrencyLimitByName(name string) int {
if len(ge.queryConcurrency) > 0 {
if limit, ok := ge.queryConcurrency[name]; ok {
return limit
}
}
return ge.queryConcurrencyLimit
}
var _globalEnvironments = globalEnvironments{}
func initGlobalEnvironments() {
rawQueryConcurrencyLimit := os.Getenv("QUERY_CONCURRENCY_LIMIT")
if rawQueryConcurrencyLimit != "" {
limit, err := strconv.ParseInt(rawQueryConcurrencyLimit, 10, 64)
if err != nil {
panic(fmt.Sprintf("QUERY_CONCURRENCY_LIMIT: invalid integer <%s>", rawQueryConcurrencyLimit))
}
_globalEnvironments.queryConcurrencyLimit = int(limit)
}
rawMutationConcurrencyLimit := os.Getenv("MUTATION_CONCURRENCY_LIMIT")
if rawMutationConcurrencyLimit != "" {
limit, err := strconv.ParseInt(rawMutationConcurrencyLimit, 10, 64)
if err != nil {
panic(fmt.Sprintf("MUTATION_CONCURRENCY_LIMIT: invalid integer <%s>", rawMutationConcurrencyLimit))
}
_globalEnvironments.mutationConcurrencyLimit = int(limit)
}
queryConcurrency, err := utils.ParseIntMapFromString(os.Getenv("QUERY_CONCURRENCY"))
if err != nil {
panic(fmt.Sprintf("QUERY_CONCURRENCY: %s", err))
}
_globalEnvironments.queryConcurrency = queryConcurrency
}
func getGlobalEnvironments() *globalEnvironments {
loadGlobalEnvOnce.Do(initGlobalEnvironments)
return &_globalEnvironments
}