forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testexecutor.go
201 lines (175 loc) · 5.11 KB
/
testexecutor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package testexecutor
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/executor"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
)
type MockResponse struct {
Name string `json:"name"`
}
func (mr *MockResponse) UnmarshalGQL(v interface{}) error {
return nil
}
func (mr *MockResponse) MarshalGQL(w io.Writer) {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(mr)
if err != nil {
panic(err)
}
ba := bytes.NewBuffer(bytes.TrimRight(buf.Bytes(), "\n"))
fmt.Fprint(w, ba)
}
// New provides a server for use in tests that isn't relying on generated code. It isnt a perfect reproduction of
// a generated server, but it aims to be good enough to test the handler package without relying on codegen.
func New() *TestExecutor {
next := make(chan struct{})
schema := gqlparser.MustLoadSchema(&ast.Source{Input: `
type Query {
name: String!
find(id: Int!): String!
}
type Mutation {
name: String!
}
type Subscription {
name: String!
}
`})
exec := &TestExecutor{
next: next,
}
exec.schema = &graphql.ExecutableSchemaMock{
ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
switch rc.Operation.Operation {
case ast.Query:
ran := false
return func(ctx context.Context) *graphql.Response {
if ran {
return nil
}
ran = true
// Field execution happens inside the generated code, lets simulate some of it.
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Object: "Query",
Field: graphql.CollectedField{
Field: &ast.Field{
Name: "name",
Alias: "name",
Definition: schema.Types["Query"].Fields.ForName("name"),
},
},
})
data := graphql.GetOperationContext(ctx).RootResolverMiddleware(ctx, func(ctx context.Context) graphql.Marshaler {
res, err := graphql.GetOperationContext(ctx).ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
// return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
return &MockResponse{Name: "test"}, nil
})
if err != nil {
panic(err)
}
return res.(*MockResponse)
})
var buf bytes.Buffer
data.MarshalGQL(&buf)
return &graphql.Response{Data: buf.Bytes()}
}
case ast.Mutation:
return graphql.OneShot(graphql.ErrorResponse(ctx, "mutations are not supported"))
case ast.Subscription:
return func(context context.Context) *graphql.Response {
select {
case <-ctx.Done():
return nil
case <-next:
return &graphql.Response{
Data: []byte(`{"name":"test"}`),
}
}
}
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
}
},
SchemaFunc: func() *ast.Schema {
return schema
},
ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (i int, b bool) {
return exec.complexity, true
},
}
exec.Executor = executor.New(exec.schema)
return exec
}
// NewError provides a server for use in resolver error tests that isn't relying on generated code. It isnt a perfect reproduction of
// a generated server, but it aims to be good enough to test the handler package without relying on codegen.
func NewError() *TestExecutor {
next := make(chan struct{})
schema := gqlparser.MustLoadSchema(&ast.Source{Input: `
type Query {
name: String!
}
`})
exec := &TestExecutor{
next: next,
}
exec.schema = &graphql.ExecutableSchemaMock{
ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
switch rc.Operation.Operation {
case ast.Query:
ran := false
return func(ctx context.Context) *graphql.Response {
if ran {
return nil
}
ran = true
graphql.AddError(ctx, fmt.Errorf("resolver error"))
return &graphql.Response{
Data: []byte(`null`),
}
}
case ast.Mutation:
return graphql.OneShot(graphql.ErrorResponse(ctx, "mutations are not supported"))
case ast.Subscription:
return graphql.OneShot(graphql.ErrorResponse(ctx, "subscription are not supported"))
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
}
},
SchemaFunc: func() *ast.Schema {
return schema
},
ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (i int, b bool) {
return exec.complexity, true
},
}
exec.Executor = executor.New(exec.schema)
return exec
}
type TestExecutor struct {
*executor.Executor
schema graphql.ExecutableSchema
next chan struct{}
complexity int
}
func (e *TestExecutor) Schema() graphql.ExecutableSchema {
return e.schema
}
func (e *TestExecutor) SendNextSubscriptionMessage() {
select {
case e.next <- struct{}{}:
case <-time.After(1 * time.Second):
fmt.Println("WARNING: no active subscription")
}
}
func (e *TestExecutor) SetCalculatedComplexity(complexity int) {
e.complexity = complexity
}