Skip to content

Commit

Permalink
return null when data struct is empty (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlfaKaroui authored Jun 3, 2021
1 parent 747bc40 commit 5eee34e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (g *Gateway) Execute(ctx *RequestContext, plans QueryPlanList) (map[string]
// execute the plan and return the results
result, err := g.executor.Execute(executionContext)
if err != nil {
if len(result) == 0 {
return nil, err
}
return result, err
}

Expand Down
33 changes: 33 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ func TestGateway(t *testing.T) {
}
})

t.Run("Response Middleware Error Empty Data", func(t *testing.T) {
// create a new schema with the sources and some configuration
gateway, err := New(sources,
WithExecutor(ExecutorFunc(func(ctx *ExecutionContext) (map[string]interface{}, error) {
return map[string]interface{}{}, errors.New("error string")
})),
WithMiddlewares(
ResponseMiddleware(func(ctx *ExecutionContext, response map[string]interface{}) error {
return errors.New("this string")
}),
))
if err != nil {
t.Error(err.Error())
return
}

// build a query plan that the executor will follow
reqCtx := &RequestContext{
Context: context.Background(),
Query: "{ allUsers { firstName } }",
}
plans, err := gateway.GetPlans(reqCtx)
if err != nil {
t.Errorf("Encountered error building plan.")
}

res, err := gateway.Execute(reqCtx, plans)
if err == nil {
t.Errorf("Did not encounter error executing plan.")
}
assert.Nil(t, res)
})

t.Run("Response Middleware Success", func(t *testing.T) {
// create a new schema with the sources and some configuration
gateway, err := New(sources,
Expand Down
34 changes: 34 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,40 @@ func TestGraphQLHandler(t *testing.T) {
}
assert.Equal(t, result.Errors[0].Extensions["code"], "UNKNOWN_ERROR")
})

t.Run("internal server error response", func(t *testing.T) {
// create gateway schema we can test against
innerGateway, err := New([]*graphql.RemoteSchema{
{Schema: schema, URL: "url1"},
}, WithExecutor(ExecutorFunc(
func(*ExecutionContext) (map[string]interface{}, error) {
return nil, errors.New("error string")
},
)))

if err != nil {
t.Error(err.Error())
return
}

// the incoming request
request := httptest.NewRequest("GET", `/graphql?query={allUsers}`, strings.NewReader(""))
// a recorder so we can check what the handler responded with
responseRecorder := httptest.NewRecorder()

// call the http hander
innerGateway.GraphQLHandler(responseRecorder, request)

// make sure we got an error code
assert.Equal(t, http.StatusOK, responseRecorder.Result().StatusCode)

// verify the graphql error code
result, err := readResultWithErrors(responseRecorder, t)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, result.Errors[0].Extensions["code"], "INTERNAL_SERVER_ERROR")
})
}

func readResultWithErrors(responseRecorder *httptest.ResponseRecorder, t *testing.T) (*resultWithErrors, error) {
Expand Down

0 comments on commit 5eee34e

Please sign in to comment.