1package handler
2
3import "context"
4
5type key string
6
7const (
8 initpayload key = "ws_initpayload_context"
9)
10
11// InitPayload is a structure that is parsed from the websocket init message payload. TO use
12// request headers for non-websocket, instead wrap the graphql handler in a middleware.
13type InitPayload map[string]interface{}
14
15// GetString safely gets a string value from the payload. It returns an empty string if the
16// payload is nil or the value isn't set.
17func (payload InitPayload) GetString(key string) string {
18 if payload == nil {
19 return ""
20 }
21
22 if value, ok := payload[key]; ok {
23 res, _ := value.(string)
24 return res
25 }
26
27 return ""
28}
29
30// Authorization is a short hand for getting the Authorization header from the
31// payload.
32func (payload InitPayload) Authorization() string {
33 if value := payload.GetString("Authorization"); value != "" {
34 return value
35 }
36
37 if value := payload.GetString("authorization"); value != "" {
38 return value
39 }
40
41 return ""
42}
43
44func withInitPayload(ctx context.Context, payload InitPayload) context.Context {
45 return context.WithValue(ctx, initpayload, payload)
46}
47
48// GetInitPayload gets a map of the data sent with the connection_init message, which is used by
49// graphql clients as a stand-in for HTTP headers.
50func GetInitPayload(ctx context.Context) InitPayload {
51 payload, ok := ctx.Value(initpayload).(InitPayload)
52 if !ok {
53 return nil
54 }
55
56 return payload
57}