Class: GraphiQL::Rails::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiql/rails/config.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => ->(_) { 'application/json' },
}
CSRF_TOKEN_HEADER =
{
  "X-CSRF-Token" => -> (view_context) { view_context.form_authenticity_token }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_params: false, initial_query: nil, csrf: true, headers: DEFAULT_HEADERS) ⇒ Config

Returns a new instance of Config.



20
21
22
23
24
25
# File 'lib/graphiql/rails/config.rb', line 20

def initialize(query_params: false, initial_query: nil, csrf: true, headers: DEFAULT_HEADERS)
  @query_params = query_params
  @headers = headers.dup
  @initial_query = initial_query
  @csrf = csrf
end

Instance Attribute Details

#csrfObject

Returns the value of attribute csrf.



10
11
12
# File 'lib/graphiql/rails/config.rb', line 10

def csrf
  @csrf
end

#headersHash<String => Proc>

Returns Keys are headers to include in GraphQL requests, values are ‘->(view_context) { … }` procs to determin values.

Examples:

Adding a header to the request

config.headers["My-Header"] = -> (view_context) { "My-Value" }

Returns:

  • (Hash<String => Proc>)

    Keys are headers to include in GraphQL requests, values are ‘->(view_context) { … }` procs to determin values



8
9
10
# File 'lib/graphiql/rails/config.rb', line 8

def headers
  @headers
end

#initial_queryObject

Returns the value of attribute initial_query.



10
11
12
# File 'lib/graphiql/rails/config.rb', line 10

def initial_query
  @initial_query
end

#query_paramsObject

Returns the value of attribute query_params.



10
11
12
# File 'lib/graphiql/rails/config.rb', line 10

def query_params
  @query_params
end

Instance Method Details

#resolve_headers(view_context) ⇒ Object

Call defined procs, add CSRF token if specified



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphiql/rails/config.rb', line 28

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