Skip to content

Commit ccd052d

Browse files
committed
initial tests pass
1 parent c991585 commit ccd052d

File tree

3 files changed

+53
-42
lines changed

3 files changed

+53
-42
lines changed

subscription.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
110110
if err := recover(); err != nil {
111111
e, ok := err.(error)
112112
if !ok {
113+
fmt.Println("strange program path")
113114
return
114115
}
115116
sendOneResultandClose(&Result{
@@ -217,16 +218,17 @@ func ExecuteSubscription(p ExecuteParams) chan *Result {
217218
switch fieldResult.(type) {
218219
case chan interface{}:
219220
sub := fieldResult.(chan interface{})
220-
defer close(resultChannel)
221221
for {
222222
select {
223223
case <-p.Context.Done():
224224
println("context cancelled")
225+
close(resultChannel)
225226
// TODO send the context error to the resultchannel
226227
return
227228

228229
case res, more := <-sub:
229230
if !more {
231+
close(resultChannel)
230232
return
231233
}
232234
resultChannel <- mapSourceToResponse(res)

subscription_test.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestSchemaSubscribe(t *testing.T) {
6666
"sub_without_resolver": &graphql.Field{
6767
Type: graphql.String,
6868
Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
69-
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
69+
Resolve: func(p graphql.ResolveParams) (interface{}, error) { // TODO remove dummy resolver
7070
return p.Source, nil
7171
},
7272
},
@@ -108,44 +108,46 @@ func TestSchemaSubscribe(t *testing.T) {
108108
{Data: `{ "sub_with_resolver": "result=c" }`},
109109
},
110110
},
111-
// {
112-
// Name: "subscribe to a nested object",
113-
// Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
114-
// Name: "Subscription",
115-
// Fields: graphql.Fields{
116-
// "sub_with_object": &graphql.Field{
117-
// Type: graphql.String,
118-
// Subscribe: makeSubscribeToMapFunction([]map[string]interface{}{
119-
// {
120-
// "field": "hello",
121-
// "obj": map[string]interface{}{
122-
// "field": "hello",
123-
// },
124-
// },
125-
// {
126-
// "field": "bye",
127-
// "obj": map[string]interface{}{
128-
// "field": "bye",
129-
// },
130-
// },
131-
// }),
132-
// },
133-
// },
134-
// }),
135-
// Query: `
136-
// subscription onHelloSaid {
137-
// sub_with_object {
138-
// field
139-
// obj {
140-
// field
141-
// }
142-
// }
143-
// }
144-
// `,
145-
// ExpectedResults: []testutil.TestResponse{
146-
// {Data: `{ "sub_with_object": { "field": "hello", "obj": { "field": "hello" } } }`},
147-
// },
148-
// },
111+
{
112+
Name: "subscribe to a nested object",
113+
Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
114+
Name: "Subscription",
115+
Fields: graphql.Fields{
116+
"sub_with_object": &graphql.Field{
117+
Type: graphql.NewObject(graphql.ObjectConfig{
118+
Name: "Obj",
119+
Fields: graphql.Fields{
120+
"field": &graphql.Field{
121+
Type: graphql.String,
122+
},
123+
},
124+
}),
125+
Resolve: func(p graphql.ResolveParams) (interface{}, error) { // TODO remove dummy resolver
126+
return p.Source, nil
127+
},
128+
Subscribe: makeSubscribeToMapFunction([]map[string]interface{}{
129+
{
130+
"field": "hello",
131+
},
132+
{
133+
"field": "bye",
134+
},
135+
}),
136+
},
137+
},
138+
}),
139+
Query: `
140+
subscription onHelloSaid {
141+
sub_with_object {
142+
field
143+
}
144+
}
145+
`,
146+
ExpectedResults: []testutil.TestResponse{
147+
{Data: `{ "sub_with_object": { "field": "hello" } }`},
148+
{Data: `{ "sub_with_object": { "field": "bye" } }`},
149+
},
150+
},
149151

150152
// {
151153
// Name: "parse_errors",

testutil/subscription.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/json"
77
"errors"
8-
"fmt"
98
"strconv"
109
"testing"
1110

@@ -65,7 +64,7 @@ func RunSubscribe(t *testing.T, test *TestSubscription) {
6564

6665
var results []*graphql.Result
6766
for res := range c {
68-
fmt.Println(res)
67+
println(pretty(res))
6968
results = append(results, res)
7069
}
7170

@@ -139,3 +138,11 @@ func formatJSON(data string) ([]byte, error) {
139138
}
140139
return formatted, nil
141140
}
141+
142+
func pretty(x interface{}) string {
143+
got, err := json.MarshalIndent(x, "", " ")
144+
if err != nil {
145+
panic(err)
146+
}
147+
return string(got)
148+
}

0 commit comments

Comments
 (0)