MCPcopy
hub / github.com/dgraph-io/dgraph / sendWebhookEvent

Function sendWebhookEvent

graphql/resolve/webhook.go:60–127  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
60func 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

Callers 1

rewriteAndExecuteMethod · 0.85

Calls 15

ExtractJwtFunction · 0.92
GetJwtTokenFunction · 0.92
ExtractNamespaceFunction · 0.92
MakeHttpRequestFunction · 0.92
LambdaUrlFunction · 0.92
GetHeaderMethod · 0.80
InfoMethod · 0.80
GetAuthMetaMethod · 0.65
NameMethod · 0.65
MutatedTypeMethod · 0.65
MutationTypeMethod · 0.65
ArgValueMethod · 0.65

Tested by

no test coverage detected