From ca5294b18512f9207c737612ac119e9469bc64b3 Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Gaur <12651351+abhimanyusinghgaur@users.noreply.github.com> Date: Tue, 1 Sep 2020 22:39:37 +0530 Subject: [PATCH 1/2] fix(GraphQL): incorrect generatedSchema in updateGQLSchema (#6349) This PR fixes the behaviour where Dgraph schema was being given as generatedSchema field in updateGQLSchema request. Now, the newly generated complete GraphQL schema is given as generatedSchema. (cherry picked from commit 0b8681cdca27b1032aa645ee6c0e6851ae82b154) # Conflicts: # graphql/e2e/schema/schema_test.go --- graphql/admin/schema.go | 8 +- graphql/e2e/schema/generatedSchema.graphql | 232 +++++++++++++++++++++ graphql/e2e/schema/schema_test.go | 46 +++- 3 files changed, 277 insertions(+), 9 deletions(-) create mode 100644 graphql/e2e/schema/generatedSchema.graphql diff --git a/graphql/admin/schema.go b/graphql/admin/schema.go index 96f9fd5bac3..7e1c23448f6 100644 --- a/graphql/admin/schema.go +++ b/graphql/admin/schema.go @@ -57,10 +57,8 @@ func resolveUpdateGQLSchema(ctx context.Context, m schema.Mutation) (*resolve.Re if _, err = schema.FromString(schHandler.GQLSchema()); err != nil { return resolve.EmptyResult(m, err), false } - newGQLSchema := input.Set.Schema - newDgraphSchema := schHandler.DGSchema() - resp, err := edgraph.UpdateGQLSchema(ctx, newGQLSchema, newDgraphSchema) + resp, err := edgraph.UpdateGQLSchema(ctx, input.Set.Schema, schHandler.DGSchema()) if err != nil { return resolve.EmptyResult(m, err), false } @@ -70,8 +68,8 @@ func resolveUpdateGQLSchema(ctx context.Context, m schema.Mutation) (*resolve.Re m.Name(): map[string]interface{}{ "gqlSchema": map[string]interface{}{ "id": query.UidToHex(resp.Uid), - "schema": newGQLSchema, - "generatedSchema": newDgraphSchema, + "schema": input.Set.Schema, + "generatedSchema": schHandler.GQLSchema(), }}}, Field: m, Err: nil, diff --git a/graphql/e2e/schema/generatedSchema.graphql b/graphql/e2e/schema/generatedSchema.graphql new file mode 100644 index 00000000000..f010dc0096b --- /dev/null +++ b/graphql/e2e/schema/generatedSchema.graphql @@ -0,0 +1,232 @@ +####################### +# Input Schema +####################### + +type Author { + id: ID! + name: String! +} + +####################### +# Extended Definitions +####################### + +""" +The Int64 scalar type represents a signed 64‐bit numeric non‐fractional value. +Int64 can currently represent values in range [-(2^53)+1, (2^53)-1] without any error. +Values out of this range but representable by a signed 64-bit integer, may get coercion error. +""" +scalar Int64 + +scalar DateTime + +enum DgraphIndex { + int + int64 + float + bool + hash + exact + term + fulltext + trigram + regexp + year + month + day + hour +} + +input AuthRule { + and: [AuthRule] + or: [AuthRule] + not: AuthRule + rule: String +} + +enum HTTPMethod { + GET + POST + PUT + PATCH + DELETE +} + +enum Mode { + BATCH + SINGLE +} + +input CustomHTTP { + url: String! + method: HTTPMethod! + body: String + graphql: String + mode: Mode + forwardHeaders: [String!] + secretHeaders: [String!] + introspectionHeaders: [String!] + skipIntrospection: Boolean +} + +directive @hasInverse(field: String!) on FIELD_DEFINITION +directive @search(by: [DgraphIndex!]) on FIELD_DEFINITION +directive @dgraph(type: String, pred: String) on OBJECT | INTERFACE | FIELD_DEFINITION +directive @id on FIELD_DEFINITION +directive @withSubscription on OBJECT | INTERFACE +directive @secret(field: String!, pred: String) on OBJECT | INTERFACE +directive @auth( + query: AuthRule, + add: AuthRule, + update: AuthRule, + delete:AuthRule) on OBJECT +directive @custom(http: CustomHTTP, dql: String) on FIELD_DEFINITION +directive @remote on OBJECT | INTERFACE +directive @cascade(fields: [String]) on FIELD + +input IntFilter { + eq: Int + le: Int + lt: Int + ge: Int + gt: Int +} + +input Int64Filter { + eq: Int64 + le: Int64 + lt: Int64 + ge: Int64 + gt: Int64 +} + +input FloatFilter { + eq: Float + le: Float + lt: Float + ge: Float + gt: Float +} + +input DateTimeFilter { + eq: DateTime + le: DateTime + lt: DateTime + ge: DateTime + gt: DateTime +} + +input StringTermFilter { + allofterms: String + anyofterms: String +} + +input StringRegExpFilter { + regexp: String +} + +input StringFullTextFilter { + alloftext: String + anyoftext: String +} + +input StringExactFilter { + eq: String + le: String + lt: String + ge: String + gt: String +} + +input StringHashFilter { + eq: String +} + +####################### +# Generated Types +####################### + +type AddAuthorPayload { + author(filter: AuthorFilter, order: AuthorOrder, first: Int, offset: Int): [Author] + numUids: Int +} + +type DeleteAuthorPayload { + author(filter: AuthorFilter, order: AuthorOrder, first: Int, offset: Int): [Author] + msg: String + numUids: Int +} + +type UpdateAuthorPayload { + author(filter: AuthorFilter, order: AuthorOrder, first: Int, offset: Int): [Author] + numUids: Int +} + +####################### +# Generated Enums +####################### + +enum AuthorHasFilter { + name +} + +enum AuthorOrderable { + name +} + +####################### +# Generated Inputs +####################### + +input AddAuthorInput { + name: String! +} + +input AuthorFilter { + id: [ID!] + has: AuthorHasFilter + and: AuthorFilter + or: AuthorFilter + not: AuthorFilter +} + +input AuthorOrder { + asc: AuthorOrderable + desc: AuthorOrderable + then: AuthorOrder +} + +input AuthorPatch { + name: String +} + +input AuthorRef { + id: ID + name: String +} + +input UpdateAuthorInput { + filter: AuthorFilter! + set: AuthorPatch + remove: AuthorPatch +} + +####################### +# Generated Query +####################### + +type Query { + getAuthor(id: ID!): Author + queryAuthor(filter: AuthorFilter, order: AuthorOrder, first: Int, offset: Int): [Author] +} + +####################### +# Generated Mutations +####################### + +type Mutation { + addAuthor(input: [AddAuthorInput!]!): AddAuthorPayload + updateAuthor(input: UpdateAuthorInput!): UpdateAuthorPayload + deleteAuthor(filter: AuthorFilter!): DeleteAuthorPayload +} + diff --git a/graphql/e2e/schema/schema_test.go b/graphql/e2e/schema/schema_test.go index c964f02cfdf..c2f5bc2f66b 100644 --- a/graphql/e2e/schema/schema_test.go +++ b/graphql/e2e/schema/schema_test.go @@ -20,17 +20,16 @@ import ( "context" "encoding/json" "fmt" + "io/ioutil" "sync" "testing" "time" - "github.com/dgraph-io/dgo/v200/protos/api" - - "github.com/dgraph-io/dgraph/worker" - "github.com/dgraph-io/dgo/v200" + "github.com/dgraph-io/dgo/v200/protos/api" "github.com/dgraph-io/dgraph/graphql/e2e/common" "github.com/dgraph-io/dgraph/testutil" + "github.com/dgraph-io/dgraph/worker" "github.com/stretchr/testify/require" ) @@ -328,6 +327,45 @@ func TestGQLSchemaAfterDropData(t *testing.T) { } +func TestUpdateGQLSchemaFields(t *testing.T) { + schema := ` + type Author { + id: ID! + name: String! + }` + + generatedSchema, err := ioutil.ReadFile("generatedSchema.graphql") + require.NoError(t, err) + + req := &common.GraphQLParams{ + Query: `mutation updateGQLSchema($sch: String!) { + updateGQLSchema(input: { set: { schema: $sch }}) { + gqlSchema { + schema + generatedSchema + } + } + }`, + Variables: map[string]interface{}{"sch": schema}, + } + resp := req.ExecuteAsPost(t, groupOneAdminServer) + require.NotNil(t, resp) + require.Nilf(t, resp.Errors, "%s", resp.Errors) + + var updateResp struct { + UpdateGQLSchema struct { + GQLSchema struct { + Schema string + GeneratedSchema string + } + } + } + require.NoError(t, json.Unmarshal(resp.Data, &updateResp)) + + require.Equal(t, schema, updateResp.UpdateGQLSchema.GQLSchema.Schema) + require.Equal(t, string(generatedSchema), updateResp.UpdateGQLSchema.GQLSchema.GeneratedSchema) +} + func updateGQLSchema(t *testing.T, schema, url string) *common.GraphQLResponse { req := &common.GraphQLParams{ Query: `mutation updateGQLSchema($sch: String!) { From f78a84d910b9ab26cfe28d03ab981d3a1eeaa827 Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Gaur <12651351+abhimanyusinghgaur@users.noreply.github.com> Date: Tue, 1 Sep 2020 23:12:18 +0530 Subject: [PATCH 2/2] fix test as per release branch --- graphql/e2e/schema/generatedSchema.graphql | 27 ++-------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/graphql/e2e/schema/generatedSchema.graphql b/graphql/e2e/schema/generatedSchema.graphql index f010dc0096b..10ec75f3b8a 100644 --- a/graphql/e2e/schema/generatedSchema.graphql +++ b/graphql/e2e/schema/generatedSchema.graphql @@ -11,18 +11,10 @@ type Author { # Extended Definitions ####################### -""" -The Int64 scalar type represents a signed 64‐bit numeric non‐fractional value. -Int64 can currently represent values in range [-(2^53)+1, (2^53)-1] without any error. -Values out of this range but representable by a signed 64-bit integer, may get coercion error. -""" -scalar Int64 - scalar DateTime enum DgraphIndex { int - int64 float bool hash @@ -80,9 +72,9 @@ directive @auth( add: AuthRule, update: AuthRule, delete:AuthRule) on OBJECT -directive @custom(http: CustomHTTP, dql: String) on FIELD_DEFINITION +directive @custom(http: CustomHTTP) on FIELD_DEFINITION directive @remote on OBJECT | INTERFACE -directive @cascade(fields: [String]) on FIELD +directive @cascade on FIELD input IntFilter { eq: Int @@ -92,14 +84,6 @@ input IntFilter { gt: Int } -input Int64Filter { - eq: Int64 - le: Int64 - lt: Int64 - ge: Int64 - gt: Int64 -} - input FloatFilter { eq: Float le: Float @@ -166,10 +150,6 @@ type UpdateAuthorPayload { # Generated Enums ####################### -enum AuthorHasFilter { - name -} - enum AuthorOrderable { name } @@ -184,9 +164,6 @@ input AddAuthorInput { input AuthorFilter { id: [ID!] - has: AuthorHasFilter - and: AuthorFilter - or: AuthorFilter not: AuthorFilter }