Skip to content

Commit

Permalink
feat(GraphQL): Add support for Geo point type in Graphql. (#6481)
Browse files Browse the repository at this point in the history
* Add support for Geo point type in Graphql.

* Add unit and E2E test.

Co-authored-by: Pawan Rawal <[email protected]>
  • Loading branch information
Arijit Das and pawanrawal authored Oct 7, 2020
1 parent d994cb3 commit 0db78e7
Show file tree
Hide file tree
Showing 64 changed files with 1,681 additions and 100 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
2 changes: 2 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func RunAll(t *testing.T) {
t.Run("queries have extensions", queriesHaveExtensions)
t.Run("alias works for queries", queryWithAlias)
t.Run("cascade directive", queryWithCascade)
t.Run("query geo near filter", queryGeoNearFilter)

// mutation tests
t.Run("add mutation", addMutation)
Expand Down Expand Up @@ -378,6 +379,7 @@ func RunAll(t *testing.T) {
t.Run("update mutation without set & remove", updateMutationWithoutSetRemove)
t.Run("Input coercing for int64 type", int64BoundaryTesting)
t.Run("Check cascade with mutation without ID field", checkCascadeWithMutationWithoutIDField)
t.Run("Geo type", mutationGeoType)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
43 changes: 43 additions & 0 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3747,6 +3747,49 @@ func nestedAddMutationWithHasInverse(t *testing.T) {
deleteGqlType(t, "Person1", map[string]interface{}{}, 3, nil)
}

func mutationGeoType(t *testing.T) {
addHotelParams := &GraphQLParams{
Query: `
mutation addHotel($hotel: AddHotelInput!) {
addHotel(input: [$hotel]) {
hotel {
name
location {
latitude
longitude
}
}
}
}`,
Variables: map[string]interface{}{"hotel": map[string]interface{}{
"name": "Taj Hotel",
"location": map[string]interface{}{
"latitude": 11.11,
"longitude": 22.22,
},
}},
}
gqlResponse := addHotelParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

addHotelExpected := `
{
"addHotel": {
"hotel": [{
"name": "Taj Hotel",
"location": {
"latitude": 11.11,
"longitude": 22.22
}
}]
}
}`
testutil.CompareJSON(t, addHotelExpected, string(gqlResponse.Data))

// Cleanup
deleteGqlType(t, "Hotel", map[string]interface{}{}, 1, nil)
}

func addMutationWithHasInverseOverridesCorrectly(t *testing.T) {
params := &GraphQLParams{
Query: `mutation addCountry($input: [AddCountryInput!]!) {
Expand Down
72 changes: 72 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2084,3 +2084,75 @@ func queryWithCascade(t *testing.T) {
nil)
cleanupStarwars(t, newStarship.ID, humanID, "")
}

func queryGeoNearFilter(t *testing.T) {
addHotelParams := &GraphQLParams{
Query: `
mutation addHotel($hotels: [AddHotelInput!]!) {
addHotel(input: $hotels) {
hotel {
name
location {
latitude
longitude
}
}
}
}`,
Variables: map[string]interface{}{"hotels": []interface{}{
map[string]interface{}{
"name": "Taj Hotel 1",
"location": map[string]interface{}{
"latitude": 11.11,
"longitude": 22.22,
},
},
map[string]interface{}{
"name": "Taj Hotel 2",
"location": map[string]interface{}{
"latitude": 33.33,
"longitude": 22.22,
},
},
map[string]interface{}{
"name": "Taj Hotel 3",
"location": map[string]interface{}{
"latitude": 11.11,
"longitude": 33.33,
},
},
},
},
}
gqlResponse := addHotelParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

queryHotel := &GraphQLParams{
Query: `
query {
queryHotel(filter: { location: { near: { distance: 100, coordinate: { latitude: 11.11, longitude: 22.22} } } }) {
name
location {
latitude
longitude
}
}
}`,
}
gqlResponse = queryHotel.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

queryHotelExpected := `
{
"queryHotel":[{
"name" : "Taj Hotel 1",
"location" : {
"latitude" : 11.11,
"longitude" : 22.22
}
}]
}`
testutil.CompareJSON(t, queryHotelExpected, string(gqlResponse.Data))
// Cleanup
deleteGqlType(t, "Hotel", map[string]interface{}{}, 3, nil)
}
6 changes: 6 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# **Don't delete** Comments at top of schemas should work
# See: https://github.com/dgraph-io/dgraph/issues/4227

type Hotel {
id: ID!
name: String! @search(by: [exact])
location: Point @search
}

type Country {
# **Don't delete** Comments in types should work
id: ID! # **Don't delete** Comments in lines should work
Expand Down
105 changes: 68 additions & 37 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"schema": [
{
"index": true,
"list": true,
"predicate": "dgraph.cors",
"tokenizer": [
"exact"
],
"type": "string",
"upsert": true
},
{
"predicate": "Category.name",
"type": "string"
Expand Down Expand Up @@ -42,6 +32,22 @@
"trigram"
]
},
{
"predicate": "Hotel.location",
"type": "geo",
"index": true,
"tokenizer": [
"geo"
]
},
{
"predicate": "Hotel.name",
"type": "string",
"index": true,
"tokenizer": [
"exact"
]
},
{
"predicate": "Human.starships",
"type": "uid",
Expand All @@ -68,15 +74,15 @@
],
"upsert": true
},
{
"predicate": "Person1.name",
"type": "string"
},
{
"predicate": "Person1.friends",
"type": "uid",
"list": true
},
{
"predicate": "Person1.name",
"type": "string"
},
{
"predicate": "Post1.comments",
"type": "uid",
Expand All @@ -91,17 +97,6 @@
],
"upsert": true
},
{
"predicate": "post1.title",
"type": "string",
"index": true,
"tokenizer": ["trigram", "hash"],
"upsert": true
},
{
"predicate": "post1.numLikes",
"type": "int"
},
{
"predicate": "State.capital",
"type": "string"
Expand Down Expand Up @@ -176,6 +171,16 @@
"predicate": "credits",
"type": "float"
},
{
"predicate": "dgraph.cors",
"type": "string",
"index": true,
"tokenizer": [
"exact"
],
"list": true,
"upsert": true
},
{
"predicate": "dgraph.graphql.schema",
"type": "string"
Expand Down Expand Up @@ -292,6 +297,20 @@
"predicate": "post.author",
"type": "uid"
},
{
"predicate": "post1.numLikes",
"type": "int"
},
{
"predicate": "post1.title",
"type": "string",
"index": true,
"tokenizer": [
"hash",
"trigram"
],
"upsert": true
},
{
"predicate": "pwd",
"type": "password"
Expand Down Expand Up @@ -405,6 +424,17 @@
],
"name": "Country"
},
{
"fields": [
{
"name": "Hotel.name"
},
{
"name": "Hotel.location"
}
],
"name": "Hotel"
},
{
"fields": [
{
Expand Down Expand Up @@ -488,17 +518,6 @@
],
"name": "Post1"
},
{
"fields": [
{
"name": "post1.title"
},
{
"name": "post1.numLikes"
}
],
"name": "post1"
},
{
"fields": [
{
Expand Down Expand Up @@ -609,7 +628,8 @@
"fields": [
{
"name": "dgraph.graphql.schema_history"
},{
},
{
"name": "dgraph.graphql.schema_created_at"
}
],
Expand Down Expand Up @@ -661,6 +681,17 @@
],
"name": "performance.character"
},
{
"fields": [
{
"name": "post1.title"
},
{
"name": "post1.numLikes"
}
],
"name": "post1"
},
{
"fields": [
{
Expand Down
6 changes: 6 additions & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# **Don't delete** Comments at top of schemas should work
# See: https://github.com/dgraph-io/dgraph/issues/4227

type Hotel {
id: ID!
name: String! @search(by: [exact])
location: Point @search
}

type Country {
# **Don't delete** Comments in types should work
id: ID! # **Don't delete** Comments in in lines should work
Expand Down
Loading

0 comments on commit 0db78e7

Please sign in to comment.