@@ -43,8 +43,6 @@ func (rr *RouterRequest) UnmarshalJSON(data []byte) error {
43
43
// 1. Unmarshal everything but Body
44
44
if err = json .Unmarshal (data , & tmpRequest ); err != nil {
45
45
46
- fmt .Println ("Can we even unmarshal something? ," , data )
47
-
48
46
return fmt .Errorf ("failed to unmarshal router request: %v" , err )
49
47
}
50
48
@@ -94,16 +92,22 @@ func (rr *RouterRequest) MarshalJSON() ([]byte, error) {
94
92
* TmpType
95
93
}
96
94
97
- var jsonBody []byte
98
- jsonBody , err = json .Marshal (rr .Body )
99
- if err != nil {
100
- logger .Error (err , "failed to marshal coprocessor request string into struct: %v" )
95
+ routerRequestBodyOverride := & RouterRequestBodyOverride {
96
+ TmpType : (* TmpType )(rr ),
101
97
}
102
98
103
- return json .Marshal (& RouterRequestBodyOverride {
104
- Body : string (jsonBody ),
105
- TmpType : (* TmpType )(rr ),
106
- })
99
+ if rr .Body != nil {
100
+ var jsonBody []byte
101
+ jsonBody , err = json .Marshal (rr .Body )
102
+
103
+ if err != nil {
104
+ logger .Error (err , "failed to marshal coprocessor request string into struct: %v" )
105
+ }
106
+
107
+ routerRequestBodyOverride .Body = string (jsonBody )
108
+ }
109
+
110
+ return json .Marshal (routerRequestBodyOverride )
107
111
}
108
112
109
113
type RouterResponse struct {
@@ -185,27 +189,44 @@ func (rr *RouterResponse) MarshalJSON() ([]byte, error) {
185
189
* TmpType
186
190
}
187
191
188
- var jsonBody []byte
189
- jsonBody , err = json .Marshal (rr .Body )
190
- if err != nil {
191
- logger .Error (err , "failed to unmarshal coprocessor request string into struct: %v" )
192
+ routerRequestBodyOverride := & RouterRequestBodyOverride {
193
+ TmpType : (* TmpType )(rr ),
192
194
}
193
195
194
- return json .Marshal (& RouterRequestBodyOverride {
195
- Body : string (jsonBody ),
196
- TmpType : (* TmpType )(rr ),
197
- })
196
+ if rr .Body != nil {
197
+ var jsonBody []byte
198
+ jsonBody , err = json .Marshal (rr .Body )
199
+
200
+ if err != nil {
201
+ logger .Error (err , "failed to marshal coprocessor request string into struct: %v" )
202
+ }
203
+
204
+ routerRequestBodyOverride .Body = string (jsonBody )
205
+ }
206
+
207
+ return json .Marshal (routerRequestBodyOverride )
198
208
}
199
209
200
210
func handleRouterRequest (httpRequestBody * []byte ) (* RouterRequest , error ) {
201
211
cr , err := NewRouterRequest (httpRequestBody )
202
212
203
- fmt .Println ("Request: " , cr )
204
-
205
213
if err != nil {
206
214
return nil , fmt .Errorf ("error unmarshaling httpRequestBody: %w" , err )
207
215
}
208
216
217
+ // Normally you won't Marshal like this, this is simply to get a stringified
218
+ // representation of the request in the console
219
+ requestBody , err := json .Marshal (cr )
220
+
221
+ if err != nil {
222
+ logger .Error (err , "failed to unmarshal coprocessor request string into struct: %v" )
223
+ }
224
+
225
+ // This is the object sent by the Router that you can act upon to update headers, context, auth claims, etc
226
+ // If you update the "control" property from "Continue" to something like { "break": 400 }, it will terminate the request and return the specified HTTP error
227
+ // See: https://www.apollographql.com/docs/router/customizations/coprocessor/
228
+ fmt .Println (string (requestBody ))
229
+
209
230
return cr , nil
210
231
}
211
232
@@ -222,7 +243,6 @@ func NewRouterRequest(httpRequestBody *[]byte) (*RouterRequest, error) {
222
243
var cr * RouterRequest
223
244
err = json .Unmarshal (* httpRequestBody , & cr )
224
245
if err != nil {
225
- fmt .Println ("Error on unmarshal: " , err , & cr )
226
246
return nil , err
227
247
}
228
248
return cr , nil
0 commit comments