Skip to content

Commit

Permalink
Query filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Sep 23, 2020
1 parent 72df5bc commit b2ab70c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
3 changes: 3 additions & 0 deletions graphql/dgraph/graphquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ func writeFilterFunction(b *strings.Builder, f *gql.Function) {
x.Check2(b.WriteString(fmt.Sprintf("%s(%s)", f.Name, f.Args[0].Value)))
case len(f.Args) == 2:
x.Check2(b.WriteString(fmt.Sprintf("%s(%s, %s)", f.Name, f.Args[0].Value, f.Args[1].Value)))
case len(f.Args) == 3:
x.Check2(b.WriteString(fmt.Sprintf("%s(%s, %s, %s)", f.Name, f.Args[0].Value, f.Args[1].Value,
f.Args[2].Value)))
}
}

Expand Down
39 changes: 31 additions & 8 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,15 +1057,38 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
// OR
// numLikes: { le: 10 } -> le(Post.numLikes, 10)
fn, val := first(dgFunc)
ands = append(ands, &gql.FilterTree{
Func: &gql.Function{
Name: fn,
Args: []gql.Arg{
{Value: typ.DgraphPredicate(field)},
{Value: maybeQuoteArg(fn, val)},
switch fn {
case "near":
geoParams := val.(map[string]interface{})
distance, _ := geoParams["distance"]

coordinate, _ := geoParams["coordinate"].(map[string]interface{})
lat, _ := coordinate["latitude"]
long, _ := coordinate["longitude"]

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

ands = append(ands, &gql.FilterTree{
Func: &gql.Function{
Name: fn,
Args: args,
},
},
})
})
default:
ands = append(ands, &gql.FilterTree{
Func: &gql.Function{
Name: fn,
Args: []gql.Arg{
{Value: typ.DgraphPredicate(field)},
{Value: maybeQuoteArg(fn, val)},
},
},
})
}
case []interface{}:
// ids: [ 0x123, 0x124 ] -> uid(0x123, 0x124)
ids := convertIDs(dgFunc)
Expand Down
3 changes: 3 additions & 0 deletions graphql/resolve/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestQueryRewriting(t *testing.T) {

for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
if tcase.Name != "Geo query" {
return
}

op, err := gqlSchema.Operation(
&schema.Request{
Expand Down
16 changes: 16 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
-
name: "Geo query"
gqlquery: |
query {
queryHotel(filter: { location: { near: { distance: 33.33, coordinate: { latitude: 11.11, longitude: 22.22} } } }) {
name
}
}
dgquery: |-
query {
queryHotel(func: type(Hotel))) @filter(near(Hotel.location, [22.22,11.11], 33.33)) {
name : Hotel.name
dgraph.uid : uid
}
}
-
name: "ID query"
gqlquery: |
Expand Down
6 changes: 6 additions & 0 deletions graphql/resolve/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Test schema that contains an example of everything that's useful to
# test for query rewriting.

type Hotel {
id: ID!
name: String!
location: Point @search
}

type Country {
id: ID!
name: String! @search(by: [trigram, exact])
Expand Down
14 changes: 7 additions & 7 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,22 @@ input CustomHTTP {
}
type Point {
Latitude: Float!
Longitude: Float!
latitude: Float!
longitude: Float!
}
input PointInput {
Latitude: Float!
Longitude: Float!
latitude: Float!
longitude: Float!
}
input NearFilter {
Distance: Float!
Coordinate: PointInput!
distance: Float!
coordinate: PointInput!
}
input PointGeoFilter {
near: NearFilter
near: NearFilter!
}
directive @hasInverse(field: String!) on FIELD_DEFINITION
Expand Down

0 comments on commit b2ab70c

Please sign in to comment.