Skip to content

Commit

Permalink
add the nested field selection to command context
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Jan 6, 2025
1 parent ae15d34 commit 0aeafbe
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 19 deletions.
7 changes: 3 additions & 4 deletions cmd/hasura-ndc-go/command/internal/connector_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func (chb connectorHandlerBuilder) Render() {
bs.imports["github.com/hasura/ndc-sdk-go/connector"] = ""
bs.imports["github.com/hasura/ndc-sdk-go/schema"] = ""
bs.imports["go.opentelemetry.io/otel/trace"] = ""
if len(chb.Functions) > 0 || len(chb.Procedures) > 0 {
bs.imports[packageSDKUtils] = ""
}
bs.imports[packageSDKUtils] = ""

if chb.RawSchema.StateType != nil && bs.packagePath != chb.RawSchema.StateType.PackagePath {
bs.imports[chb.RawSchema.StateType.PackagePath] = ""
Expand Down Expand Up @@ -99,7 +97,7 @@ func (dch DataConnectorHandler) Query(ctx context.Context, state *`)
return nil, schema.UnprocessableContentError(err.Error(), nil)
}
result, err := dch.execQuery(ctx, state, request, queryFields, rawArgs)
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,6 +206,7 @@ func (dch DataConnectorHandler) Mutation(ctx context.Context, state *`)
_, _ = sb.WriteString(`, operation *schema.MutationOperation) (schema.MutationOperationResults, error) {
span := trace.SpanFromContext(ctx)
logger := connector.GetLogger(ctx)
ctx = context.WithValue(ctx, utils.CommandSelectionFieldKey, operation.Fields)
connector_addSpanEvent(span, logger, "validate_request", map[string]any{
"operations_name": operation.Name,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ func (dch DataConnectorHandler) Query(ctx context.Context, state *types.State, r
return nil, schema.UnprocessableContentError(err.Error(), nil)
}

result, err := dch.execQuery(ctx, state, request, queryFields, rawArgs)
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1371,6 +1371,7 @@ func (dch DataConnectorHandler) MutationExists(name string) bool {
func (dch DataConnectorHandler) Mutation(ctx context.Context, state *types.State, operation *schema.MutationOperation) (schema.MutationOperationResults, error) {
span := trace.SpanFromContext(ctx)
logger := connector.GetLogger(ctx)
ctx = context.WithValue(ctx, utils.CommandSelectionFieldKey, operation.Fields)
connector_addSpanEvent(span, logger, "validate_request", map[string]any{
"operations_name": operation.Name,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.21
require (
github.com/google/uuid v1.6.0
github.com/hasura/ndc-sdk-go v1.4.1
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/sync v0.10.0
)

require (
Expand All @@ -24,7 +27,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/contrib/bridges/otelslog v0.4.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.29.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
Expand All @@ -38,7 +40,6 @@ require (
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.5.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module github.com/hasura/ndc-codegen-empty-test

go 1.21

require github.com/hasura/ndc-sdk-go v1.4.1
require (
github.com/hasura/ndc-sdk-go v1.4.1
go.opentelemetry.io/otel v1.29.0
golang.org/x/sync v0.10.0
)

require (
github.com/alecthomas/kong v1.6.0 // indirect
Expand All @@ -22,7 +26,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/contrib/bridges/otelslog v0.4.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.29.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ module github.com/hasura/ndc-codegen-function-only-test

go 1.21

require github.com/hasura/ndc-sdk-go v1.6.2
require (
github.com/hasura/ndc-sdk-go v1.6.2
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/sync v0.10.0
)

require (
github.com/alecthomas/kong v1.6.0 // indirect
Expand All @@ -22,7 +27,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/contrib/bridges/otelslog v0.4.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.29.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
Expand All @@ -36,7 +40,6 @@ require (
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.5.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ func (dch DataConnectorHandler) Query(ctx context.Context, state *types.State, r
return nil, schema.UnprocessableContentError(err.Error(), nil)
}

result, err := dch.execQuery(ctx, state, request, queryFields, rawArgs)
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1032,6 +1032,7 @@ func (dch DataConnectorHandler) MutationExists(name string) bool {
func (dch DataConnectorHandler) Mutation(ctx context.Context, state *types.State, operation *schema.MutationOperation) (schema.MutationOperationResults, error) {
span := trace.SpanFromContext(ctx)
logger := connector.GetLogger(ctx)
ctx = context.WithValue(ctx, utils.CommandSelectionFieldKey, operation.Fields)
connector_addSpanEvent(span, logger, "validate_request", map[string]any{
"operations_name": operation.Name,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.21
require (
github.com/google/uuid v1.6.0
github.com/hasura/ndc-sdk-go v1.6.3
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/sync v0.10.0
)

require (
Expand All @@ -24,7 +27,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/contrib/bridges/otelslog v0.4.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.29.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
Expand All @@ -38,7 +40,6 @@ require (
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.5.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (dch DataConnectorHandler) Query(ctx context.Context, state *types.State, r
return nil, schema.UnprocessableContentError(err.Error(), nil)
}

result, err := dch.execQuery(ctx, state, request, queryFields, rawArgs)
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs)
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions example/codegen/functions/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package functions
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -63,6 +64,11 @@ type CreateAuthorResult struct {

// ProcedureCreateAuthor creates an author
func ProcedureCreateAuthor(ctx context.Context, state *types.State, arguments *CreateAuthorArguments) (*CreateAuthorResult, error) {
selection := utils.CommandSelectionFieldFromContext(ctx)
if len(selection) == 0 {
return nil, errors.New("expected not-null selection field, got null")
}

return &CreateAuthorResult{
ID: 1,
Name: arguments.Name,
Expand Down Expand Up @@ -92,6 +98,11 @@ func FunctionGetInts(ctx context.Context, state *types.State) ([]*int, error) {
}

func FunctionGetTypes(ctx context.Context, state *types.State, arguments *arguments.GetTypesArguments) (*arguments.GetTypesArguments, error) {
selection := utils.CommandSelectionFieldFromContext(ctx)
if len(selection) == 0 {
return nil, errors.New("expected not-null selection field, got null")
}

return arguments, nil
}

Expand Down
3 changes: 2 additions & 1 deletion example/codegen/functions/types.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/codegen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/hasura/ndc-sdk-go v1.6.0
github.com/hasura/ndc-sdk-go v1.6.4
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/sync v0.10.0
Expand Down
2 changes: 1 addition & 1 deletion example/codegen/types/types.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions utils/connector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"errors"
"fmt"
"reflect"
Expand All @@ -10,6 +11,9 @@ import (

const (
errFunctionValueFieldRequired = "__value field is required in query function type"

// CommandSelectionFieldKey the context key for the nested selection field in command.
CommandSelectionFieldKey string = "ndc-command-selection-field"
)

var ErrHandlerNotfound = errors.New("connector handler not found")
Expand Down Expand Up @@ -322,3 +326,15 @@ func MergeSchemas(schemas ...*schema.SchemaResponse) (*schema.SchemaResponse, []
}
return &result, errs
}

// CommandSelectionFieldFromContext gets the command's nested selection field from context.
func CommandSelectionFieldFromContext(ctx context.Context) schema.NestedField {
value := ctx.Value(CommandSelectionFieldKey)
if value != nil {
if selection, ok := value.(schema.NestedField); ok {
return selection
}
}

return nil
}

0 comments on commit 0aeafbe

Please sign in to comment.