Skip to content

Commit 1860af8

Browse files
committed
Fix a nil check issue when Marshalling the response and add a couple comments
1 parent 08423f0 commit 1860af8

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

golang-coprocessor/internal/stages_router.go

+41-21
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func (rr *RouterRequest) UnmarshalJSON(data []byte) error {
4343
// 1. Unmarshal everything but Body
4444
if err = json.Unmarshal(data, &tmpRequest); err != nil {
4545

46-
fmt.Println("Can we even unmarshal something? ,", data)
47-
4846
return fmt.Errorf("failed to unmarshal router request: %v", err)
4947
}
5048

@@ -94,16 +92,22 @@ func (rr *RouterRequest) MarshalJSON() ([]byte, error) {
9492
*TmpType
9593
}
9694

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),
10197
}
10298

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)
107111
}
108112

109113
type RouterResponse struct {
@@ -185,27 +189,44 @@ func (rr *RouterResponse) MarshalJSON() ([]byte, error) {
185189
*TmpType
186190
}
187191

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),
192194
}
193195

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)
198208
}
199209

200210
func handleRouterRequest(httpRequestBody *[]byte) (*RouterRequest, error) {
201211
cr, err := NewRouterRequest(httpRequestBody)
202212

203-
fmt.Println("Request: ", cr)
204-
205213
if err != nil {
206214
return nil, fmt.Errorf("error unmarshaling httpRequestBody: %w", err)
207215
}
208216

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+
209230
return cr, nil
210231
}
211232

@@ -222,7 +243,6 @@ func NewRouterRequest(httpRequestBody *[]byte) (*RouterRequest, error) {
222243
var cr *RouterRequest
223244
err = json.Unmarshal(*httpRequestBody, &cr)
224245
if err != nil {
225-
fmt.Println("Error on unmarshal: ", err, &cr)
226246
return nil, err
227247
}
228248
return cr, nil

0 commit comments

Comments
 (0)