Skip to content
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

Partial success (data + errors) from one service only returns errors #212

Closed
JohnStarich opened this issue Aug 19, 2024 · 0 comments · Fixed by #213
Closed

Partial success (data + errors) from one service only returns errors #212

JohnStarich opened this issue Aug 19, 2024 · 0 comments · Fixed by #213
Labels
bug Something isn't working

Comments

@JohnStarich
Copy link
Member

JohnStarich commented Aug 19, 2024

I can reproduce this with a failing test as well:

func TestDataAndErrorsBothReturnFromOneServicePartialSuccess(t *testing.T) {
	schema, err := graphql.LoadSchema(`
type Query {
	foo: String
}
`)
	require.NoError(t, err)
	queryerFactory := QueryerFactory(func(ctx *PlanningContext, url string) graphql.Queryer {
		return graphql.QueryerFunc(func(input *graphql.QueryInput) (interface{}, error) {
			data := map[string]interface{}{"foo": "bar"}
			err := graphql.ErrorList{&graphql.Error{Message: "baz"}}
			return data, err
		})
	})
	gateway, err := New([]*graphql.RemoteSchema{
		{Schema: schema, URL: "boo"},
	}, WithQueryerFactory(&queryerFactory))
	require.NoError(t, err)

	req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"query": "query { foo }"}`))
	resp := httptest.NewRecorder()
	gateway.GraphQLHandler(resp, req)
	assert.Equal(t, http.StatusOK, resp.Code)
	assert.JSONEq(t, `
		{
			"data": {
				"foo": "bar"
			},
			"errors": [
				{
					"message": "baz",
					"extensions": null
				}
			]
		}
	`, resp.Body.String())
}

Currently fails with:

➜ nautilus-gateway git:(wip/test-missing-data-with-errors) go test . -run TestDataAndErrorsBothReturnFromOneServicePartialSuccess
WARNING Network Error: baz                           
--- FAIL: TestDataAndErrorsBothReturnFromOneServicePartialSuccess (0.00s)
    gateway_test.go:783: 
                Error Trace:    /Users/johnstarich/Documents/IBM/Projects/nautilus-gateway/gateway_test.go:783
                Error:          Not equal: 
                                expected: map[string]interface {}{"data":map[string]interface {}{"foo":"bar"}, "errors":[]interface {}{map[string]interface {}{"extensions":interface {}(nil), "message":"baz"}}}
                                actual  : map[string]interface {}{"data":interface {}(nil), "errors":[]interface {}{map[string]interface {}{"extensions":interface {}(nil), "message":"baz"}}}
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,5 +1,3 @@
                                 (map[string]interface {}) (len=2) {
                                - (string) (len=4) "data": (map[string]interface {}) (len=1) {
                                -  (string) (len=3) "foo": (string) (len=3) "bar"
                                - },
                                + (string) (len=4) "data": (interface {}) <nil>,
                                  (string) (len=6) "errors": ([]interface {}) (len=1) {
                Test:           TestDataAndErrorsBothReturnFromOneServicePartialSuccess
FAIL
FAIL    github.com/nautilus/gateway     3.432s
FAIL
➜ nautilus-gateway git:(wip/test-missing-data-with-errors) 

The GraphQL spec says returning data and errors is expected:
https://spec.graphql.org/October2021/#sec-Response-Format

If the data entry in the response is present (including if it is the value null), the errors entry in the response may contain any field errors that were raised during execution. If field errors were raised during execution, it should contain those errors.

@JohnStarich JohnStarich added the bug Something isn't working label Aug 19, 2024
JohnStarich added a commit to nautilus/graphql that referenced this issue Aug 20, 2024
* Add failing tests for partial successes (data + errors)
* Return errors alongside result instead of just an error

Part of nautilus/gateway#212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant