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

feat(GraphQL): add support for between filter in GraphQL #6651

Merged
merged 19 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
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
24 changes: 16 additions & 8 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,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 @@ -1076,7 +1076,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 @@ -1087,18 +1087,26 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
lat := coordinate["latitude"]
long := coordinate["longitude"]

args = append(args, gql.Arg{Value: fmt.Sprintf("[%v,%v]", long, lat)})
args = append(args, gql.Arg{Value: fmt.Sprintf("%v", distance)})
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"])})
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, vals["max"])})

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
45 changes: 45 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,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 @@ -80,6 +80,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 @@ -171,6 +196,7 @@ input IntFilter {
lt: Int
ge: Int
gt: Int
between: IntRange
}

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

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

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

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

input StringHashFilter {
Expand Down
Loading