Skip to content

Commit 7e815e2

Browse files
committed
added test for subscriptions parse errors
1 parent ccd052d commit 7e815e2

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

subscription.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Subscribe(p Params) chan *Result {
5454
if err != nil {
5555

5656
// merge the errors from extensions and the original error from parser
57-
return sendOneResultandClose(&Result{
57+
return sendOneResultAndClose(&Result{
5858
Errors: gqlerrors.FormatErrors(err),
5959
})
6060
}
@@ -64,7 +64,7 @@ func Subscribe(p Params) chan *Result {
6464

6565
if !validationResult.IsValid {
6666
// run validation finish functions for extensions
67-
return sendOneResultandClose(&Result{
67+
return sendOneResultAndClose(&Result{
6868
Errors: validationResult.Errors,
6969
})
7070

@@ -79,8 +79,8 @@ func Subscribe(p Params) chan *Result {
7979
})
8080
}
8181

82-
func sendOneResultandClose(res *Result) chan *Result {
83-
resultChannel := make(chan *Result)
82+
func sendOneResultAndClose(res *Result) chan *Result {
83+
resultChannel := make(chan *Result, 1) // TODO unbuffered channel does not pass errors, why?
8484
resultChannel <- res
8585
close(resultChannel)
8686
return resultChannel
@@ -113,7 +113,7 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
113113
fmt.Println("strange program path")
114114
return
115115
}
116-
sendOneResultandClose(&Result{
116+
sendOneResultAndClose(&Result{
117117
Errors: gqlerrors.FormatErrors(e),
118118
})
119119
}
@@ -132,15 +132,15 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
132132
})
133133

134134
if err != nil {
135-
sendOneResultandClose(&Result{
135+
sendOneResultAndClose(&Result{
136136
Errors: gqlerrors.FormatErrors(err),
137137
})
138138
return
139139
}
140140

141141
operationType, err := getOperationRootType(p.Schema, exeContext.Operation)
142142
if err != nil {
143-
sendOneResultandClose(&Result{
143+
sendOneResultAndClose(&Result{
144144
Errors: gqlerrors.FormatErrors(err),
145145
})
146146
return
@@ -163,7 +163,7 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
163163
fieldDef := getFieldDef(p.Schema, operationType, fieldName)
164164

165165
if fieldDef == nil {
166-
sendOneResultandClose(&Result{
166+
sendOneResultAndClose(&Result{
167167
Errors: gqlerrors.FormatErrors(fmt.Errorf("the subscription field %q is not defined", fieldName)),
168168
})
169169
return
@@ -172,7 +172,7 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
172172
resolveFn := fieldDef.Subscribe
173173

174174
if resolveFn == nil {
175-
sendOneResultandClose(&Result{
175+
sendOneResultAndClose(&Result{
176176
Errors: gqlerrors.FormatErrors(fmt.Errorf("the subscription function %q is not defined", fieldName)),
177177
})
178178
return
@@ -202,14 +202,14 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
202202
Context: p.Context,
203203
})
204204
if err != nil {
205-
sendOneResultandClose(&Result{
205+
sendOneResultAndClose(&Result{
206206
Errors: gqlerrors.FormatErrors(err),
207207
})
208208
return
209209
}
210210

211211
if fieldResult == nil {
212-
sendOneResultandClose(&Result{
212+
sendOneResultAndClose(&Result{
213213
Errors: gqlerrors.FormatErrors(fmt.Errorf("no field result")),
214214
})
215215
return

subscription_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,35 @@ func TestSchemaSubscribe(t *testing.T) {
8383
{Data: `{ "sub_without_resolver": "c" }`},
8484
},
8585
},
86+
{
87+
Name: "receive parse error",
88+
Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
89+
Name: "Subscription",
90+
Fields: graphql.Fields{
91+
"sub_without_resolver": &graphql.Field{
92+
Type: graphql.String,
93+
Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
94+
},
95+
},
96+
}),
97+
Query: `
98+
subscription onHelloSaid {
99+
sub_without_resolver
100+
xxx
101+
}
102+
`,
103+
ExpectedResults: []testutil.TestResponse{
104+
{Errors: []string{"Cannot query field \"xxx\" on type \"Subscription\"."}},
105+
},
106+
},
86107
{
87108
Name: "subscribe with resolver changes output",
88109
Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
89110
Name: "Subscription",
90111
Fields: graphql.Fields{
91112
"sub_with_resolver": &graphql.Field{
92113
Type: graphql.String,
93-
Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
114+
Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c", "d"}),
94115
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
95116
return fmt.Sprintf("result=%v", p.Source), nil
96117
},
@@ -106,6 +127,7 @@ func TestSchemaSubscribe(t *testing.T) {
106127
{Data: `{ "sub_with_resolver": "result=a" }`},
107128
{Data: `{ "sub_with_resolver": "result=b" }`},
108129
{Data: `{ "sub_with_resolver": "result=c" }`},
130+
{Data: `{ "sub_with_resolver": "result=d" }`},
109131
},
110132
},
111133
{

testutil/subscription.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
"testing"
1010

1111
"github.com/graphql-go/graphql"
12-
"github.com/graphql-go/graphql/gqlerrors"
1312
)
1413

1514
// TestResponse models the expected response
1615
type TestResponse struct {
1716
Data string
18-
Errors []gqlerrors.FormattedError
17+
Errors []string
1918
}
2019

2120
// TestSubscription is a GraphQL test case to be used with RunSubscribe.
@@ -26,7 +25,6 @@ type TestSubscription struct {
2625
OperationName string
2726
Variables map[string]interface{}
2827
ExpectedResults []TestResponse
29-
ExpectedErr error
3028
}
3129

3230
// RunSubscribes runs the given GraphQL subscription test cases as subtests.
@@ -75,15 +73,18 @@ func RunSubscribe(t *testing.T, test *TestSubscription) {
7573
}
7674
res := results[i]
7775

78-
checkErrorStrings(t, expected.Errors, res.Errors)
79-
80-
resData, err := json.MarshalIndent(res.Data, "", " ")
81-
if err != nil {
82-
t.Fatal(err)
76+
var errs []string
77+
for _, err := range res.Errors {
78+
errs = append(errs, err.Message)
8379
}
80+
checkErrorStrings(t, expected.Errors, errs)
81+
if expected.Data == "" {
82+
continue
83+
}
84+
8485
got, err := json.MarshalIndent(res.Data, "", " ")
8586
if err != nil {
86-
t.Fatalf("got: invalid JSON: %s; raw: %s", err, resData)
87+
t.Fatalf("got: invalid JSON: %s; raw: %s", err, got)
8788
}
8889

8990
if err != nil {
@@ -102,19 +103,19 @@ func RunSubscribe(t *testing.T, test *TestSubscription) {
102103
}
103104
}
104105

105-
func checkErrorStrings(t *testing.T, expected, actual []gqlerrors.FormattedError) {
106+
func checkErrorStrings(t *testing.T, expected, actual []string) {
106107
expectedCount, actualCount := len(expected), len(actual)
107108

108109
if expectedCount != actualCount {
109-
t.Fatalf("unexpected number of errors: want %d, got %d", expectedCount, actualCount)
110+
t.Fatalf("unexpected number of errors: want `%d`, got `%d`", expectedCount, actualCount)
110111
}
111112

112113
if expectedCount > 0 {
113114
for i, want := range expected {
114115
got := actual[i]
115116

116-
if got.Error() != want.Error() {
117-
t.Fatalf("unexpected error: got %+v, want %+v", got, want)
117+
if got != want {
118+
t.Fatalf("unexpected error: got `%+v`, want `%+v`", got, want)
118119
}
119120
}
120121

0 commit comments

Comments
 (0)