Skip to content

Commit

Permalink
feat(GraphQL): add support for between filter in GraphQL (#6651)
Browse files Browse the repository at this point in the history
Fixes GRAPHQL-667.

This PR extend support for `between` filter in GraphQL. This filter can be used for all scalars when indexed in a type (`exact` index for `String`). For the given type
```
type Student{
   age: Int @search
   name: String @search(by: [exact])
}
```
`between filter` can be used both on the `age` and `name` field. for eg:
```
queryStudent(fitler: {age: between: {min: 10, max: 20}}){
    age
    name
}
```
and
```
queryStudent(fitler: {name: between: {min: "abc", max: "def"}}){
    age
    name
}
```
  • Loading branch information
minhaj-shakeel authored Oct 19, 2020
1 parent 0c28a88 commit 542cfff
Show file tree
Hide file tree
Showing 49 changed files with 1,480 additions and 3 deletions.
3 changes: 3 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type post struct {
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
Tags []string `json:"tags,omitempty"`
Topic string `json:"topic,omitempty"`
NumLikes int `json:"numLikes,omitempty"`
NumViews int64 `json:"numViews,omitempty"`
IsPublished bool `json:"isPublished,omitempty"`
Expand Down Expand Up @@ -298,6 +299,8 @@ func RunAll(t *testing.T) {
t.Run("multiple search indexes wrong field", multipleSearchIndexesWrongField)
t.Run("hash search", hashSearch)
t.Run("in filter", inFilter)
t.Run("between filter", betweenFilter)
t.Run("deep between filter", deepBetweenFilter)
t.Run("deep filter", deepFilter)
t.Run("deep has filter", deepHasFilter)
t.Run("many queries", manyQueries)
Expand Down
68 changes: 68 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,74 @@ func inFilter(t *testing.T) {
deleteGqlType(t, "State", deleteFilter, 2, nil)
}

func betweenFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
queryPost(filter: {numLikes: {between: {min:90, max:100}}}) {
title
numLikes
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

var result struct {
QueryPost []*post
}

err := json.Unmarshal([]byte(gqlResponse.Data), &result)
require.NoError(t, err)
require.Equal(t, 1, len(result.QueryPost))

expected := &post{
Title: "Introducing GraphQL in Dgraph",
NumLikes: 100,
}

if diff := cmp.Diff(expected, result.QueryPost[0]); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}

func deepBetweenFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query{
queryAuthor(filter: {reputation: {between: {min:6.0, max: 7.2}}}){
name
reputation
posts(filter: {topic: {between: {min: "GraphQL", max: "GraphQL+-"}}}){
title
topic
}
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

var result struct {
QueryAuthor []*author
}

err := json.Unmarshal([]byte(gqlResponse.Data), &result)
require.NoError(t, err)
require.Equal(t, 1, len(result.QueryAuthor))

expected := &author{
Name: "Ann Author",
Reputation: 6.6,
Posts: []*post{{Title: "Introducing GraphQL in Dgraph", Topic: "GraphQL"}},
}

if diff := cmp.Diff(expected, result.QueryAuthor[0]); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}

}

func deepFilter(t *testing.T) {
getAuthorParams := &GraphQLParams{
Query: `query {
Expand Down
30 changes: 30 additions & 0 deletions graphql/e2e/schema/generatedSchema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ For example: "1985-04-12T23:20:50.52Z" represents 20 minutes and 50.52 seconds a
"""
scalar DateTime

input IntRange{
min: Int
max: Int
}

input FloatRange{
min: Float
max: Float
}

input Int64Range{
min: Int64
max: Int64
}

input DateTimeRange{
min: DateTime
max: DateTime
}

input StringRange{
min: String
max: String
}

enum DgraphIndex {
int
int64
Expand Down Expand Up @@ -114,6 +139,7 @@ input IntFilter {
lt: Int
ge: Int
gt: Int
between: IntRange
}

input Int64Filter {
Expand All @@ -122,6 +148,7 @@ input Int64Filter {
lt: Int64
ge: Int64
gt: Int64
between: Int64Range
}

input FloatFilter {
Expand All @@ -130,6 +157,7 @@ input FloatFilter {
lt: Float
ge: Float
gt: Float
between: FloatRange
}

input DateTimeFilter {
Expand All @@ -138,6 +166,7 @@ input DateTimeFilter {
lt: DateTime
ge: DateTime
gt: DateTime
between: DateTimeRange
}

input StringTermFilter {
Expand All @@ -161,6 +190,7 @@ input StringExactFilter {
lt: String
ge: String
gt: String
between: StringRange
}

input StringHashFilter {
Expand Down
15 changes: 12 additions & 3 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,9 +1071,9 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
Child: []*gql.FilterTree{not},
})
default:
// It's a base case like:
// title: { anyofterms: "GraphQL" } -> anyofterms(Post.title: "GraphQL")

//// It's a base case like:
//// title: { anyofterms: "GraphQL" } -> anyofterms(Post.title: "GraphQL")
//// numLikes: { between : { min : 10, max:100 }}
switch dgFunc := filter[field].(type) {
case map[string]interface{}:
// title: { anyofterms: "GraphQL" } -> anyofterms(Post.title, "GraphQL")
Expand All @@ -1096,6 +1096,7 @@ 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 @@ -1108,6 +1109,14 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
args = append(args, gql.Arg{Value: fmt.Sprintf("[%v,%v]", long, lat)},
gql.Arg{Value: fmt.Sprintf("%v", distance)})

case "between":
// numLikes: { between : { min : 10, max:100 }} should be rewritten into
// between(numLikes,10,20). Order of arguments (min,max) is neccessary or
// it will return empty
vals := val.(map[string]interface{})
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, vals["min"])},
gql.Arg{Value: maybeQuoteArg(fn, vals["max"])})

default:
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, val)})
}
Expand Down
45 changes: 45 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,51 @@
}
}
-
name: "Between filter"
gqlquery: |
query {
queryPost(filter: { numLikes: { between : { min :10, max: 20 }}}) {
title
text
}
}
dgquery: |-
query {
queryPost(func: type(Post)) @filter(between(Post.numLikes, 10, 20)) {
title : Post.title
text : Post.text
dgraph.uid : uid
}
}
-
name: "deep Between filter"
gqlquery: |
query{
queryAuthor(filter: {reputation: {between: {min:6.0, max: 7.2}}}){
name
reputation
posts(filter: {numLikes: {between: {min: 10, max: 100}}}){
title
numLikes
}
}
}
dgquery: |-
query {
queryAuthor(func: type(Author)) @filter(between(Author.reputation, 6, 7.2)) {
name : Author.name
reputation : Author.reputation
posts : Author.posts @filter(between(Post.numLikes, 10, 100)) {
title : Post.title
numLikes : Post.numLikes
dgraph.uid : uid
}
dgraph.uid : uid
}
}
-
name: "Filter with id inside and argument doesn't use uid func at root."
gqlquery: |
Expand Down
30 changes: 30 additions & 0 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ For example: "1985-04-12T23:20:50.52Z" represents 20 minutes and 50.52 seconds a
"""
scalar DateTime
input IntRange{
min: Int
max: Int
}
input FloatRange{
min: Float
max: Float
}
input Int64Range{
min: Int64
max: Int64
}
input DateTimeRange{
min: DateTime
max: DateTime
}
input StringRange{
min: String
max: String
}
enum DgraphIndex {
int
int64
Expand Down Expand Up @@ -173,6 +198,7 @@ input IntFilter {
lt: Int
ge: Int
gt: Int
between: IntRange
}
input Int64Filter {
Expand All @@ -181,6 +207,7 @@ input Int64Filter {
lt: Int64
ge: Int64
gt: Int64
between: Int64Range
}
input FloatFilter {
Expand All @@ -189,6 +216,7 @@ input FloatFilter {
lt: Float
ge: Float
gt: Float
between: FloatRange
}
input DateTimeFilter {
Expand All @@ -197,6 +225,7 @@ input DateTimeFilter {
lt: DateTime
ge: DateTime
gt: DateTime
between: DateTimeRange
}
input StringTermFilter {
Expand All @@ -220,6 +249,7 @@ input StringExactFilter {
lt: String
ge: String
gt: String
between: StringRange
}
input StringHashFilter {
Expand Down
30 changes: 30 additions & 0 deletions graphql/schema/testdata/schemagen/output/authorization.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ For example: "1985-04-12T23:20:50.52Z" represents 20 minutes and 50.52 seconds a
"""
scalar DateTime

input IntRange{
min: Int
max: Int
}

input FloatRange{
min: Float
max: Float
}

input Int64Range{
min: Int64
max: Int64
}

input DateTimeRange{
min: DateTime
max: DateTime
}

input StringRange{
min: String
max: String
}

enum DgraphIndex {
int
int64
Expand Down Expand Up @@ -125,6 +150,7 @@ input IntFilter {
lt: Int
ge: Int
gt: Int
between: IntRange
}

input Int64Filter {
Expand All @@ -133,6 +159,7 @@ input Int64Filter {
lt: Int64
ge: Int64
gt: Int64
between: Int64Range
}

input FloatFilter {
Expand All @@ -141,6 +168,7 @@ input FloatFilter {
lt: Float
ge: Float
gt: Float
between: FloatRange
}

input DateTimeFilter {
Expand All @@ -149,6 +177,7 @@ input DateTimeFilter {
lt: DateTime
ge: DateTime
gt: DateTime
between: DateTimeRange
}

input StringTermFilter {
Expand All @@ -172,6 +201,7 @@ input StringExactFilter {
lt: String
ge: String
gt: String
between: StringRange
}

input StringHashFilter {
Expand Down
Loading

0 comments on commit 542cfff

Please sign in to comment.