Skip to content

Commit

Permalink
fix(GraphQL): This PR fix panic error when we give null value in filt…
Browse files Browse the repository at this point in the history
…er connectives. (#6707)

Fixes GRAPHQL-744

This PR fix panic error when we give null value in filter connectives. Null values corresponding to connective are skipped now.
For example in the below filter, not connective gets skipped.

```

 filter:{
      id:{eq:"123"},
      not:null
    },

```
  • Loading branch information
JatinDev543 authored Oct 14, 2020
1 parent c269de8 commit 345b6d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 7 additions & 6 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,9 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
// Each key in filter is either "and", "or", "not" or the field name it
// applies to such as "title" in: `title: { anyofterms: "GraphQL" }``
for _, field := range keys {
if filter[field] == nil {
continue
}
switch field {

// In 'and', 'or' and 'not' cases, filter[field] must be a map[string]interface{}
Expand Down Expand Up @@ -1076,7 +1079,6 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
for _, v := range vals {
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, v)})
}

case "near":
// For Geo type we have `near` filter which is written as follows:
// { near: { distance: 33.33, coordinate: { latitude: 11.11, longitude: 22.22 } } }
Expand All @@ -1086,19 +1088,18 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
coordinate, _ := geoParams["coordinate"].(map[string]interface{})
lat := coordinate["latitude"]
long := coordinate["longitude"]
args = append(args, gql.Arg{Value: fmt.Sprintf("[%v,%v]", long, lat)},
gql.Arg{Value: fmt.Sprintf("%v", distance)})

args = append(args, gql.Arg{Value: fmt.Sprintf("[%v,%v]", long, lat)})
args = append(args, gql.Arg{Value: fmt.Sprintf("%v", distance)})

default:
default:
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, val)})
}
ands = append(ands, &gql.FilterTree{
Func: &gql.Function{
Name: fn,
Args: args,
},
})
})
case []interface{}:
// ids: [ 0x123, 0x124 ] -> uid(0x123, 0x124)
ids := convertIDs(dgFunc)
Expand Down
18 changes: 17 additions & 1 deletion graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
dgraph.uid : uid
}
}
-
name: "Geo query"
gqlquery: |
Expand Down Expand Up @@ -206,6 +206,22 @@
}
}
-
name: "Filter connectives with null values gets skipped "
gqlquery: |
query {
queryAuthor(filter: { name: { eq: "A. N. Author" },not:null }) {
name
}
}
dgquery: |-
query {
queryAuthor(func: type(Author)) @filter(eq(Author.name, "A. N. Author")) {
name : Author.name
dgraph.uid : uid
}
}
-
name: "Query with has Filter"
gqlquery: |
Expand Down

0 comments on commit 345b6d1

Please sign in to comment.