Class: GraphQL::Doctor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/doctor/config.rb

Constant Summary collapse

DEFAULTS =
{
  "schema" => nil,
  "require" => "./config/environment",
  "include" => ["app/graphql/**/*.rb"],
  "exclude" => [],
  "checks" => {
    "GQLD102" => {"enabled" => false},
    "GQLD103" => {"enabled" => false},
    "GQLD201" => {"severity" => "warning"},
    "GQLD305" => {"enabled" => false}
  },
  "allow_underlying_object" => [],
  "abstract_classes" => [],
  "experimental" => {"new_execution_api" => false},
  "require_suppression_reason" => false
}.freeze
CHECK_CODES =
%w[
  GQLD101 GQLD102 GQLD103 GQLD104 GQLD201 GQLD202 GQLD203 GQLD204 GQLD205
  GQLD301 GQLD302 GQLD303 GQLD305 GQLD306 GQLD307 GQLD401
].freeze
FIXED_ERROR_CODES =
%w[GQLD101 GQLD104].freeze
CONFIGURABLE_CHECK_CODES =
(CHECK_CODES - FIXED_ERROR_CODES).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Config

Returns a new instance of Config.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql/doctor/config.rb', line 33

def initialize(values = {})
  raise Error, "Configuration must be a mapping" unless values.is_a?(Hash)

  unknown = values.keys.map(&:to_s) - DEFAULTS.keys
  raise Error, "Unknown configuration key(s): #{unknown.join(', ')}" unless unknown.empty?

  normalized = values.to_h { |key, value| [key.to_s, value] }
  normalized["checks"] = normalize_checks(normalized.fetch("checks", {}))
  normalized["experimental"] = normalize_experimental(normalized.fetch("experimental", {}))
  validate_values(normalized)
  merged = DEFAULTS.merge(normalized)
  merged["checks"] = DEFAULTS["checks"].merge(normalized.fetch("checks", {}))
  @values = immutable(merged)
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



31
32
33
# File 'lib/graphql/doctor/config.rb', line 31

def values
  @values
end

Class Method Details

.load(path = ".graphql-doctor.yml", required: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/doctor/config.rb', line 48

def self.load(path = ".graphql-doctor.yml", required: false)
  unless File.file?(path)
    raise Error, "Configuration file does not exist: #{path}" if required

    return new
  end

  content = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: false) || {}
  raise Error, "Configuration must be a mapping" unless content.is_a?(Hash)

  new(content)
rescue Psych::Exception => e
  raise Error, "Invalid configuration: #{e.message}"
end

Instance Method Details

#[](key) ⇒ Object



63
64
65
# File 'lib/graphql/doctor/config.rb', line 63

def [](key)
  values.fetch(key.to_s)
end

#check_enabled?(code) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/graphql/doctor/config.rb', line 67

def check_enabled?(code)
  values["checks"].fetch(code, {}).fetch("enabled", true)
end

#severity_for(code, default) ⇒ Object



71
72
73
# File 'lib/graphql/doctor/config.rb', line 71

def severity_for(code, default)
  values["checks"].fetch(code, {}).fetch("severity", default).to_s
end

#with_only(codes) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/graphql/doctor/config.rb', line 75

def with_only(codes)
  checks = CONFIGURABLE_CHECK_CODES.to_h do |code|
    settings = values["checks"].fetch(code, {}).merge("enabled" => codes.include?(code))
    [code, settings]
  end
  self.class.new(values.merge("checks" => checks))
end