class GraphiQL::Rails::Config

Constants

CSRF_TOKEN_HEADER
DEFAULT_HEADERS

Attributes

csrf[RW]
headers[RW]

@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

initial_query[RW]
query_params[RW]
title[RW]

Public Class Methods

new(query_params: false, initial_query: nil, title: nil, logo: nil, csrf: true, headers: DEFAULT_HEADERS) click to toggle source
# File lib/graphiql/rails/config.rb, line 20
def initialize(query_params: false, initial_query: nil, title: nil, logo: nil, csrf: true, headers: DEFAULT_HEADERS)
  @query_params = query_params
  @headers = headers.dup
  @initial_query = initial_query
  @title = title
  @logo = logo
  @csrf = csrf
end

Public Instance Methods

resolve_headers(view_context) click to toggle source

Call defined procs, add CSRF token if specified

# File lib/graphiql/rails/config.rb, line 30
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