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

Support ndc-spec 0.1.6 (#137) #138

Merged
merged 1 commit into from
Aug 22, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All functions of the Connector interface are analogous to their Rust counterpart

## Features

The SDK fully supports [NDC Specification v0.1.5](https://hasura.github.io/ndc-spec/specification/changelog.html#015) and [Connector Deployment spec](https://github.com/hasura/ndc-hub/blob/main/rfcs/0000-deployment.md) with following features:
The SDK fully supports [NDC Specification v0.1.6](https://hasura.github.io/ndc-spec/specification/changelog.html#016) and [Connector Deployment spec](https://github.com/hasura/ndc-hub/blob/main/rfcs/0000-deployment.md) with following features:

- Connector HTTP server
- Authentication
Expand Down
5 changes: 1 addition & 4 deletions cmd/hasura-ndc-go/command/test_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ func (cmd *genTestSnapshotsCommand) genQueryArguments(arguments schema.FunctionI
if err != nil {
return nil, err
}
result[key] = schema.Argument{
Type: schema.ArgumentTypeLiteral,
Value: value,
}
result[key] = schema.NewArgumentLiteral(value).Encode()
}
return result, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/templates/new/connector.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var connectorCapabilities = schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Variables: schema.LeafCapability{},
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/testdata/basic/source/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var connectorCapabilities = schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Variables: schema.LeafCapability{},
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/testdata/empty/source/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var connectorCapabilities = schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Variables: schema.LeafCapability{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var connectorCapabilities = schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Variables: schema.LeafCapability{},
Expand Down
4 changes: 2 additions & 2 deletions connector/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func TestServerConnector(t *testing.T) {
})

t.Run("POST /query/explain - json decode failure", func(t *testing.T) {
res, err := httpPostJSON(fmt.Sprintf("%s/query/explain", httpServer.URL), schema.QueryRequest{})
res, err := httpPostJSON(fmt.Sprintf("%s/query/explain", httpServer.URL), map[string]any{})
if err != nil {
t.Errorf("expected no error, got %s", err)
t.FailNow()
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestServerConnector(t *testing.T) {
})

t.Run("POST /mutation/explain - json decode failure", func(t *testing.T) {
res, err := httpPostJSON(fmt.Sprintf("%s/mutation/explain", httpServer.URL), schema.MutationRequest{})
res, err := httpPostJSON(fmt.Sprintf("%s/mutation/explain", httpServer.URL), map[string]any{})
if err != nil {
t.Errorf("expected no error, got %s", err)
t.FailNow()
Expand Down
2 changes: 1 addition & 1 deletion example/codegen/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var connectorCapabilities = schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Variables: schema.LeafCapability{},
Expand Down
2 changes: 1 addition & 1 deletion example/codegen/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cd "$(dirname "${BASH_SOURCE[0]}")"
mkdir -p ./dist

if [ ! -f ./dist/ndc-test ]; then
curl -L https://github.com/hasura/ndc-spec/releases/download/v0.1.5/ndc-test-x86_64-unknown-linux-gnu -o ./dist/ndc-test
curl -L https://github.com/hasura/ndc-spec/releases/download/v0.1.6/ndc-test-x86_64-unknown-linux-gnu -o ./dist/ndc-test
chmod +x ./dist/ndc-test
fi

Expand Down
2 changes: 1 addition & 1 deletion example/codegen/testdata/capabilities
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.5",
"version": "0.1.6",
"capabilities": {
"query": {
"variables": {}
Expand Down
88 changes: 27 additions & 61 deletions example/reference/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (mc *Connector) HealthCheck(ctx context.Context, configuration *Configurati

func (mc *Connector) GetCapabilities(configuration *Configuration) schema.CapabilitiesResponseMarshaler {
return &schema.CapabilitiesResponse{
Version: "0.1.5",
Version: "0.1.6",
Capabilities: schema.Capabilities{
Query: schema.QueryCapabilities{
Aggregates: schema.LeafCapability{},
Expand Down Expand Up @@ -514,17 +514,9 @@ func executeQueryWithVariables(
variables map[string]any,
state *State,
) (*schema.RowSet, error) {
argumentValues := make(map[string]schema.Argument)

for argumentName, argument := range arguments {
argumentValue, err := evalArgument(variables, &argument)
if err != nil {
return nil, err
}
argumentValues[argumentName] = schema.Argument{
Type: schema.ArgumentTypeLiteral,
Value: argumentValue,
}
argumentValues, err := utils.ResolveArgumentVariables(arguments, variables)
if err != nil {
return nil, err
}

coll, err := getCollectionByName(collection, argumentValues, state)
Expand Down Expand Up @@ -924,16 +916,13 @@ func evalInCollection(
source := []map[string]any{item}
return evalPathElement(collectionRelationships, variables, state, &relationship, inCol.Arguments, source, nil)
case *schema.ExistsInCollectionUnrelated:
arguments := make(map[string]schema.Argument)
arguments := make(map[string]any)
for key, relArg := range inCol.Arguments {
argValue, err := evalRelationshipArgument(variables, item, &relArg)
if err != nil {
return nil, err
}
arguments[key] = schema.Argument{
Type: schema.ArgumentTypeLiteral,
Value: argValue,
}
arguments[key] = argValue
}
return getCollectionByName(inCol.Collection, arguments, state)
default:
Expand Down Expand Up @@ -1046,7 +1035,7 @@ func evalPathElement(
source []map[string]any,
predicate schema.Expression,
) ([]map[string]any, error) {
allArguments := make(map[string]schema.Argument)
allArguments := make(map[string]any)
var matchingRows []map[string]any

// Note: Join strategy
Expand All @@ -1072,10 +1061,7 @@ func evalPathElement(
if err != nil {
return nil, err
}
allArguments[argName] = schema.Argument{
Type: schema.ArgumentTypeLiteral,
Value: relValue,
}
allArguments[argName] = relValue
}
for argName, arg := range arguments {
if _, ok := allArguments[argName]; ok {
Expand All @@ -1085,10 +1071,7 @@ func evalPathElement(
if err != nil {
return nil, err
}
allArguments[argName] = schema.Argument{
Type: schema.ArgumentTypeLiteral,
Value: relValue,
}
allArguments[argName] = relValue
}

targetRows, err := getCollectionByName(relationship.TargetCollection, allArguments, state)
Expand Down Expand Up @@ -1122,27 +1105,28 @@ func evalPathElement(
}

func evalRelationshipArgument(variables map[string]any, row map[string]any, argument *schema.RelationshipArgument) (any, error) {
switch argument.Type {
case schema.RelationshipArgumentTypeColumn:
value, ok := row[argument.Name]
argT, err := argument.InterfaceT()
switch arg := argT.(type) {
case *schema.RelationshipArgumentColumn:
value, ok := row[arg.Name]
if !ok {
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid column name: %s", argument.Name), nil)
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid column name: %s", arg.Name), nil)
}
return value, nil
case schema.RelationshipArgumentTypeLiteral:
return argument.Value, nil
case schema.RelationshipArgumentTypeVariable:
variable, ok := variables[argument.Name]
case *schema.RelationshipArgumentLiteral:
return arg.Value, nil
case *schema.RelationshipArgumentVariable:
variable, ok := variables[arg.Name]
if !ok {
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid variable name: %s", argument.Name), nil)
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid variable name: %s", arg.Name), nil)
}
return variable, nil
default:
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid argument type: %s", argument.Type), nil)
return nil, schema.UnprocessableContentError(err.Error(), nil)
}
}

func getCollectionByName(collectionName string, arguments schema.QueryRequestArguments, state *State) ([]map[string]any, error) {
func getCollectionByName(collectionName string, arguments map[string]any, state *State) ([]map[string]any, error) {
var rows []map[string]any
switch collectionName {
// function
Expand Down Expand Up @@ -1189,21 +1173,18 @@ func getCollectionByName(collectionName string, arguments schema.QueryRequestArg
rows = append(rows, row)
}
case "articles_by_author":
authorIdArg, ok := arguments["author_id"]
authorId, ok := arguments["author_id"]
if !ok {
return nil, schema.UnprocessableContentError("missing argument author_id", nil)
}

for _, row := range state.Articles {
switch authorIdArg.Type {
case schema.ArgumentTypeLiteral:
if fmt.Sprint(row.AuthorID) == fmt.Sprint(authorIdArg.Value) {
r, err := utils.EncodeObject(row)
if err != nil {
return nil, err
}
rows = append(rows, r)
if fmt.Sprint(row.AuthorID) == fmt.Sprint(authorId) {
r, err := utils.EncodeObject(row)
if err != nil {
return nil, err
}
rows = append(rows, r)
}
}
default:
Expand Down Expand Up @@ -1454,21 +1435,6 @@ func evalExpression(
}
}

func evalArgument(variables map[string]any, argument *schema.Argument) (any, error) {
switch argument.Type {
case schema.ArgumentTypeVariable:
value, ok := variables[argument.Name]
if !ok {
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid variable name: %s", argument.Name), nil)
}
return value, nil
case schema.ArgumentTypeLiteral:
return argument.Value, nil
default:
return nil, schema.UnprocessableContentError(fmt.Sprintf("invalid argument type: %s", argument.Type), nil)
}
}

func evalColumnMapping(relationship *schema.Relationship, srcRow map[string]any, target map[string]any) (bool, error) {
for srcColumn, targetColumn := range relationship.ColumnMapping {
srcValue, ok := srcRow[srcColumn]
Expand Down
2 changes: 1 addition & 1 deletion example/reference/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hasura/ndc-sdk-go/schema"
)

const test_SpecVersion = "v0.1.5"
const test_SpecVersion = "v0.1.6"

func createTestServer(t *testing.T) *connector.Server[Configuration, State] {
server, err := connector.NewServer[Configuration, State](&Connector{}, &connector.ServerOptions{
Expand Down
2 changes: 1 addition & 1 deletion example/reference/testdata/capabilities
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.5",
"version": "0.1.6",
"capabilities": {
"query": {
"aggregates": {},
Expand Down
Loading