Skip to content

Commit

Permalink
fix(export): don't return an error if there was no GraphQL schema
Browse files Browse the repository at this point in the history
Cherry-pick of #6815.

FIxes GRAPHQL-747
Fixes [Discuss Issue](https://discuss.dgraph.io/t/multiple-values-for-the-graphql-schema/10881)
This PR fixes the issue where you would get an error, if you first add a GraphQL schema, then delete the GraphQL schema using `<uid> * *` delete mutation and try to export the DB.
  • Loading branch information
abhimanyusinghgaur authored and danielmai committed Mar 26, 2021
1 parent b3e416e commit f739844
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
49 changes: 49 additions & 0 deletions graphql/e2e/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,47 @@ func TestIntrospection(t *testing.T) {
// introspection response or the JSON comparison. Needs deeper looking.
}

func TestDeleteSchemaAndExport(t *testing.T) {
// first apply a schema
schema := `
type Person {
name: String
}`
schemaResp := updateGQLSchemaReturnSchema(t, schema, groupOneAdminServer)

// now delete it with S * * delete mutation
dg, err := testutil.DgraphClient(groupOnegRPC)
require.NoError(t, err)
txn := dg.NewTxn()
_, err = txn.Mutate(context.Background(), &api.Mutation{
DelNquads: []byte(fmt.Sprintf("<%s> * * .", schemaResp.Id)),
})
require.NoError(t, err)
require.NoError(t, txn.Commit(context.Background()))

// running an export shouldn't give any errors
exportReq := &common.GraphQLParams{
Query: `mutation {
export(input: {format: "rdf"}) {
exportedFiles
}
}`,
}
exportGqlResp := exportReq.ExecuteAsPost(t, groupOneAdminServer)
common.RequireNoGQLErrors(t, exportGqlResp)

// applying a new schema should still work
newSchemaResp := updateGQLSchemaReturnSchema(t, schema, groupOneAdminServer)
// we can assert that the uid allocated to new schema isn't same as the uid for old schema
require.NotEqual(t, schemaResp.Id, newSchemaResp.Id)
}

func updateGQLSchema(t *testing.T, schema, url string) *common.GraphQLResponse {
req := &common.GraphQLParams{
Query: `mutation updateGQLSchema($sch: String!) {
updateGQLSchema(input: { set: { schema: $sch }}) {
gqlSchema {
id
schema
}
}
Expand All @@ -449,6 +485,19 @@ func updateGQLSchemaRequireNoErrors(t *testing.T, schema, url string) {
require.Nil(t, updateGQLSchema(t, schema, url).Errors)
}

func updateGQLSchemaReturnSchema(t *testing.T, schema, url string) gqlSchema {
resp := updateGQLSchema(t, schema, url)
require.Nil(t, resp.Errors)

var updateResp struct {
UpdateGQLSchema struct {
GqlSchema gqlSchema
}
}
require.NoError(t, json.Unmarshal(resp.Data, &updateResp))
return updateResp.UpdateGQLSchema.GqlSchema
}

func updateGQLSchemaConcurrent(t *testing.T, schema, url string) bool {
res := updateGQLSchema(t, schema, url)
err := res.Errors.Error()
Expand Down
9 changes: 8 additions & 1 deletion worker/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,14 @@ func exportInternal(ctx context.Context, in *pb.ExportRequest, db *badger.DB,
if err != nil {
return nil, errors.Wrapf(err, "cannot read value of GraphQL schema")
}
if len(vals) != 1 {
// if the GraphQL schema node was deleted with S * * delete mutation,
// then the data key will be overwritten with nil value.
// So, just skip exporting it as there will be no value for this data key.
if len(vals) == 0 {
return nil, nil
}
// Give an error only if we find more than one value for the schema.
if len(vals) > 1 {
return nil, errors.Errorf("found multiple values for the GraphQL schema")
}
val, ok := vals[0].Value.([]byte)
Expand Down

0 comments on commit f739844

Please sign in to comment.