Skip to content

Commit

Permalink
Support ndc-spec 0.1.6 (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Aug 22, 2024
1 parent 318b480 commit 86c9f4b
Show file tree
Hide file tree
Showing 19 changed files with 1,085 additions and 634 deletions.
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 @@ -153,10 +153,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 @@ -456,7 +456,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 @@ -484,7 +484,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 @@ -15,7 +15,7 @@ import (
"github.com/rs/zerolog"
)

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

0 comments on commit 86c9f4b

Please sign in to comment.