Skip to content

Commit

Permalink
fix(GraphQL): fix restoreStatus query with query variables (#6414) (#…
Browse files Browse the repository at this point in the history
…6424)

Fixes GRAPHQL-642.

For this restoreStatus query using variable
```
query restoreStatus($restoreId: Int!) {
	restoreStatus(restoreId: $restoreId) {
		status
		errors
	}
}
```
was giving this panic
```
panic: interface conversion: interface {} is json.Number, not int64.
```
Whereas the expected result should be
```
{
  "data": {
    "restoreStatus": {
      "status": "UNKNOWN",
      "errors": []
    }
  },
  "extensions": {}
}
```
This PR fixes this panic, Now `resolveStatus` with or without variable works fine.

(cherry picked from commit 45afae9)
  • Loading branch information
minhaj-shakeel authored Sep 21, 2020
1 parent d7feabd commit 3d2bfe7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
21 changes: 19 additions & 2 deletions graphql/admin/restore_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package admin
import (
"context"
"encoding/json"
"fmt"

"github.com/dgraph-io/dgraph/graphql/resolve"
"github.com/dgraph-io/dgraph/graphql/schema"
Expand All @@ -40,9 +41,25 @@ func unknownStatus(q schema.Query, err error) *resolve.Resolved {
}
}

func getRestoreStatusInput(q schema.Query) (int64, error) {
restoreId := q.ArgValue("restoreId")
switch v := restoreId.(type) {
case int64:
return v, nil
case json.Number:
return v.Int64()
default:
return -1, fmt.Errorf("Invalid value of restoreId")
}

}

func resolveRestoreStatus(ctx context.Context, q schema.Query) *resolve.Resolved {
restoreId := int(q.ArgValue("restoreId").(int64))
status, err := worker.ProcessRestoreStatus(ctx, restoreId)
restoreId, err := getRestoreStatusInput(q)
if err != nil {
return unknownStatus(q, err)
}
status, err := worker.ProcessRestoreStatus(ctx, int(restoreId))
if err != nil {
return unknownStatus(q, err)
}
Expand Down
38 changes: 38 additions & 0 deletions graphql/admin/restore_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package admin

import (
"encoding/json"
"strings"
"testing"

"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/graphql/test"
"github.com/stretchr/testify/require"
)

func TestRestoreStatus(t *testing.T) {
gqlSchema := test.LoadSchema(t, graphqlAdminSchema)
Query := `query restoreStatus($restoreId: Int!) {
restoreStatus(restoreId: $restoreId) {
status
errors
}
}`
variables := `{"restoreId": 2 }`
vars := make(map[string]interface{})
d := json.NewDecoder(strings.NewReader(variables))
d.UseNumber()
err := d.Decode(&vars)
require.NoError(t, err)

op, err := gqlSchema.Operation(
&schema.Request{
Query: Query,
Variables: vars,
})
require.NoError(t, err)
gqlQuery := test.GetQuery(t, op)
v, err := getRestoreStatusInput(gqlQuery)
require.NoError(t, err)
require.IsType(t, int64(2), v, nil)
}

0 comments on commit 3d2bfe7

Please sign in to comment.