Skip to content

Commit

Permalink
graphql: make updateGQLSchema always return the new schema (#5540)
Browse files Browse the repository at this point in the history
This PR makes the updateGQLSchema mutation return the updated schema always, instead of relying on async badger update.

Fixes #GRAPHQL-497.

(cherry-picked from commit 265086f)

# Conflicts:
#	graphql/admin/schema.go
  • Loading branch information
abhimanyusinghgaur committed Jun 5, 2020
1 parent ea9bd79 commit 35dc57a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 14 additions & 4 deletions graphql/admin/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type updateSchemaResolver struct {

mutation schema.Mutation

// new GraphQL schema that is given as mutation input
newSchema string
// GraphQL schema that is generated from that input
generatedSchema string
// dgraph schema that is generated from the mutation input
newDgraphSchema string

Expand Down Expand Up @@ -73,11 +77,12 @@ func (asr *updateSchemaResolver) Rewrite(
return nil, err
}

_, err = schema.FromString(schHandler.GQLSchema())
asr.generatedSchema = schHandler.GQLSchema()
_, err = schema.FromString(asr.generatedSchema)
if err != nil {
return nil, err
}

asr.newSchema = input.Set.Schema
asr.newDgraphSchema = schHandler.DGSchema()

// There will always be a graphql schema node present in Dgraph cluster. So, we just need to
Expand Down Expand Up @@ -108,7 +113,11 @@ func (asr *updateSchemaResolver) Execute(
if req == nil || (req.Query == "" && len(req.Mutations) == 0) {
// For schema updates, Execute will get called twice. Once for the
// mutation and once for the following query. This is the query case.
b, err := doQuery(asr.admin.schema, asr.mutation.QueryField())
b, err := doQuery(&gqlSchema{
ID: asr.admin.schema.ID,
Schema: asr.newSchema,
GeneratedSchema: asr.generatedSchema,
}, asr.mutation.QueryField())
return &dgoapi.Response{Json: b}, err
}

Expand All @@ -118,7 +127,8 @@ func (asr *updateSchemaResolver) Execute(
return nil, err
}

_, err = (&edgraph.Server{}).Alter(ctx, &dgoapi.Operation{Schema: asr.newDgraphSchema})
_, err = (&edgraph.Server{}).Alter(ctx, &dgoapi.Operation{Schema: asr.newDgraphSchema,
RunInBackground: false})
if err != nil {
return nil, schema.GQLWrapf(err,
"succeeded in saving GraphQL schema but failed to alter Dgraph schema ")
Expand Down
5 changes: 5 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,18 @@ func addSchema(url string, schema string) error {
}
}
}
Errors []interface{}
}

err = json.Unmarshal(resp, &addResult)
if err != nil {
return errors.Wrap(err, "error trying to unmarshal GraphQL mutation result")
}

if len(addResult.Errors) > 0 {
return errors.Errorf("%v", addResult.Errors)
}

if addResult.Data.UpdateGQLSchema.GQLSchema.Schema == "" {
return errors.New("GraphQL schema mutation failed")
}
Expand Down

0 comments on commit 35dc57a

Please sign in to comment.