Skip to content
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

cli: skip validating scalar result types #165

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions cmd/hasura-ndc-go/command/internal/connector_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ func (dch DataConnectorHandler) execQuery(ctx context.Context, state *`)
functionKeys[i] = fn.Name
op := OperationInfo(fn)
resultType, isNullable := unwrapNullableType(op.ResultType.Type)
chb.writeOperationValidation(sb, &op, "queryFields", resultType)
schemaName := resultType.SchemaName(false)
_, isScalar := chb.RawSchema.Scalars[schemaName]

chb.writeOperationValidation(sb, &op, "queryFields", resultType, isScalar)

var argumentParamStr string
if fn.ArgumentsType != nil {
Expand Down Expand Up @@ -154,21 +157,21 @@ func (dch DataConnectorHandler) execQuery(ctx context.Context, state *`)
argumentParamStr = ", &args"
}

switch t := resultType.(type) {
if isScalar {
sb.WriteString(fmt.Sprintf("\n return %s(ctx, state%s)\n", fn.OriginName, argumentParamStr))
continue
}
switch resultType.(type) {
case *ArrayType:
chb.writeOperationResult(sb, fn.OriginName, OperationFunction, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnArrayIntoSlice(selection, rawResult)")
writeErrorCheck(sb, 2, 4)
sb.WriteString(" return result, nil\n")
case *NamedType:
if _, ok := chb.RawSchema.Scalars[t.NativeType.SchemaName]; ok {
sb.WriteString(fmt.Sprintf("\n return %s(ctx, state%s)\n", fn.OriginName, argumentParamStr))
} else {
chb.writeOperationResult(sb, fn.OriginName, OperationFunction, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnObject(selection, rawResult)")
writeErrorCheck(sb, 2, 4)
sb.WriteString(" return result, nil\n")
}
chb.writeOperationResult(sb, fn.OriginName, OperationFunction, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnObject(selection, rawResult)")
writeErrorCheck(sb, 2, 4)
sb.WriteString(" return result, nil\n")
}
}

Expand Down Expand Up @@ -213,7 +216,9 @@ func (dch DataConnectorHandler) Mutation(ctx context.Context, state *`)
procedureKeys[i] = fn.Name
op := OperationInfo(fn)
resultType, isNullable := unwrapNullableType(op.ResultType.Type)
chb.writeOperationValidation(sb, &op, "operation.Fields", resultType)
schemaName := resultType.SchemaName(false)
_, isScalar := chb.RawSchema.Scalars[schemaName]
chb.writeOperationValidation(sb, &op, "operation.Fields", resultType, isScalar)

var argumentParamStr string
if fn.ArgumentsType != nil {
Expand All @@ -235,15 +240,15 @@ func (dch DataConnectorHandler) Mutation(ctx context.Context, state *`)

sb.WriteString("\n span.AddEvent(\"execute_procedure\")")

switch t := resultType.(type) {
case *ArrayType:
chb.writeOperationResult(sb, fn.OriginName, OperationProcedure, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnArrayIntoSlice(selection, rawResult)\n")
writeErrorCheck(sb, 2, 4)
case *NamedType:
if _, ok := chb.RawSchema.Scalars[t.NativeType.SchemaName]; ok {
chb.writeOperationExecution(sb, fn.OriginName, argumentParamStr, "result")
} else {
if isScalar {
chb.writeOperationExecution(sb, fn.OriginName, argumentParamStr, "result")
} else {
switch resultType.(type) {
case *ArrayType:
chb.writeOperationResult(sb, fn.OriginName, OperationProcedure, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnArrayIntoSlice(selection, rawResult)\n")
writeErrorCheck(sb, 2, 4)
case *NamedType:
chb.writeOperationResult(sb, fn.OriginName, OperationProcedure, argumentParamStr, isNullable)
sb.WriteString("\n result, err := utils.EvalNestedColumnObject(selection, rawResult)\n")
writeErrorCheck(sb, 2, 4)
Expand All @@ -261,12 +266,21 @@ func (dch DataConnectorHandler) Mutation(ctx context.Context, state *`)
chb.writeOperationNameEnums(sb, procedureEnumsName, procedureKeys)
}

func (chb connectorHandlerBuilder) writeOperationValidation(sb *strings.Builder, fn *OperationInfo, selector string, resultType Type) {
func (chb connectorHandlerBuilder) writeOperationValidation(sb *strings.Builder, fn *OperationInfo, selector string, resultType Type, isScalar bool) {
_, _ = sb.WriteString("\n case \"")
_, _ = sb.WriteString(fn.Name)
_, _ = sb.WriteString("\":\n")

switch t := resultType.(type) {
if isScalar {
sb.WriteString("\n if len(")
sb.WriteString(selector)
sb.WriteString(`) > 0 {
return nil, schema.UnprocessableContentError("cannot evaluate selection fields for scalar", nil)
}`)
return
}

switch resultType.(type) {
case *ArrayType:
sb.WriteString("\n selection, err := ")
sb.WriteString(selector)
Expand All @@ -277,15 +291,6 @@ func (chb connectorHandlerBuilder) writeOperationValidation(sb *strings.Builder,
})
}`)
case *NamedType:
if _, ok := chb.RawSchema.Scalars[t.NativeType.SchemaName]; ok {
sb.WriteString("\n if len(")
sb.WriteString(selector)
sb.WriteString(`) > 0 {
return nil, schema.UnprocessableContentError("cannot evaluate selection fields for scalar", nil)
}`)
return
}

sb.WriteString("\n selection, err := ")
sb.WriteString(selector)
sb.WriteString(`.AsObject()
Expand Down
9 changes: 8 additions & 1 deletion example/codegen/functions/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/hasura/ndc-codegen-example/types"
"github.com/hasura/ndc-codegen-example/types/arguments"
"github.com/hasura/ndc-sdk-go/utils"
)

// A foo scalar
Expand Down Expand Up @@ -85,6 +86,11 @@ func FunctionGetBool(ctx context.Context, state *types.State) (bool, error) {
return true, nil
}

// FunctionGetInts return a slice of scalar ints
func FunctionGetInts(ctx context.Context, state *types.State) ([]*int, error) {
return []*int{utils.ToPtr(1), utils.ToPtr(2), utils.ToPtr(3)}, nil
}

func FunctionGetTypes(ctx context.Context, state *types.State, arguments *arguments.GetTypesArguments) (*arguments.GetTypesArguments, error) {
return arguments, nil
}
Expand Down Expand Up @@ -143,7 +149,8 @@ func FunctionGetCustomHeaders(ctx context.Context, state *types.State, arguments
if arguments.Headers == nil {
arguments.Headers = make(map[string]string)
}
arguments.Headers["X-Test-ResponseHeader"] = "I set this in the code"

arguments.Headers["X-Test-ResponseHeader"] = arguments.Input.Name
result := HelloResult{}
if arguments.Input != nil {
result.Text = types.Text(arguments.Input.Name)
Expand Down
9 changes: 8 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.5.1
github.com/hasura/ndc-sdk-go v1.6.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
golang.org/x/sync v0.8.0
Expand Down
6 changes: 6 additions & 0 deletions example/codegen/schema.generated.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"text": "oK022ZdXnL"
},
"headers": {
"X-Test-ResponseHeader": "I set this in the code",
"X-Test-ResponseHeader": "oK022ZdXnL",
"foo": "bar"
}
}
Expand Down
9 changes: 9 additions & 0 deletions example/codegen/testdata/query/getInts/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"rows": [
{
"__value": [1, 2, 3]
}
]
}
]
13 changes: 13 additions & 0 deletions example/codegen/testdata/query/getInts/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"arguments": {},
"collection": "getInts",
"collection_relationships": {},
"query": {
"fields": {
"__value": {
"column": "__value",
"type": "column"
}
}
}
}