aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/handler/context.go
blob: 2992aa3d4fe15fa1a367d26ef0016127bae5c21c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package handler

import "context"

type key string

const (
	initpayload key = "ws_initpayload_context"
)

// InitPayload is a structure that is parsed from the websocket init message payload. TO use
// request headers for non-websocket, instead wrap the graphql handler in a middleware.
type InitPayload map[string]interface{}

// GetString safely gets a string value from the payload. It returns an empty string if the
// payload is nil or the value isn't set.
func (payload InitPayload) GetString(key string) string {
	if payload == nil {
		return ""
	}

	if value, ok := payload[key]; ok {
		res, _ := value.(string)
		return res
	}

	return ""
}

// Authorization is a short hand for getting the Authorization header from the
// payload.
func (payload InitPayload) Authorization() string {
	if value := payload.GetString("Authorization"); value != "" {
		return value
	}

	if value := payload.GetString("authorization"); value != "" {
		return value
	}

	return ""
}

func withInitPayload(ctx context.Context, payload InitPayload) context.Context {
	return context.WithValue(ctx, initpayload, payload)
}

// GetInitPayload gets a map of the data sent with the connection_init message, which is used by
// graphql clients as a stand-in for HTTP headers.
func GetInitPayload(ctx context.Context) InitPayload {
	payload, ok := ctx.Value(initpayload).(InitPayload)
	if !ok {
		return nil
	}

	return payload
}