Skip to content

Commit 2b63a00

Browse files
committed
handle errors in ExecuteSubscribe
1 parent 7e815e2 commit 2b63a00

File tree

2 files changed

+50
-65
lines changed

2 files changed

+50
-65
lines changed

subscription.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
106106
}
107107
var resultChannel = make(chan *Result)
108108
go func() {
109+
defer close(resultChannel)
109110
defer func() {
110111
if err := recover(); err != nil {
111112
e, ok := err.(error)
112113
if !ok {
113114
fmt.Println("strange program path")
114115
return
115116
}
116-
sendOneResultAndClose(&Result{
117+
resultChannel <- &Result{
117118
Errors: gqlerrors.FormatErrors(e),
118-
})
119+
}
119120
}
120-
// close(resultChannel)
121121
return
122122
}()
123123

@@ -132,17 +132,19 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
132132
})
133133

134134
if err != nil {
135-
sendOneResultAndClose(&Result{
135+
resultChannel <- &Result{
136136
Errors: gqlerrors.FormatErrors(err),
137-
})
137+
}
138+
138139
return
139140
}
140141

141142
operationType, err := getOperationRootType(p.Schema, exeContext.Operation)
142143
if err != nil {
143-
sendOneResultAndClose(&Result{
144+
resultChannel <- &Result{
144145
Errors: gqlerrors.FormatErrors(err),
145-
})
146+
}
147+
146148
return
147149
}
148150

@@ -163,18 +165,20 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
163165
fieldDef := getFieldDef(p.Schema, operationType, fieldName)
164166

165167
if fieldDef == nil {
166-
sendOneResultAndClose(&Result{
168+
resultChannel <- &Result{
167169
Errors: gqlerrors.FormatErrors(fmt.Errorf("the subscription field %q is not defined", fieldName)),
168-
})
170+
}
171+
169172
return
170173
}
171174

172175
resolveFn := fieldDef.Subscribe
173176

174177
if resolveFn == nil {
175-
sendOneResultAndClose(&Result{
178+
resultChannel <- &Result{
176179
Errors: gqlerrors.FormatErrors(fmt.Errorf("the subscription function %q is not defined", fieldName)),
177-
})
180+
}
181+
178182
return
179183
}
180184
fieldPath := &ResponsePath{
@@ -202,16 +206,18 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
202206
Context: p.Context,
203207
})
204208
if err != nil {
205-
sendOneResultAndClose(&Result{
209+
resultChannel <- &Result{
206210
Errors: gqlerrors.FormatErrors(err),
207-
})
211+
}
212+
208213
return
209214
}
210215

211216
if fieldResult == nil {
212-
sendOneResultAndClose(&Result{
217+
resultChannel <- &Result{
213218
Errors: gqlerrors.FormatErrors(fmt.Errorf("no field result")),
214-
})
219+
}
220+
215221
return
216222
}
217223

@@ -222,13 +228,13 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
222228
select {
223229
case <-p.Context.Done():
224230
println("context cancelled")
225-
close(resultChannel)
231+
226232
// TODO send the context error to the resultchannel
227233
return
228234

229235
case res, more := <-sub:
230236
if !more {
231-
close(resultChannel)
237+
232238
return
233239
}
234240
resultChannel <- mapSourceToResponse(res)
@@ -237,7 +243,7 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
237243
default:
238244
fmt.Println(fieldResult)
239245
resultChannel <- mapSourceToResponse(fieldResult)
240-
close(resultChannel)
246+
241247
return
242248
}
243249
}()

subscription_test.go

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql_test
22

33
import (
4+
"errors"
45
"fmt"
56
"testing"
67

@@ -84,7 +85,7 @@ func TestSchemaSubscribe(t *testing.T) {
8485
},
8586
},
8687
{
87-
Name: "receive parse error",
88+
Name: "receive query validation error",
8889
Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
8990
Name: "Subscription",
9091
Fields: graphql.Fields{
@@ -171,52 +172,30 @@ func TestSchemaSubscribe(t *testing.T) {
171172
},
172173
},
173174

174-
// {
175-
// Name: "parse_errors",
176-
// Schema: schema,
177-
// Query: `invalid graphQL query`,
178-
// ExpectedResults: []testutil.TestResponse{
179-
// {
180-
// Errors: []gqlerrors.FormattedError{{Message: ""}},
181-
// },
182-
// },
183-
// },
184-
// {
185-
// Name: "subscribe_to_query_succeeds",
186-
// Schema: schema,
187-
// Query: `
188-
// query Hello {
189-
// hello
190-
// }
191-
// `,
192-
// ExpectedResults: []testutil.TestResponse{
193-
// {
194-
// Data: json.RawMessage(`
195-
// {
196-
// "hello": "Hello world!"
197-
// }
198-
// `),
199-
// },
200-
// },
201-
// },
202-
// {
203-
// Name: "subscription_resolver_can_error",
204-
// Schema: schema,
205-
// Query: `
206-
// subscription onHelloSaid {
207-
// helloSaid {
208-
// msg
209-
// }
210-
// }
211-
// `,
212-
// ExpectedResults: []testutil.TestResponse{
213-
// {
214-
// Data: json.RawMessage(`
215-
// null
216-
// `),
217-
// Errors: []gqlerrors.FormattedError{{Message: ""}}},
218-
// },
219-
// },
175+
{
176+
Name: "subscription_resolver_can_error",
177+
Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
178+
Name: "Subscription",
179+
Fields: graphql.Fields{
180+
"should_error": &graphql.Field{
181+
Type: graphql.String,
182+
Subscribe: func(p graphql.ResolveParams) (interface{}, error) {
183+
return nil, errors.New("got a subscribe error")
184+
},
185+
},
186+
},
187+
}),
188+
Query: `
189+
subscription {
190+
should_error
191+
}
192+
`,
193+
ExpectedResults: []testutil.TestResponse{
194+
{
195+
Errors: []string{"got a subscribe error"},
196+
},
197+
},
198+
},
220199
// {
221200
// Name: "subscription_resolver_can_error_optional_msg",
222201
// Schema: schema,

0 commit comments

Comments
 (0)