Skip to content

Commit

Permalink
handle null gql inputs correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Sep 16, 2024
1 parent ea3a74f commit c4b2456
Show file tree
Hide file tree
Showing 11 changed files with 1,292 additions and 158 deletions.
3 changes: 3 additions & 0 deletions internal/planner/mapper/targetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func filterObjectToMap(mapping *core.DocumentMapping, obj map[connor.FilterKey]a
return nil
}
for k, v := range obj {
if v == nil {
continue // ignore nil filter object values
}
switch keyType := k.(type) {
case *PropertyIndex:
subObj := v.(map[connor.FilterKey]any)
Expand Down
71 changes: 52 additions & 19 deletions internal/request/graphql/parser/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,61 @@ func parseCommitSelect(
arguments := gql.GetArgumentValues(fieldDef.Args, field.Arguments, exe.VariableValues)

for _, argument := range field.Arguments {
prop := argument.Name.Value
if prop == request.DocIDArgName {
commit.DocID = immutable.Some(arguments[prop].(string))
} else if prop == request.Cid {
commit.CID = immutable.Some(arguments[prop].(string))
} else if prop == request.FieldIDName {
commit.FieldID = immutable.Some(arguments[prop].(string))
} else if prop == request.OrderClause {
conditions, err := ParseConditionsInOrder(argument.Value.(*ast.ObjectValue), arguments[prop].(map[string]any))
name := argument.Name.Value
value := arguments[name]

Check warning on line 39 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L38-L39

Added lines #L38 - L39 were not covered by tests

switch name {
case request.DocIDArgName:
if v, ok := value.(string); ok {
commit.DocID = immutable.Some(v)

Check warning on line 44 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L41-L44

Added lines #L41 - L44 were not covered by tests
}

case request.Cid:
if v, ok := value.(string); ok {
commit.CID = immutable.Some(v)

Check warning on line 49 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L47-L49

Added lines #L47 - L49 were not covered by tests
}

case request.FieldIDName:
if v, ok := value.(string); ok {
commit.FieldID = immutable.Some(v)

Check warning on line 54 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}

case request.OrderClause:
v, ok := arguments[name].(map[string]any)
if !ok {
continue // value is nil

Check warning on line 60 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}
conditions, err := ParseConditionsInOrder(argument.Value.(*ast.ObjectValue), v)

Check warning on line 62 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L62

Added line #L62 was not covered by tests
if err != nil {
return nil, err
}
commit.OrderBy = immutable.Some(request.OrderBy{
Conditions: conditions,
})
} else if prop == request.LimitClause {
commit.Limit = immutable.Some(uint64(arguments[prop].(int32)))
} else if prop == request.OffsetClause {
commit.Offset = immutable.Some(uint64(arguments[prop].(int32)))
} else if prop == request.DepthClause {
commit.Depth = immutable.Some(uint64(arguments[prop].(int32)))
} else if prop == request.GroupByClause {
fields := []string{}
for _, v := range arguments[prop].([]any) {
fields = append(fields, v.(string))

case request.LimitClause:
if v, ok := value.(int32); ok {
immutable.Some(uint64(v))

Check warning on line 72 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L70-L72

Added lines #L70 - L72 were not covered by tests
}

case request.OffsetClause:
if v, ok := value.(int32); ok {
immutable.Some(uint64(v))

Check warning on line 77 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L75-L77

Added lines #L75 - L77 were not covered by tests
}

case request.DepthClause:
if v, ok := value.(int32); ok {
immutable.Some(uint64(v))

Check warning on line 82 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L80-L82

Added lines #L80 - L82 were not covered by tests
}

case request.GroupByClause:
v, ok := arguments[name].([]any)
if !ok {
continue // value is nil

Check warning on line 88 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L85-L88

Added lines #L85 - L88 were not covered by tests
}
fields := make([]string, len(v))
for i, c := range v {
fields[i] = c.(string)

Check warning on line 92 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L90-L92

Added lines #L90 - L92 were not covered by tests
}
commit.GroupBy = immutable.Some(request.GroupBy{
Fields: fields,
Expand Down Expand Up @@ -91,6 +121,9 @@ func parseCommitSelect(
}

commit.Fields, err = parseSelectFields(exe, fieldObject, field.SelectionSet)
if err != nil {
return nil, err

Check warning on line 125 in internal/request/graphql/parser/commit.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/commit.go#L124-L125

Added lines #L124 - L125 were not covered by tests
}

return commit, err
}
29 changes: 19 additions & 10 deletions internal/request/graphql/parser/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,9 @@ func ParseConditionsInOrder(stmt *ast.ObjectValue, args map[string]any) ([]reque
for _, field := range stmt.Fields {
switch v := args[field.Name.Value].(type) {
case int: // base direction parsed (hopefully, check NameToOrderDirection)
var dir request.OrderDirection
switch v {
case 0:
dir = request.ASC

case 1:
dir = request.DESC

default:
return nil, ErrInvalidOrderDirection
dir, err := parseOrderDirection(v)
if err != nil {
return nil, err

Check warning on line 83 in internal/request/graphql/parser/filter.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/filter.go#L81-L83

Added lines #L81 - L83 were not covered by tests
}
conditions = append(conditions, request.OrderCondition{
Fields: []string{field.Name.Value},
Expand All @@ -109,6 +102,9 @@ func ParseConditionsInOrder(stmt *ast.ObjectValue, args map[string]any) ([]reque
conditions = append(conditions, cond)
}

case nil:
continue // ignore nil filter input

Check warning on line 106 in internal/request/graphql/parser/filter.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/filter.go#L105-L106

Added lines #L105 - L106 were not covered by tests

default:
return nil, client.NewErrUnhandledType("parseConditionInOrder", v)
}
Expand Down Expand Up @@ -199,3 +195,16 @@ func parseFilterFieldsForDescriptionSlice(
}
return fields, nil
}

func parseOrderDirection(v int) (request.OrderDirection, error) {
switch v {
case 0:
return request.ASC, nil

Check warning on line 202 in internal/request/graphql/parser/filter.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/filter.go#L199-L202

Added lines #L199 - L202 were not covered by tests

case 1:
return request.DESC, nil

Check warning on line 205 in internal/request/graphql/parser/filter.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/filter.go#L204-L205

Added lines #L204 - L205 were not covered by tests

default:
return request.ASC, ErrInvalidOrderDirection

Check warning on line 208 in internal/request/graphql/parser/filter.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/filter.go#L207-L208

Added lines #L207 - L208 were not covered by tests
}
}
76 changes: 51 additions & 25 deletions internal/request/graphql/parser/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,60 @@ func parseMutation(exe *gql.ExecutionContext, parent *gql.Object, field *ast.Fie
mut.Collection = strings.Join(mutNameParts[1:], "_")
}

// parse arguments
for _, argument := range field.Arguments {
prop := argument.Name.Value
// parse each individual arg type seperately
if prop == request.Input { // parse input
mut.Input = arguments[prop].(map[string]any)
} else if prop == request.Inputs {
inputsValue := arguments[prop].([]any)
inputs := make([]map[string]any, len(inputsValue))
for i, v := range inputsValue {
name := argument.Name.Value
value := arguments[name]

Check warning on line 100 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L99-L100

Added lines #L99 - L100 were not covered by tests

switch name {
case request.Input:
if v, ok := value.(map[string]any); ok {
mut.Input = v

Check warning on line 105 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L102-L105

Added lines #L102 - L105 were not covered by tests
}

case request.Inputs:
v, ok := value.([]any)
if !ok {
continue // value is nil

Check warning on line 111 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L108-L111

Added lines #L108 - L111 were not covered by tests
}
inputs := make([]map[string]any, len(v))
for i, v := range v {

Check warning on line 114 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L113-L114

Added lines #L113 - L114 were not covered by tests
inputs[i] = v.(map[string]any)
}
mut.Inputs = inputs
} else if prop == request.FilterClause { // parse filter
mut.Filter = immutable.Some(request.Filter{
Conditions: arguments[prop].(map[string]any),
})
} else if prop == request.DocIDArgName {
mut.DocIDs = immutable.Some([]string{arguments[prop].(string)})
} else if prop == request.DocIDsArgName {
docIDsValue := arguments[prop].([]any)
docIDs := make([]string, len(docIDsValue))
for i, v := range docIDsValue {

case request.FilterClause:
if v, ok := value.(map[string]any); ok {
mut.Filter = immutable.Some(request.Filter{Conditions: v})

Check warning on line 121 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}

case request.DocIDArgName:
if v, ok := value.(string); ok {
mut.DocIDs = immutable.Some([]string{v})

Check warning on line 126 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L124-L126

Added lines #L124 - L126 were not covered by tests
}

case request.DocIDsArgName:
v, ok := value.([]any)
if !ok {
continue // value is nil

Check warning on line 132 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L129-L132

Added lines #L129 - L132 were not covered by tests
}
docIDs := make([]string, len(v))
for i, v := range v {

Check warning on line 135 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L134-L135

Added lines #L134 - L135 were not covered by tests
docIDs[i] = v.(string)
}
mut.DocIDs = immutable.Some(docIDs)
} else if prop == request.EncryptDocArgName {
mut.Encrypt = arguments[prop].(bool)
} else if prop == request.EncryptFieldsArgName {
fieldsValue := arguments[prop].([]any)
fields := make([]string, len(fieldsValue))
for i, v := range fieldsValue {

case request.EncryptDocArgName:
if v, ok := value.(bool); ok {
mut.Encrypt = v

Check warning on line 142 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}

case request.EncryptFieldsArgName:
v, ok := value.([]any)
if !ok {
continue // value is nil

Check warning on line 148 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L145-L148

Added lines #L145 - L148 were not covered by tests
}
fields := make([]string, len(v))
for i, v := range v {

Check warning on line 151 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L150-L151

Added lines #L150 - L151 were not covered by tests
fields[i] = v.(string)
}
mut.EncryptFields = fields
Expand All @@ -144,5 +166,9 @@ func parseMutation(exe *gql.ExecutionContext, parent *gql.Object, field *ast.Fie
}

mut.Fields, err = parseSelectFields(exe, fieldObject, field.SelectionSet)
if err != nil {
return nil, err

Check warning on line 170 in internal/request/graphql/parser/mutation.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/parser/mutation.go#L169-L170

Added lines #L169 - L170 were not covered by tests
}

return mut, err
}
Loading

0 comments on commit c4b2456

Please sign in to comment.