Skip to content

fix(export): don't return an error if there was no GraphQL schema #6815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions graphql/e2e/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,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 @@ -674,6 +710,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 @@ -677,7 +677,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