Class: ForestAdminDatasourceGraphqlHasura::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_graphql_hasura/configuration.rb

Constant Summary collapse

DEFAULTS =

polymorphic_relations declares associations explicitly when the metadata API is unreachable: { 'comments' => { 'commentable' => %w[transfers cards] } }. type_values overrides the Rails class name a table maps to, for those that classify gets wrong: { 'bank_accounts' => 'Banking::Account' }.

{
  headers: {},
  metadata_uri: nil,
  included_tables: nil,
  excluded_tables: [],
  polymorphic_relations: {},
  type_values: {},
  timeout: 30
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, **options) ⇒ Configuration

Returns a new instance of Configuration.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 20

def initialize(uri:, **options)
  raise ConfigurationError, 'uri is required' if uri.nil? || uri.empty?

  unknown = options.keys - DEFAULTS.keys
  raise ConfigurationError, "Unknown option(s): #{unknown.join(", ")}" if unknown.any?

  @uri = uri
  # An explicit nil means "the default" (`headers: nil` must not crash every
  # request), and `default.dup` keeps the DEFAULTS hashes and arrays from
  # being shared — and mutated — across Configuration instances.
  DEFAULTS.each do |option, default|
    instance_variable_set("@#{option}", options[option].nil? ? default.dup : options[option])
  end
  validate_polymorphic_relations
  validate_table_lists
  # Only derivable from the conventional endpoint path: substituting on any
  # other uri would silently post metadata commands to the GraphQL endpoint.
  @metadata_uri ||= uri.include?('/v1/graphql') ? uri.sub('/v1/graphql', '/v1/metadata') : nil
end

Instance Attribute Details

#excluded_tablesObject (readonly)

Returns the value of attribute excluded_tables.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def excluded_tables
  @excluded_tables
end

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def headers
  @headers
end

#included_tablesObject (readonly)

Returns the value of attribute included_tables.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def included_tables
  @included_tables
end

#metadata_uriObject (readonly)

Returns the value of attribute metadata_uri.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def 
  @metadata_uri
end

#polymorphic_relationsObject (readonly)

Returns the value of attribute polymorphic_relations.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def polymorphic_relations
  @polymorphic_relations
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def timeout
  @timeout
end

#type_valuesObject (readonly)

Returns the value of attribute type_values.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def type_values
  @type_values
end

#uriObject (readonly)

Returns the value of attribute uri.



17
18
19
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 17

def uri
  @uri
end

Instance Method Details

#table_allowed?(*table_names) ⇒ Boolean

Accepts every spelling a table goes by (root field and type name): an exclusion must hold under renaming, or it would silently re-expose data.

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/forest_admin_datasource_graphql_hasura/configuration.rb', line 42

def table_allowed?(*table_names)
  return false if table_names.any? { |name| excluded_tables.include?(name) }
  return (table_names & included_tables).any? unless included_tables.nil?

  true
end