From 96dec74e60545255a31baabb4f810d705b7b5c1b Mon Sep 17 00:00:00 2001 From: mjarkk Date: Thu, 14 Oct 2021 14:44:26 +0200 Subject: [PATCH] resolver: do not return extensions or errors if there are non --- implement_helpers_test.go | 8 ++++---- readme_test.go | 2 +- resolver.go | 6 ++---- resolver_test.go | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/implement_helpers_test.go b/implement_helpers_test.go index 080c4eb..9814e1f 100644 --- a/implement_helpers_test.go +++ b/implement_helpers_test.go @@ -31,7 +31,7 @@ func TestHandleRequestRequestInURL(t *testing.T) { for _, err := range errs { panic(err) } - a.Equal(t, `{"data":{"a":{"bar":"baz"}},"errors":[],"extensions":{}}`, string(res)) + a.Equal(t, `{"data":{"a":{"bar":"baz"}}}`, string(res)) } func TestHandleRequestRequestJsonBody(t *testing.T) { @@ -71,7 +71,7 @@ func TestHandleRequestRequestJsonBody(t *testing.T) { for _, err := range errs { panic(err) } - a.Equal(t, `{"data":{"a":{"bar":"baz"}},"errors":[],"extensions":{}}`, string(res)) + a.Equal(t, `{"data":{"a":{"bar":"baz"}}}`, string(res)) } func TestHandleRequestRequestForm(t *testing.T) { @@ -115,7 +115,7 @@ func TestHandleRequestRequestForm(t *testing.T) { for _, err := range errs { panic(err) } - a.Equal(t, `{"data":{"a":{"bar":"baz"}},"errors":[],"extensions":{}}`, string(res)) + a.Equal(t, `{"data":{"a":{"bar":"baz"}}}`, string(res)) } func TestHandleRequestRequestBatch(t *testing.T) { @@ -162,5 +162,5 @@ func TestHandleRequestRequestBatch(t *testing.T) { for _, err := range errs { panic(err) } - a.Equal(t, `[{"data":{"a":{"bar":"baz"}},"errors":[],"extensions":{}},{"data":{"a":{"foo":null}},"errors":[],"extensions":{}}]`, string(res)) + a.Equal(t, `[{"data":{"a":{"bar":"baz"}}},{"data":{"a":{"foo":null}}}]`, string(res)) } diff --git a/readme_test.go b/readme_test.go index 11c4dc8..7595991 100644 --- a/readme_test.go +++ b/readme_test.go @@ -46,5 +46,5 @@ func TestReadmeExample(t *testing.T) { log.Fatal(err) } - a.Equal(t, `{"data":{"posts":[{"id":"1","name":"post 1"},{"id":"2","name":"post 2"},{"id":"3","name":"post 3"}]},"errors":[],"extensions":{}}`, string(s.Result)) + a.Equal(t, `{"data":{"posts":[{"id":"1","name":"post 1"},{"id":"2","name":"post 2"},{"id":"3","name":"post 3"}]}}`, string(s.Result)) } diff --git a/resolver.go b/resolver.go index 9d2262e..082bc9d 100644 --- a/resolver.go +++ b/resolver.go @@ -223,11 +223,9 @@ func (s *Schema) Resolve(query []byte, opts ResolveOptions) []error { // Add errors to output errsLen := len(ctx.query.Errors) if errsLen == 0 && !ctx.tracingEnabled { - ctx.write([]byte(`,"errors":[],"extensions":{}}`)) + ctx.write([]byte(`}`)) } else { - if errsLen == 0 { - ctx.write([]byte(`,"errors":[]`)) - } else { + if errsLen != 0 { ctx.write([]byte(`,"errors":[`)) for i, err := range ctx.query.Errors { if i > 0 { diff --git a/resolver_test.go b/resolver_test.go index 3899e16..bdd3fec 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -399,7 +399,7 @@ func TestBytecodeResolveCorrectMeta(t *testing.T) { if !json.Valid([]byte(res)) { panic("invalid json: " + res) } - a.Equal(t, `{"data":{"a":{"foo":null,"bar":""},"b":{"baz":""}},"errors":[],"extensions":{}}`, res) + a.Equal(t, `{"data":{"a":{"foo":null,"bar":""},"b":{"baz":""}}}`, res) } func TestBytecodeResolveCorrectMetaWithError(t *testing.T) {