github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go@v0.0.0-20240508172728-e960be013ca2/testdata/empty/expected/connector.go.tmpl (about) 1 // Code generated by github.com/hasura/ndc-sdk-go/codegen, DO NOT EDIT. 2 package main 3 4 import ( 5 "context" 6 _ "embed" 7 "fmt" 8 "log/slog" 9 10 11 "github.com/hasura/ndc-codegen-empty-test/types" 12 "github.com/hasura/ndc-sdk-go/connector" 13 "github.com/hasura/ndc-sdk-go/schema" 14 "github.com/hasura/ndc-sdk-go/utils" 15 "go.opentelemetry.io/otel/attribute" 16 "go.opentelemetry.io/otel/codes" 17 "go.opentelemetry.io/otel/trace" 18 ) 19 20 //go:embed schema.generated.json 21 var rawSchema []byte 22 var schemaResponse *schema.RawSchemaResponse 23 24 func init() { 25 var err error 26 schemaResponse, err = schema.NewRawSchemaResponse(rawSchema) 27 if err != nil { 28 panic(err) 29 } 30 } 31 32 // GetSchema gets the connector's schema. 33 func (c *Connector) GetSchema(ctx context.Context, configuration *types.Configuration, _ *types.State) (schema.SchemaResponseMarshaler, error) { 34 return schemaResponse, nil 35 } 36 37 // Query executes a query. 38 func (c *Connector) Query(ctx context.Context, configuration *types.Configuration, state *types.State, request *schema.QueryRequest) (schema.QueryResponse, error) { 39 valueField, err := utils.EvalFunctionSelectionFieldValue(request) 40 if err != nil { 41 return nil, schema.UnprocessableContentError(err.Error(), nil) 42 } 43 44 span := trace.SpanFromContext(ctx) 45 requestVars := request.Variables 46 varsLength := len(requestVars) 47 if varsLength == 0 { 48 requestVars = []schema.QueryRequestVariablesElem{make(schema.QueryRequestVariablesElem)} 49 varsLength = 1 50 } 51 52 rowSets := make([]schema.RowSet, varsLength) 53 for i, requestVar := range requestVars { 54 childSpan := span 55 childContext := ctx 56 if varsLength > 1 { 57 childContext, childSpan = state.Tracer.Start(ctx, fmt.Sprintf("execute_function_%d", i)) 58 defer childSpan.End() 59 } 60 61 result, err := execQuery(childContext, state, request, valueField, requestVar, childSpan) 62 if err != nil { 63 if varsLength > 1 { 64 childSpan.SetStatus(codes.Error, err.Error()) 65 } 66 return nil, err 67 } 68 rowSets[i] = schema.RowSet{ 69 Aggregates: schema.RowSetAggregates{}, 70 Rows: []map[string]any{ 71 { 72 "__value": result, 73 }, 74 }, 75 } 76 if varsLength > 1 { 77 childSpan.End() 78 } 79 } 80 81 return rowSets, nil 82 } 83 84 // Mutation executes a mutation. 85 func (c *Connector) Mutation(ctx context.Context, configuration *types.Configuration, state *types.State, request *schema.MutationRequest) (*schema.MutationResponse, error) { 86 operationLen := len(request.Operations) 87 operationResults := make([]schema.MutationOperationResults, operationLen) 88 span := trace.SpanFromContext(ctx) 89 90 for i, operation := range request.Operations { 91 childSpan := span 92 childContext := ctx 93 if operationLen > 1 { 94 childContext, childSpan = state.Tracer.Start(ctx, fmt.Sprintf("execute_operation_%d", i)) 95 defer childSpan.End() 96 } 97 childSpan.SetAttributes( 98 attribute.String("operation.type", string(operation.Type)), 99 attribute.String("operation.name", string(operation.Name)), 100 ) 101 102 switch operation.Type { 103 case schema.MutationOperationProcedure: 104 result, err := execProcedure(childContext, state, &operation, childSpan) 105 if err != nil { 106 if operationLen > 1 { 107 childSpan.SetStatus(codes.Error, err.Error()) 108 } 109 return nil, err 110 } 111 operationResults[i] = result 112 if operationLen > 1 { 113 childSpan.End() 114 } 115 default: 116 return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid operation type: %s", operation.Type), nil) 117 } 118 } 119 120 return &schema.MutationResponse{ 121 OperationResults: operationResults, 122 }, nil 123 } 124 125 func execQuery(ctx context.Context, state *types.State, request *schema.QueryRequest, queryFields schema.NestedField, variables map[string]any, span trace.Span) (any, error) { 126 logger := connector.GetLogger(ctx) 127 connector_addSpanEvent(span, logger, "validate_request", map[string]any{ 128 "variables": variables, 129 }) 130 switch request.Collection { 131 132 default: 133 return nil, schema.UnprocessableContentError(fmt.Sprintf("unsupported query: %s", request.Collection), nil) 134 } 135 } 136 137 func execProcedure(ctx context.Context, state *types.State, operation *schema.MutationOperation, span trace.Span) (schema.MutationOperationResults, error) { 138 logger := connector.GetLogger(ctx) 139 connector_addSpanEvent(span, logger, "validate_request", map[string]any{ 140 "operations_name": operation.Name, 141 }) 142 switch operation.Name { 143 144 default: 145 return nil, schema.UnprocessableContentError(fmt.Sprintf("unsupported procedure operation: %s", operation.Name), nil) 146 } 147 } 148 149 func connector_addSpanEvent(span trace.Span, logger *slog.Logger, name string, data map[string]any, options ...trace.EventOption) { 150 logger.Debug(name, slog.Any("data", data)) 151 attrs := utils.DebugJSONAttributes(data, connector_isDebug(logger)) 152 span.AddEvent(name, append(options, trace.WithAttributes(attrs...))...) 153 } 154 155 func connector_isDebug(logger *slog.Logger) bool { 156 return logger.Enabled(context.TODO(), slog.LevelDebug) 157 }