sendWebhookEvent forms an HTTP payload required for the webhooks configured with @lambdaOnMutate directive, and then sends that payload to the lambda URL configured with Alpha. There is no guarantee that the payload will be delivered successfully to the lambda server.
(ctx context.Context, m schema.Mutation, commitTs uint64, rootUIDs []string)
| 58 | // directive, and then sends that payload to the lambda URL configured with Alpha. There is no |
| 59 | // guarantee that the payload will be delivered successfully to the lambda server. |
| 60 | func sendWebhookEvent(ctx context.Context, m schema.Mutation, commitTs uint64, rootUIDs []string) { |
| 61 | accessJWT, _ := x.ExtractJwt(ctx) |
| 62 | var authHeader *authHeaderPayload |
| 63 | if m.GetAuthMeta() != nil { |
| 64 | authHeader = &authHeaderPayload{ |
| 65 | Key: m.GetAuthMeta().GetHeader(), |
| 66 | Value: authorization.GetJwtToken(ctx), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | payload := webhookPayload{ |
| 71 | Resolver: "$webhook", |
| 72 | AccessJWT: accessJWT, |
| 73 | AuthHeader: authHeader, |
| 74 | Event: eventPayload{ |
| 75 | Typename: m.MutatedType().Name(), |
| 76 | Operation: m.MutationType(), |
| 77 | CommitTs: commitTs, |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | switch payload.Event.Operation { |
| 82 | case schema.AddMutation: |
| 83 | input, _ := m.ArgValue(schema.InputArgName).([]interface{}) |
| 84 | payload.Event.Add = &addEvent{ |
| 85 | RootUIDs: rootUIDs, |
| 86 | Input: input, |
| 87 | } |
| 88 | case schema.UpdateMutation: |
| 89 | inp, _ := m.ArgValue(schema.InputArgName).(map[string]interface{}) |
| 90 | payload.Event.Update = &updateEvent{ |
| 91 | RootUIDs: rootUIDs, |
| 92 | SetPatch: inp["set"], |
| 93 | RemovePatch: inp["remove"], |
| 94 | } |
| 95 | case schema.DeleteMutation: |
| 96 | payload.Event.Delete = &deleteEvent{RootUIDs: rootUIDs} |
| 97 | } |
| 98 | |
| 99 | b, err := json.Marshal(payload) |
| 100 | if err != nil { |
| 101 | glog.Error(errors.Wrap(err, "error marshalling webhook payload")) |
| 102 | // don't care to send the payload if there are JSON marshalling errors |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | // send the request |
| 107 | ns, _ := x.ExtractNamespace(ctx) |
| 108 | headers := http.Header{} |
| 109 | headers.Set("Content-Type", "application/json") |
| 110 | resp, err := schema.MakeHttpRequest(nil, http.MethodPost, x.LambdaUrl(ns), headers, b) |
| 111 | |
| 112 | // just log the response errors, if any. |
| 113 | if err != nil { |
| 114 | glog.V(3).Info(errors.Wrap(err, "unable to send webhook event")) |
| 115 | return |
| 116 | } |
| 117 |
no test coverage detected