graphql-armory 0.1.0.pre12 → 0.1.0.pre13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -1
  3. data/README.md +3 -3
  4. data/docs/versioning.md +540 -0
  5. data/lib/graphql/armory/authorization/audit/findings.rb +6 -1
  6. data/lib/graphql/armory/authorization/audit/overrides.rb +8 -2
  7. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +9 -8
  8. data/lib/graphql/armory/authorization/definitions.rb +8 -92
  9. data/lib/graphql/armory/authorization.rb +3 -1
  10. data/lib/graphql/armory/definitions.rb +111 -0
  11. data/lib/graphql/armory/location.rb +101 -0
  12. data/lib/graphql/armory/version.rb +1 -1
  13. data/lib/graphql/armory/versioning/audit/command.rb +43 -0
  14. data/lib/graphql/armory/versioning/audit/own_checks.rb +139 -0
  15. data/lib/graphql/armory/versioning/audit/profiles.rb +123 -0
  16. data/lib/graphql/armory/versioning/audit/subscription_triggers.rb +98 -0
  17. data/lib/graphql/armory/versioning/audit.rb +559 -0
  18. data/lib/graphql/armory/versioning/controls.rb +403 -0
  19. data/lib/graphql/armory/versioning/decision.rb +89 -0
  20. data/lib/graphql/armory/versioning/deprecation.rb +95 -0
  21. data/lib/graphql/armory/versioning/diff/command.rb +49 -0
  22. data/lib/graphql/armory/versioning/diff.rb +349 -0
  23. data/lib/graphql/armory/versioning/dump/command.rb +46 -0
  24. data/lib/graphql/armory/versioning/dump.rb +107 -0
  25. data/lib/graphql/armory/versioning/gate.rb +140 -0
  26. data/lib/graphql/armory/versioning/integration.rb +903 -0
  27. data/lib/graphql/armory/versioning/lifecycle.rb +61 -0
  28. data/lib/graphql/armory/versioning/profiles.rb +122 -0
  29. data/lib/graphql/armory/versioning/rspec.rb +38 -0
  30. data/lib/graphql/armory/versioning/versions.rb +111 -0
  31. data/lib/graphql/armory/versioning/view.rb +61 -0
  32. data/lib/graphql/armory/versioning.rb +132 -0
  33. data/lib/graphql/armory.rb +1 -0
  34. data/lib/tasks/graphql_armory.rake +20 -0
  35. metadata +24 -2
  36. data/lib/graphql/armory/authorization/location.rb +0 -103
@@ -27,7 +27,7 @@ module GraphQL
27
27
  end
28
28
  return if payloads.empty?
29
29
 
30
- named = definitions.to_h { |type| [type.graphql_name, type] }
30
+ named = definitions.group_by(&:graphql_name)
31
31
  fields.each { |type, field| note(type, field, objects, payloads, named) }
32
32
  end
33
33
 
@@ -61,21 +61,22 @@ module GraphQL
61
61
  end
62
62
 
63
63
  def note(type, field, objects, payloads, named)
64
- target = returned_type(field, named) or return
65
- reused = members(target, objects) & payloads
66
- return if reused.empty? || own_payload?(field, target)
64
+ targets = returned_types(field, named)
65
+ reused = targets.flat_map { |target| members(target, objects) }.uniq & payloads
66
+ return if reused.empty? || (targets.one? && own_payload?(field, targets.first))
67
67
 
68
68
  @findings.problem(:payload_outside_mutation, "#{type.graphql_name}.#{field.original_name}",
69
69
  "returns #{reused.map(&:graphql_name).sort.join(", ")}, a mutation's payload, whose fields are covered by that mutation's rule: " \
70
70
  "here no such rule runs, so they are served with no check. Return a type of its own, or, for a public child of a mutation with a rule, give the parent's field `authorize: :none, reason: \"...\"`", at: field)
71
71
  end
72
72
 
73
- # A shadowed definition may name a type that never resolved; it cannot serve anything.
74
- def returned_type(field, named)
73
+ # A shadowed definition may name a type that never resolved; it cannot serve anything. A late-bound name with
74
+ # several definitions (Versioning's type twins) may mean any of them.
75
+ def returned_types(field, named)
75
76
  type = field.type.unwrap
76
- type.is_a?(GraphQL::Schema::LateBoundType) ? named[type.graphql_name] : type
77
+ type.is_a?(GraphQL::Schema::LateBoundType) ? named.fetch(type.graphql_name, []) : [type]
77
78
  rescue NameError, GraphQL::Error
78
- nil
79
+ []
79
80
  end
80
81
 
81
82
  def members(target, objects)
@@ -1,24 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "set"
3
+ require "graphql/armory/definitions"
4
4
 
5
5
  module GraphQL
6
6
  module Armory
7
7
  module Authorization
8
- # Every definition a schema holds, hidden or not. `schema.types` is read with no request, so it leaves out what is
9
- # hidden there and a request can still reach: a type or field hidden by `visible?`, or one of several definitions of
10
- # a name. This follows the definitions themselves from the roots and `orphan_types`.
8
+ # Every definition a schema holds, hidden or not (`Armory::Definitions`), plus what `schema.types` lists, which the
9
+ # audit must not miss either.
11
10
  module Definitions
12
11
  def self.call(schema, &on_listing_error)
13
- found = Set.new.compare_by_identity
14
- pending = [schema.query, schema.mutation, schema.subscription, *schema.orphan_types, *directive_types(schema), *introspection_field_types(schema), *listed_types(schema, &on_listing_error)]
15
- until pending.empty?
16
- type = pending.pop
17
- next if !type.respond_to?(:kind) || !found.add?(type)
18
-
19
- pending.concat(referenced_types(type))
20
- end
21
- found.to_a
12
+ Armory::Definitions.(schema, listed_types(schema, &on_listing_error))
22
13
  end
23
14
 
24
15
  # With only static profiles, graphql-ruby refuses to list types without naming one: the roots still lead everywhere.
@@ -36,85 +27,10 @@ module GraphQL
36
27
  end
37
28
  private_class_method :listed_types
38
29
 
39
- # A directive argument is client input like any other, and only `schema.types` used to reach its input objects.
40
- def self.directive_types(schema)
41
- schema.directives.values.flat_map { |directive| arguments_of(directive) }.compact
42
- end
43
- private_class_method :directive_types
44
-
45
- # `introspection(true)` is a flag anyone can set, and graphql-ruby serves such a type like any other. The `__` prefix
46
- # is reserved for introspection by the GraphQL spec: built-in types, what `Schema.introspection` replaces them with,
47
- # and their copies all carry it.
48
- def self.introspection?(type)
49
- type.introspection? && type.graphql_name.start_with?("__")
50
- end
51
-
52
- # `Schema.introspection` can add entry points and dynamic fields, which no type holds and a client can reach.
53
- def self.introspection_field_types(schema)
54
- system = schema.introspection_system
55
- (system.entry_points + system.dynamic_fields).flat_map { |field| references_of(field) }.flatten.compact
56
- end
57
- private_class_method :introspection_field_types
58
-
59
- def self.connection?(type)
60
- type.is_a?(Class) && type.include?(GraphQL::Types::Relay::ConnectionBehaviors)
61
- end
62
-
63
- def self.referenced_types(type)
64
- references = []
65
- references << fields_of(type)
66
- references << arguments_of(type) if type.kind.input_object?
67
- references.concat(type.interface_type_memberships.map(&:abstract_type)) if type.respond_to?(:interface_type_memberships)
68
- references.concat(type.orphan_types) if type.kind.interface?
69
- references.concat(included_interfaces(type)) if type.kind.interface?
70
- references.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
71
- references << type.node_type if connection?(type)
72
- references.flatten.compact
73
- end
74
- private_class_method :referenced_types
75
-
76
- # An interface brought in with `include` makes no type membership, and its fields are served all the same.
77
- def self.included_interfaces(type)
78
- type.ancestors.select { |ancestor| !ancestor.equal?(type) && ancestor.respond_to?(:kind) && ancestor.kind.interface? }
79
- end
80
- private_class_method :included_interfaces
81
-
82
- # Every definition of every field, nearest first: a hidden one falls back to the next of its name, and
83
- # `all_field_definitions` merges them by name. `field ... do ... end` blocks are loaded here, because only
84
- # `schema.types` or a request loads them, and the audit reads arguments from them.
85
- def self.fields_at_every_level(type)
86
- return [] unless type.respond_to?(:all_field_definitions)
87
-
88
- fields = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) }.flat_map { |ancestor| ancestor.own_fields.values.flatten }
89
- fields.each { |field| field.ensure_loaded if field.respond_to?(:ensure_loaded) }
90
- fields.uniq
91
- end
92
-
93
- # A shadowed definition is only served by a fallback, so one whose type cannot load fails nothing that runs today.
94
- def self.shadowed?(type, field)
95
- !type.all_field_definitions.include?(field)
96
- end
97
-
98
- def self.fields_of(type)
99
- fields_at_every_level(type).map do |field|
100
- references_of(field)
101
- rescue GraphQL::Error
102
- raise unless shadowed?(type, field)
103
-
104
- nil
105
- end
106
- end
107
- private_class_method :fields_of
108
-
109
- def self.references_of(field)
110
- [field.type.unwrap, arguments_of(field)]
111
- end
112
- private_class_method :references_of
113
-
114
- def self.arguments_of(member)
115
- member.all_argument_definitions.flat_map { |argument| [argument.type.unwrap, argument.loads] }
116
- end
117
- private_class_method :arguments_of
30
+ def self.introspection?(type) = Armory::Definitions.introspection?(type)
31
+ def self.connection?(type) = Armory::Definitions.connection?(type)
32
+ def self.fields_at_every_level(type) = Armory::Definitions.fields_at_every_level(type)
33
+ def self.shadowed?(type, field) = Armory::Definitions.shadowed?(type, field)
118
34
  end
119
35
  end
120
36
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "graphql/armory/authorization/errors"
4
- require "graphql/armory/authorization/location"
4
+ require "graphql/armory/location"
5
5
  require "graphql/armory/authorization/configuration"
6
6
  require "graphql/armory/authorization/rule"
7
7
  require "graphql/armory/authorization/memo"
@@ -30,6 +30,8 @@ module GraphQL
30
30
  module Authorization
31
31
  # Authorization's telemetry is the gem's: one Telemetry for every module.
32
32
  Telemetry = Armory::Telemetry
33
+ # So is Location, which Versioning's audit uses too.
34
+ Location = Armory::Location
33
35
  end
34
36
 
35
37
  # Deny-by-default authorization. The core (Authorize, Scope) knows nothing about
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ # Every definition a schema holds, hidden or not, read with no request. graphql-ruby's lookups with no request leave
8
+ # out what is hidden there and a request can still reach: a type or field hidden by `visible?`, or one of several
9
+ # definitions of a name. This follows the definitions themselves from the roots, `orphan_types` and `extra`.
10
+ module Definitions
11
+ def self.call(schema, extra = [])
12
+ found = Set.new.compare_by_identity
13
+ pending = [schema.query, schema.mutation, schema.subscription, *schema.orphan_types, *directive_types(schema), *introspection_field_types(schema), *extra]
14
+ until pending.empty?
15
+ type = pending.pop
16
+ next if !type.respond_to?(:kind) || !found.add?(type)
17
+
18
+ pending.concat(referenced_types(type))
19
+ end
20
+ found.to_a
21
+ end
22
+
23
+ # A directive argument is client input like any other, and only `schema.types` used to reach its input objects.
24
+ def self.directive_types(schema)
25
+ schema.directives.values.flat_map { |directive| arguments_of(directive) }.compact
26
+ end
27
+ private_class_method :directive_types
28
+
29
+ # `introspection(true)` is a flag anyone can set, and graphql-ruby serves such a type like any other. The `__` prefix
30
+ # is reserved for introspection by the GraphQL spec: built-in types, what `Schema.introspection` replaces them with,
31
+ # and their copies all carry it.
32
+ def self.introspection?(type)
33
+ type.introspection? && type.graphql_name.start_with?("__")
34
+ end
35
+
36
+ # `Schema.introspection` can add entry points and dynamic fields, which no type holds and a client can reach.
37
+ def self.introspection_field_types(schema)
38
+ system = schema.introspection_system
39
+ (system.entry_points + system.dynamic_fields).flat_map { |field| references_of(field) }.flatten.compact
40
+ end
41
+ private_class_method :introspection_field_types
42
+
43
+ def self.connection?(type)
44
+ type.is_a?(Class) && type.include?(GraphQL::Types::Relay::ConnectionBehaviors)
45
+ end
46
+
47
+ def self.referenced_types(type)
48
+ references = []
49
+ references << fields_of(type)
50
+ references << arguments_of(type) if type.kind.input_object?
51
+ references.concat(interfaces_of(type)) if type.respond_to?(:interface_type_memberships)
52
+ references.concat(type.orphan_types) if type.kind.interface?
53
+ references.concat(included_interfaces(type)) if type.kind.interface?
54
+ references.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
55
+ references << type.node_type if connection?(type)
56
+ references.flatten.compact
57
+ end
58
+ private_class_method :referenced_types
59
+
60
+ # A String or `LateBoundType` that graphql-ruby never matched to an interface stays a bare membership: it has no
61
+ # type to follow.
62
+ def self.interfaces_of(type)
63
+ type.interface_type_memberships.filter_map { |membership| membership.abstract_type if membership.respond_to?(:abstract_type) }
64
+ end
65
+ private_class_method :interfaces_of
66
+
67
+ # An interface brought in with `include` makes no type membership, and its fields are served all the same.
68
+ def self.included_interfaces(type)
69
+ type.ancestors.select { |ancestor| !ancestor.equal?(type) && ancestor.respond_to?(:kind) && ancestor.kind.interface? }
70
+ end
71
+ private_class_method :included_interfaces
72
+
73
+ # Every definition of every field, nearest first: a hidden one falls back to the next of its name, and
74
+ # `all_field_definitions` merges them by name. `field ... do ... end` blocks are loaded here, because only
75
+ # `schema.types` or a request loads them, and the audit reads arguments from them.
76
+ def self.fields_at_every_level(type)
77
+ return [] unless type.respond_to?(:all_field_definitions)
78
+
79
+ fields = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) }.flat_map { |ancestor| ancestor.own_fields.values.flatten }
80
+ fields.each { |field| field.ensure_loaded if field.respond_to?(:ensure_loaded) }
81
+ fields.uniq
82
+ end
83
+
84
+ # A shadowed definition is only served by a fallback, so one whose type cannot load fails nothing that runs today.
85
+ def self.shadowed?(type, field)
86
+ !type.all_field_definitions.include?(field)
87
+ end
88
+
89
+ def self.fields_of(type)
90
+ fields_at_every_level(type).map do |field|
91
+ references_of(field)
92
+ rescue GraphQL::Error
93
+ raise unless shadowed?(type, field)
94
+
95
+ nil
96
+ end
97
+ end
98
+ private_class_method :fields_of
99
+
100
+ def self.references_of(field)
101
+ [field.type.unwrap, arguments_of(field)]
102
+ end
103
+ private_class_method :references_of
104
+
105
+ def self.arguments_of(member)
106
+ member.all_argument_definitions.flat_map { |argument| [argument.type.unwrap, argument.loads] }
107
+ end
108
+ private_class_method :arguments_of
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ # Where something of the app's schema was written, as "path:line", for the audit's report. graphql-ruby keeps no
6
+ # location for a field or an argument, so the plugin's field and argument classes record the line that declared them,
7
+ # and a type records the line of its `authorize`. Anything else falls back to the line of its class, and a type
8
+ # graphql-ruby generated (a mutation's payload, a connection, an edge) to the class it was generated from.
9
+ #
10
+ # Limits: a schema built from SDL has no Ruby lines; a field declared by a helper method points to the helper; a class
11
+ # reopened in several files gives the first one.
12
+ module Location
13
+ GRAPHQL_RUBY_PATH = "#{File.dirname(GraphQL::Schema.method(:from_definition).source_location.first)}/"
14
+ ARMORY_PATH = "#{__dir__}/"
15
+ # The directory the app started in, read once: a later `Dir.chdir` must not change the report's text. None when the
16
+ # directory is gone, and then paths stay absolute.
17
+ ROOT = begin
18
+ "#{Dir.pwd}/"
19
+ rescue SystemCallError
20
+ nil
21
+ end
22
+
23
+ # The first line of the caller's stack outside graphql-ruby and this gem: where the app declared it. An `initialize`
24
+ # is skipped too: an app's field or argument class, or another gem's module, may define one that calls super into
25
+ # the plugin's, and its line is the same for every field.
26
+ def self.declared
27
+ found = caller_locations(2, 40)&.find do |location|
28
+ path = location.absolute_path || location.path
29
+ !path.start_with?("<", GRAPHQL_RUBY_PATH, ARMORY_PATH) && location.base_label != "initialize"
30
+ end
31
+ found && "#{found.absolute_path || found.path}:#{found.lineno}"
32
+ end
33
+
34
+ # "path:line" for a method, a field, an argument, a type or a class, relative to the directory the app started in
35
+ # when it is inside it, or nil when there is no line to give.
36
+ def self.of(thing)
37
+ relative(absolute(thing))
38
+ end
39
+
40
+ def self.absolute(thing)
41
+ case thing
42
+ when nil then nil
43
+ when String then thing
44
+ when Method, UnboundMethod then join(thing.source_location)
45
+ when GraphQL::Schema::Field, GraphQL::Schema::Argument
46
+ (thing.armory_location if thing.respond_to?(:armory_location)) || absolute(thing.owner)
47
+ when Module
48
+ (thing.armory_rule_location if thing.respond_to?(:armory_rule_location)) || constant(thing) || generated_from(thing)
49
+ end
50
+ end
51
+ private_class_method :absolute
52
+
53
+ # graphql-ruby generates some types as classes with no constant: a mutation's payload, and a type's connection and
54
+ # edge. They point to the class they were generated from, which was written somewhere.
55
+ # Only an object type is followed: a schema also answers `mutation`, with its root type. A connection of connections
56
+ # can lead back to itself, so each class is visited once, and the walk stops after a few steps.
57
+ def self.generated_from(type)
58
+ seen = {}.compare_by_identity
59
+ while type.is_a?(Class) && type < GraphQL::Schema::Object && !seen.key?(type) && seen.size < 16
60
+ seen[type] = true
61
+ source = type.mutation if type.respond_to?(:mutation)
62
+ source = nil unless source.is_a?(Class) && source < GraphQL::Schema::Resolver
63
+ source ||= type.node_type if type.respond_to?(:node_type)
64
+ return unless source.is_a?(Module)
65
+
66
+ found = (source.armory_rule_location if source.respond_to?(:armory_rule_location)) || constant(source)
67
+ return found if found
68
+
69
+ type = source
70
+ end
71
+ rescue StandardError, NotImplementedError
72
+ nil
73
+ end
74
+ private_class_method :generated_from
75
+
76
+ # Only when the name leads back to this class: a class may answer another constant's name. A location is never worth
77
+ # failing the audit, so a `name` that raises gives none.
78
+ def self.constant(mod)
79
+ name = mod.name
80
+ return if !name.is_a?(String) || name.empty? || !Object.const_get(name).equal?(mod)
81
+
82
+ join(Object.const_source_location(name))
83
+ rescue StandardError, NotImplementedError
84
+ nil
85
+ end
86
+ private_class_method :constant
87
+
88
+ def self.join(pair)
89
+ pair && !pair.empty? && pair.first ? "#{pair.first}:#{pair.last}" : nil
90
+ end
91
+ private_class_method :join
92
+
93
+ def self.relative(location)
94
+ return location if location.nil? || ROOT.nil?
95
+
96
+ location.start_with?(ROOT) ? location.delete_prefix(ROOT) : location
97
+ end
98
+ private_class_method :relative
99
+ end
100
+ end
101
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Armory
5
- VERSION = "0.1.0.pre12"
5
+ VERSION = "0.1.0.pre13"
6
6
  end
7
7
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ class Audit
7
+ # What `rake "graphql_armory:versions:audit[YourSchema]"` runs: prints the report and answers an exit status.
8
+ class Command
9
+ # The rake task's action. The app's Rake file may name the task first, to add a prerequisite, so the task existing does
10
+ # not mean this ran: the file looks for this object among its actions.
11
+ RAKE_ACTION = proc do |_task, args|
12
+ Rake::Task[:environment].invoke if Rake::Task.task_defined?(:environment)
13
+ status = new.(args[:schema])
14
+ exit(status) unless status.zero?
15
+ end
16
+
17
+ def initialize(out: $stdout)
18
+ @out = out
19
+ end
20
+
21
+ def call(schema_name)
22
+ return fail_with("Name the schema: rake \"graphql_armory:versions:audit[YourSchema]\"") if schema_name.to_s.empty?
23
+
24
+ report = Audit.(Object.const_get(schema_name))
25
+ @out.print(report)
26
+ report.ok? ? 0 : 1
27
+ rescue NameError => error
28
+ raise unless schema_name.to_s.split("::").include?(error.name.to_s)
29
+
30
+ fail_with("No schema named #{schema_name}")
31
+ end
32
+
33
+ private
34
+
35
+ def fail_with(message)
36
+ @out.puts(message)
37
+ 1
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Versioning
6
+ class Audit
7
+ # An app's `visible?` that answers before the plugin's decides the version for it: if it does not call `super`, it
8
+ # serves a member outside its lifecycle. The audit cannot read whether it does, so each one is reported once, where it
9
+ # is defined, until `own_checks:` declares it with a reason. Methods of graphql-ruby and of this gem are not the app's,
10
+ # and a method behind the plugin's runs only after the version allowed the member.
11
+ #
12
+ # Read along the whole chain, as Ruby calls it (`method(:visible?)` and its `super_method`): on type classes,
13
+ # resolvers and mutations with `Base`, on field, argument, enum value and membership classes with `Base`, on one
14
+ # member (a `def self.visible?` in a field's block, which no class holds, so it cannot be declared), and on the
15
+ # schema, whose `self.visible?` graphql-ruby asks for every member.
16
+ class OwnChecks
17
+ INTEGRATIONS = [Integration::Type, Integration::Field, Integration::Argument, Integration::EnumValue, Integration::Membership].freeze
18
+ LIBRARIES = [Armory::Location::GRAPHQL_RUBY_PATH, Armory::Location::ARMORY_PATH].freeze
19
+
20
+ def self.call(schema, definitions, members, findings)
21
+ new(definitions, members, findings).(schema, schema.armory_versioning.own_checks)
22
+ end
23
+
24
+ # The class or module `own_checks:` names for a `visible?` method: a class method's holder is the class, and an
25
+ # interface's `definition_methods` belong to the interface.
26
+ def self.holder_of(owner, method)
27
+ held = method.owner
28
+ owner.ancestors.find do |ancestor|
29
+ ancestor.singleton_class.equal?(held) ||
30
+ (!ancestor.is_a?(Class) && ancestor.const_defined?(:DefinitionMethods, false) && ancestor.const_get(:DefinitionMethods, false).equal?(held))
31
+ end || held
32
+ end
33
+
34
+ def initialize(definitions = [], members = [], findings = Findings.new)
35
+ @definitions = definitions
36
+ @members = members
37
+ @findings = findings
38
+ @found = {}.compare_by_identity
39
+ @singletons = []
40
+ end
41
+
42
+ def call(schema, declared)
43
+ types = @definitions + @members.grep(GraphQL::Schema::Field).filter_map(&:resolver)
44
+ types.uniq.each { |type| note_type(type) }
45
+ @members.each { |member| note_member(member) unless member.is_a?(Module) }
46
+ note_schema(schema)
47
+ report(declared)
48
+ @findings
49
+ end
50
+
51
+ private
52
+
53
+ def note_type(type)
54
+ return unless type.is_a?(Module) && type.singleton_class.include?(Integration::Type)
55
+
56
+ ahead(type.method(:visible?)) { |method| note(holder_of(type, method), method, ".") }
57
+ end
58
+
59
+ def note_member(member)
60
+ return unless INTEGRATIONS.any? { |integration| member.is_a?(integration) }
61
+
62
+ ahead(member.method(:visible?)) do |method|
63
+ if member.class.ancestors.include?(method.owner)
64
+ note(method.owner, method, "#")
65
+ else
66
+ @singletons << [member, method]
67
+ end
68
+ end
69
+ end
70
+
71
+ # Every method of the chain: the schema has no plugin method to stop at.
72
+ def note_schema(schema)
73
+ method = schema.method(:visible?)
74
+ while method
75
+ note(holder_of(schema, method), method, ".") unless library?(method)
76
+ method = method.super_method
77
+ end
78
+ end
79
+
80
+ # The app's methods Ruby calls before the plugin's.
81
+ def ahead(method)
82
+ while method && !INTEGRATIONS.include?(method.owner)
83
+ yield method unless library?(method)
84
+ method = method.super_method
85
+ end
86
+ end
87
+
88
+ def library?(method)
89
+ file = method.source_location&.first
90
+ !file.nil? && file.start_with?(*LIBRARIES)
91
+ end
92
+
93
+ def note(holder, method, separator)
94
+ @found[holder] ||= ["#{name_of(holder)}#{separator}visible?", method]
95
+ end
96
+
97
+ def holder_of(owner, method) = self.class.holder_of(owner, method)
98
+
99
+ def name_of(holder) = self.class.name_of(holder)
100
+
101
+ # How a report names a holder: a type by its GraphQL name, a schema or another class by its constant.
102
+ def self.name_of(holder)
103
+ return holder.name || "Schema" if holder.is_a?(Class) && holder <= GraphQL::Schema
104
+ return Integration.display_name(holder) if holder.respond_to?(:graphql_name)
105
+
106
+ holder.name || holder.inspect
107
+ end
108
+
109
+ def report(declared)
110
+ @found.each do |holder, (path, method)|
111
+ reason = declared.fetch(holder, {})[:visible?]
112
+ next @findings.information(:own_check, path, "declared in own_checks: #{reason}", at: method) if reason
113
+
114
+ @findings.problem(:own_check, path, "#{risk(holder)}. Remove it, or declare it in own_checks: with a reason", at: method)
115
+ end
116
+ @singletons.uniq { |member, _| member.object_id }.each do |member, method|
117
+ @findings.problem(:own_check, member.path, "has a visible? of its own (a def self.visible? on this one member): it answers " \
118
+ "before the plugin's, and own_checks: cannot declare it. Move it to a class, or remove it", at: method)
119
+ end
120
+ declared.each_key do |holder|
121
+ next if @found.key?(holder)
122
+
123
+ @findings.problem(:own_check, "#{name_of(holder)} visible?", "named in own_checks: but defines no visible? ahead of the plugin's: " \
124
+ "remove it from the option", at: holder)
125
+ end
126
+ end
127
+
128
+ def risk(holder)
129
+ if holder.is_a?(Class) && holder <= GraphQL::Schema
130
+ "defined by the app on the schema: graphql-ruby asks it about every member, so if it does not call super nothing is versioned"
131
+ else
132
+ "defined by the app, it answers before the plugin's: if it does not call super, it serves members outside their lifecycle"
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end