@@ -18,12 +18,15 @@ const (
18
18
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
19
19
)
20
20
21
+ type ResultCallbackFn func (ctx context.Context , params * graphql.Params , result * graphql.Result , responseBody []byte )
22
+
21
23
type Handler struct {
22
- Schema * graphql.Schema
23
- pretty bool
24
- graphiql bool
25
- playground bool
26
- rootObjectFn RootObjectFn
24
+ Schema * graphql.Schema
25
+ pretty bool
26
+ graphiql bool
27
+ playground bool
28
+ rootObjectFn RootObjectFn
29
+ resultCallbackFn ResultCallbackFn
27
30
}
28
31
type RequestOptions struct {
29
32
Query string `json:"query" url:"query" schema:"query"`
@@ -155,17 +158,22 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
155
158
// use proper JSON Header
156
159
w .Header ().Add ("Content-Type" , "application/json; charset=utf-8" )
157
160
161
+ var buff []byte
158
162
if h .pretty {
159
163
w .WriteHeader (http .StatusOK )
160
- buff , _ : = json .MarshalIndent (result , "" , "\t " )
164
+ buff , _ = json .MarshalIndent (result , "" , "\t " )
161
165
162
166
w .Write (buff )
163
167
} else {
164
168
w .WriteHeader (http .StatusOK )
165
- buff , _ : = json .Marshal (result )
169
+ buff , _ = json .Marshal (result )
166
170
167
171
w .Write (buff )
168
172
}
173
+
174
+ if h .resultCallbackFn != nil {
175
+ h .resultCallbackFn (ctx , & params , result , buff )
176
+ }
169
177
}
170
178
171
179
// ServeHTTP provides an entrypoint into executing graphQL queries.
@@ -177,11 +185,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
177
185
type RootObjectFn func (ctx context.Context , r * http.Request ) map [string ]interface {}
178
186
179
187
type Config struct {
180
- Schema * graphql.Schema
181
- Pretty bool
182
- GraphiQL bool
183
- Playground bool
184
- RootObjectFn RootObjectFn
188
+ Schema * graphql.Schema
189
+ Pretty bool
190
+ GraphiQL bool
191
+ Playground bool
192
+ RootObjectFn RootObjectFn
193
+ ResultCallbackFn ResultCallbackFn
185
194
}
186
195
187
196
func NewConfig () * Config {
@@ -202,10 +211,11 @@ func New(p *Config) *Handler {
202
211
}
203
212
204
213
return & Handler {
205
- Schema : p .Schema ,
206
- pretty : p .Pretty ,
207
- graphiql : p .GraphiQL ,
208
- playground : p .Playground ,
209
- rootObjectFn : p .RootObjectFn ,
214
+ Schema : p .Schema ,
215
+ pretty : p .Pretty ,
216
+ graphiql : p .GraphiQL ,
217
+ playground : p .Playground ,
218
+ rootObjectFn : p .RootObjectFn ,
219
+ resultCallbackFn : p .ResultCallbackFn ,
210
220
}
211
221
}
0 commit comments