Skip to content

Commit

Permalink
fix(graphql): adding support for @id with type other than strings (#7019
Browse files Browse the repository at this point in the history
)

* adding support for @id with type other than strings

* fixing query and mutation rewriter to accept @id directive with int

* fixing float error with large exponents

* adding test cases to test dgraph schemagen

* fix test cases because of float bug changes

* test cases addition

* test cases addition for query and mutation

* fixing failing test cases

* refactoring mutation rewriter

* refactoring to manage cases better

* adding cleanup in mutation tests + changing query tests to get

* making mutation rewriter robust to panic

* fixing breaking get test cases

* added test cases for large floats

* fixing vars + test
  • Loading branch information
aman-bansal authored Dec 14, 2020
1 parent 3533dfb commit 61016bd
Show file tree
Hide file tree
Showing 18 changed files with 607 additions and 31 deletions.
6 changes: 6 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,9 @@ func RunAll(t *testing.T) {
t.Run("query aggregate and other fields at child level", queryAggregateAndOtherFieldsAtChildLevel)
t.Run("query at child level with multiple alias on scalar field", queryChildLevelWithMultipleAliasOnScalarField)
t.Run("checkUserPassword query", passwordTest)
t.Run("query id directive with int", idDirectiveWithInt)
t.Run("query id directive with int64", idDirectiveWithInt64)
t.Run("query id directive with float", idDirectiveWithFloat)

// mutation tests
t.Run("add mutation", addMutation)
Expand Down Expand Up @@ -702,6 +705,9 @@ func RunAll(t *testing.T) {
t.Run("Geo - MultiPolygon type", mutationMultiPolygonType)
t.Run("filter in mutations with array for AND/OR", filterInMutationsWithArrayForAndOr)
t.Run("filter in update mutations with array for AND/OR", filterInUpdateMutationsWithFilterAndOr)
t.Run("mutation id directive with int", idDirectiveWithIntMutation)
t.Run("mutation id directive with int64", idDirectiveWithInt64Mutation)
t.Run("mutation id directive with float", idDirectiveWithFloatMutation)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
94 changes: 94 additions & 0 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4576,3 +4576,97 @@ func filterInUpdateMutationsWithFilterAndOr(t *testing.T) {
DeleteGqlType(t, "post1", filter, 2, nil)

}

func idDirectiveWithInt64Mutation(t *testing.T) {
query := &GraphQLParams{
Query: `mutation {
addBook(input:[
{
bookId: 1234567890123
name: "Graphql"
desc: "Graphql is the next big thing"
}
]) {
numUids
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
expected := `{
"addBook": {
"numUids": 1
}
}`
require.JSONEq(t, expected, string(response.Data))

// adding same mutation again should result in error because of duplicate id
response = query.ExecuteAsPost(t, GraphqlURL)
require.Contains(t, response.Errors.Error(), "already exists")

DeleteGqlType(t, "Book", map[string]interface{}{}, 2, nil)
}

func idDirectiveWithIntMutation(t *testing.T) {
query := &GraphQLParams{
Query: `mutation {
addChapter(input:[{
chapterId: 2
name: "Graphql and more"
bookId: 1234567890123
}]) {
numUids
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
var expected = `{
"addChapter": {
"numUids": 1
}
}`
require.JSONEq(t, expected, string(response.Data))

// adding same mutation again should result in error because of duplicate id
response = query.ExecuteAsPost(t, GraphqlURL)
require.Contains(t, response.Errors.Error(), "already exists")

DeleteGqlType(t, "Chapter", map[string]interface{}{}, 2, nil)
}

func idDirectiveWithFloatMutation(t *testing.T) {
query := &GraphQLParams{
Query: `mutation {
addSection(input:[{
chapterId: 2
name: "Graphql: Introduction"
sectionId: 2.1
},
{
chapterId: 2
name: "Graphql Available Data Types"
sectionId: 2.2
}]) {
numUids
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
var expected = `{
"addSection": {
"numUids": 2
}
}`
require.JSONEq(t, expected, string(response.Data))

// adding same mutation again should result in error because of duplicate id
response = query.ExecuteAsPost(t, GraphqlURL)
require.Contains(t, response.Errors.Error(), "already exists")

DeleteGqlType(t, "Section", map[string]interface{}{}, 4, nil)
}
69 changes: 69 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3283,3 +3283,72 @@ func passwordTest(t *testing.T) {

deleteUser(t, *newUser)
}

func idDirectiveWithInt64(t *testing.T) {
query := &GraphQLParams{
Query: `query {
getBook(bookId: 1234567890) {
bookId
name
desc
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
var expected = `{
"getBook": {
"bookId": 1234567890,
"name": "Dgraph and Graphql",
"desc": "All love between dgraph and graphql"
}
}`
require.JSONEq(t, expected, string(response.Data))
}

func idDirectiveWithInt(t *testing.T) {
query := &GraphQLParams{
Query: `query {
getChapter(chapterId: 1) {
bookId
chapterId
name
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
var expected = `{
"getChapter": {
"bookId": 1234567890,
"chapterId": 1,
"name": "How Dgraph Works"
}
}`
require.JSONEq(t, expected, string(response.Data))
}

func idDirectiveWithFloat(t *testing.T) {
query := &GraphQLParams{
Query: `query {
getSection(sectionId: 1.1) {
chapterId
sectionId
name
}
}`,
}

response := query.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, response)
var expected = `{
"getSection": {
"chapterId": 1,
"sectionId": 1.1,
"name": "How to define dgraph schema"
}
}`
require.JSONEq(t, expected, string(response.Data))
}
19 changes: 19 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,22 @@ type University @generate(
name: String!
numStudents: Int
}

# @id directive with multiple data types
type Book {
bookId: Int64! @id
name: String!
desc: String
}

type Chapter {
chapterId: Int! @id
name: String!
bookId: Int64! @search
}

type Section {
sectionId: Float! @id
name: String!
chapterId: Int! @search
}
101 changes: 101 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,65 @@
{
"predicate": "职业",
"type": "string"
},
{
"predicate": "Book.name",
"type": "string"
},
{
"predicate": "Book.desc",
"type": "string"
},
{
"predicate": "Section.name",
"type": "string"
},
{
"predicate": "Chapter.name",
"type": "string"
},
{
"index": true,
"predicate": "Section.sectionId",
"tokenizer": [
"float"
],
"type": "float",
"upsert": true
},
{
"index": true,
"predicate": "Chapter.bookId",
"tokenizer": [
"int"
],
"type": "int"
},
{
"index": true,
"predicate": "Chapter.chapterId",
"tokenizer": [
"int"
],
"type": "int",
"upsert": true
},
{
"index": true,
"predicate": "Book.bookId",
"tokenizer": [
"int"
],
"type": "int",
"upsert": true
},
{
"index": true,
"predicate": "Section.chapterId",
"tokenizer": [
"int"
],
"type": "int"
}
],
"types": [
Expand Down Expand Up @@ -968,6 +1027,48 @@
}
],
"name": "test.dgraph.employee.en"
},
{
"fields": [
{
"name": "Book.name"
},
{
"name": "Book.desc"
},
{
"name": "Book.bookId"
}
],
"name": "Book"
},
{
"fields": [
{
"name": "Chapter.name"
},
{
"name": "Chapter.bookId"
},
{
"name": "Chapter.chapterId"
}
],
"name": "Chapter"
},
{
"fields": [
{
"name": "Section.sectionId"
},
{
"name": "Section.chapterId"
},
{
"name": "Section.name"
}
],
"name": "Section"
}
]
}
28 changes: 28 additions & 0 deletions graphql/e2e/directives/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,33 @@
"dgraph.type": "Country",
"Country.name": "India",
"hasStates": [{ "uid": "_:mh" }, { "uid": "_:gj" }, { "uid": "_:ka"}]
},
{
"uid": "_:book1",
"dgraph.type": "Book",
"Book.bookId": 1234567890,
"Book.name": "Dgraph and Graphql",
"Book.desc": "All love between dgraph and graphql"
},
{
"uid": "_:chapter1",
"dgraph.type": "Chapter",
"Chapter.bookId": 1234567890,
"Chapter.chapterId": 1,
"Chapter.name": "How Dgraph Works"
},
{
"uid": "_:section1",
"dgraph.type": "Section",
"Section.chapterId": 1,
"Section.sectionId": 1.1,
"Section.name": "How to define dgraph schema"
},
{
"uid": "_:section2",
"dgraph.type": "Section",
"Section.chapterId": 1,
"Section.sectionId": 1.2,
"Section.name": "Dgraph Data Types"
}
]
19 changes: 19 additions & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,22 @@ type University @generate(
name: String!
numStudents: Int
}

# @id directive with multiple data types
type Book {
bookId: Int64! @id
name: String!
desc: String
}

type Chapter {
chapterId: Int! @id
name: String!
bookId: Int64! @search
}

type Section {
sectionId: Float! @id
name: String!
chapterId: Int! @search
}
Loading

0 comments on commit 61016bd

Please sign in to comment.