|
| 1 | +package coprocessor |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +type RouterLifecycleRequest struct { |
| 11 | + Stage string `json:"stage"` |
| 12 | + Body any `json:"body"` |
| 13 | +} |
| 14 | + |
| 15 | +type CommonProperties struct { |
| 16 | + Version int `json:"version"` |
| 17 | + Stage string `json:"stage"` |
| 18 | + ID string `json:"id,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type Headers struct { |
| 22 | + Headers http.Header `json:"headers,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +// The value of "control" coming from the router is always a string value of "continue" |
| 26 | +// If you don't modify it, or respond with it, and you don't respond with an object, everything proceeds normally |
| 27 | +// The only time you are going to modify it, you're going to set it to an object with a Break property and specific status code |
| 28 | +// https://www.apollographql.com/docs/router/customizations/coprocessor/#terminating-a-client-request |
| 29 | +type BreakControl struct { |
| 30 | + Break float64 `json:"break,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +type Context struct { |
| 34 | + Entries map[string]any `json:"entries"` |
| 35 | +} |
| 36 | + |
| 37 | +type Body struct { |
| 38 | + Errors []Error `json:"errors,omitempty"` |
| 39 | + Query string `json:"query,omitempty"` |
| 40 | + OperationName string `json:"operationName,omitempty"` |
| 41 | + Variables map[string]any `json:"variables,omitempty"` |
| 42 | + |
| 43 | + // RouterResponse Stage |
| 44 | + Data any `json:"data,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +type Error struct { |
| 48 | + Message string `json:"message,omitempty"` |
| 49 | + Extensions *Extension `json:"extensions,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +type Extension struct { |
| 53 | + Code string `json:"code,omitempty"` |
| 54 | + |
| 55 | + // Adding defaults to ErrorType and ErrorCode means even if there are no errors |
| 56 | + // JSON.Marshal sees this as non-nil and will erroneously add them to all responses |
| 57 | + ErrorType string `json:"errorType,omitempty"` |
| 58 | + ErrorCode string `json:"errorCode,omitempty"` |
| 59 | + ServiceName string `json:"serviceName,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +func RequestHandler(w http.ResponseWriter, r *http.Request) { |
| 63 | + var err error |
| 64 | + var response []byte |
| 65 | + |
| 66 | + cr, err := HandleRequest(w, r) |
| 67 | + if err != nil { |
| 68 | + logger.Error(err, "error handling coprocessor request") |
| 69 | + http.Error(w, fmt.Sprintf("error: %s", err), http.StatusInternalServerError) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + response, err = json.Marshal(&cr) |
| 74 | + if err != nil { |
| 75 | + logger.Error(err, "failed to marshal response") |
| 76 | + http.Error(w, fmt.Sprintf("failed to marshal response: %s", err), http.StatusInternalServerError) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + _, err = w.Write(response) |
| 81 | + if err != nil { |
| 82 | + logger.Error(err, "error writing coprocessor response") |
| 83 | + http.Error(w, fmt.Sprintf("error: %s", err), http.StatusInternalServerError) |
| 84 | + return |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func NewRequest(r *http.Request) (*[]byte, string, error) { |
| 89 | + var err error |
| 90 | + var cr *RouterLifecycleRequest |
| 91 | + |
| 92 | + httpRequestBody, err := io.ReadAll(r.Body) |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + return nil, "", fmt.Errorf("error reading request body: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // If the Router is configured to send data, this should never be empty |
| 99 | + // If it isn't configured to send data, it shouldn't call the coprocessor |
| 100 | + if len(httpRequestBody) == 0 { |
| 101 | + return nil, "", fmt.Errorf("error empty http request body at /%s", r.URL.Path[1:]) |
| 102 | + } |
| 103 | + |
| 104 | + err = json.Unmarshal(httpRequestBody, &cr) |
| 105 | + if err != nil { |
| 106 | + fmt.Println(err) |
| 107 | + return nil, "", err |
| 108 | + } |
| 109 | + |
| 110 | + return &httpRequestBody, cr.Stage, nil |
| 111 | +} |
0 commit comments