-
Notifications
You must be signed in to change notification settings - Fork 134
/
config.rb
44 lines (37 loc) · 1.45 KB
/
config.rb
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
module GraphiQL
module Rails
class Config
# @example Adding a header to the request
# config.headers["My-Header"] = -> (view_context) { "My-Value" }
#
# @return [Hash<String => Proc>] Keys are headers to include in GraphQL requests, values are `->(view_context) { ... }` procs to determin values
attr_accessor :headers
attr_accessor :query_params, :initial_query, :csrf, :title, :logo, :header_editor_enabled, :input_value_deprecation
DEFAULT_HEADERS = {
'Content-Type' => ->(_) { 'application/json' },
}
CSRF_TOKEN_HEADER = {
"X-CSRF-Token" => -> (view_context) { view_context.form_authenticity_token }
}
def initialize(query_params: false, initial_query: nil, title: nil, logo: nil, csrf: true, headers: DEFAULT_HEADERS, input_value_deprecation: false)
@query_params = query_params
@headers = headers.dup
@initial_query = initial_query
@title = title
@logo = logo
@csrf = csrf
@input_value_deprecation = input_value_deprecation
end
# Call defined procs, add CSRF token if specified
def resolve_headers(view_context)
all_headers = DEFAULT_HEADERS.merge(headers)
if csrf
all_headers = all_headers.merge(CSRF_TOKEN_HEADER)
end
all_headers.each_with_object({}) do |(key, value), memo|
memo[key] = value.call(view_context)
end
end
end
end
end