graphql-armory 0.1.0.pre13 → 0.1.0.pre15
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +5 -5
- data/docs/authorization.md +4 -0
- data/docs/encrypted-ids.md +24 -2
- data/docs/persisted-queries.md +4 -0
- data/docs/rate-limiting.md +23 -0
- data/docs/response-caching.md +590 -0
- data/docs/subscriptions.md +261 -0
- data/docs/telemetry.md +39 -1
- data/docs/versioning.md +5 -0
- data/lib/graphql/armory/authorization/authorize.rb +3 -0
- data/lib/graphql/armory/authorization/scope.rb +2 -0
- data/lib/graphql/armory/caching/audit/command.rb +51 -0
- data/lib/graphql/armory/caching/audit/context_class.rb +117 -0
- data/lib/graphql/armory/caching/audit/declarations.rb +166 -0
- data/lib/graphql/armory/caching/audit/own_checks.rb +150 -0
- data/lib/graphql/armory/caching/audit/public_rules.rb +158 -0
- data/lib/graphql/armory/caching/audit/report.rb +113 -0
- data/lib/graphql/armory/caching/audit.rb +136 -0
- data/lib/graphql/armory/caching/base.rb +118 -0
- data/lib/graphql/armory/caching/canonical.rb +138 -0
- data/lib/graphql/armory/caching/charge.rb +86 -0
- data/lib/graphql/armory/caching/codec.rb +108 -0
- data/lib/graphql/armory/caching/context_methods.rb +88 -0
- data/lib/graphql/armory/caching/controls/app_schema.rb +70 -0
- data/lib/graphql/armory/caching/controls/context.rb +30 -0
- data/lib/graphql/armory/caching/controls/operations.rb +37 -0
- data/lib/graphql/armory/caching/controls/schema.rb +123 -0
- data/lib/graphql/armory/caching/controls/store.rb +16 -0
- data/lib/graphql/armory/caching/controls/walk_schema.rb +197 -0
- data/lib/graphql/armory/caching/controls.rb +19 -0
- data/lib/graphql/armory/caching/digester.rb +48 -0
- data/lib/graphql/armory/caching/errors.rb +16 -0
- data/lib/graphql/armory/caching/gate/admission.rb +67 -0
- data/lib/graphql/armory/caching/gate/signals.rb +65 -0
- data/lib/graphql/armory/caching/gate/store_check.rb +60 -0
- data/lib/graphql/armory/caching/gate/substitute.rb +61 -0
- data/lib/graphql/armory/caching/gate.rb +384 -0
- data/lib/graphql/armory/caching/integration/guard.rb +55 -0
- data/lib/graphql/armory/caching/integration/install.rb +262 -0
- data/lib/graphql/armory/caching/integration/query.rb +42 -0
- data/lib/graphql/armory/caching/integration.rb +141 -0
- data/lib/graphql/armory/caching/keyer.rb +208 -0
- data/lib/graphql/armory/caching/memory_store.rb +164 -0
- data/lib/graphql/armory/caching/nulls.rb +121 -0
- data/lib/graphql/armory/caching/purge.rb +98 -0
- data/lib/graphql/armory/caching/record_keys.rb +109 -0
- data/lib/graphql/armory/caching/recorder/context.rb +178 -0
- data/lib/graphql/armory/caching/recorder/log.rb +142 -0
- data/lib/graphql/armory/caching/recorder/null.rb +24 -0
- data/lib/graphql/armory/caching/recorder/shared_writes.rb +58 -0
- data/lib/graphql/armory/caching/recorder/substitute.rb +119 -0
- data/lib/graphql/armory/caching/recorder.rb +34 -0
- data/lib/graphql/armory/caching/redis_store/scripts.rb +159 -0
- data/lib/graphql/armory/caching/redis_store.rb +162 -0
- data/lib/graphql/armory/caching/rspec.rb +25 -0
- data/lib/graphql/armory/caching/schema_identity.rb +102 -0
- data/lib/graphql/armory/caching/store/null.rb +31 -0
- data/lib/graphql/armory/caching/store/substitute.rb +144 -0
- data/lib/graphql/armory/caching/store.rb +34 -0
- data/lib/graphql/armory/caching/tags.rb +39 -0
- data/lib/graphql/armory/caching/values/decision.rb +45 -0
- data/lib/graphql/armory/caching/values/declaration.rb +114 -0
- data/lib/graphql/armory/caching/values/entry.rb +108 -0
- data/lib/graphql/armory/caching/values/key.rb +39 -0
- data/lib/graphql/armory/caching/values/lookup.rb +35 -0
- data/lib/graphql/armory/caching/values/policy.rb +44 -0
- data/lib/graphql/armory/caching/viewer.rb +112 -0
- data/lib/graphql/armory/caching/walk/directives.rb +45 -0
- data/lib/graphql/armory/caching/walk/relay.rb +54 -0
- data/lib/graphql/armory/caching/walk/run.rb +130 -0
- data/lib/graphql/armory/caching/walk/tally.rb +38 -0
- data/lib/graphql/armory/caching/walk.rb +46 -0
- data/lib/graphql/armory/caching.rb +69 -0
- data/lib/graphql/armory/encoder/legacy.rb +14 -0
- data/lib/graphql/armory/encoder/null.rb +14 -1
- data/lib/graphql/armory/encoder/substitute.rb +54 -2
- data/lib/graphql/armory/encoder/versioned.rb +36 -0
- data/lib/graphql/armory/encoder.rb +80 -1
- data/lib/graphql/armory/persisted_queries.rb +8 -1
- data/lib/graphql/armory/rate_limits/cached_charge.rb +43 -0
- data/lib/graphql/armory/rate_limits/concurrency.rb +12 -5
- data/lib/graphql/armory/rate_limits/invalid_queries.rb +3 -2
- data/lib/graphql/armory/rate_limits/points.rb +21 -9
- data/lib/graphql/armory/rate_limits/time.rb +15 -8
- data/lib/graphql/armory/rate_limits.rb +9 -0
- data/lib/graphql/armory/subscriptions/ably/key.rb +49 -0
- data/lib/graphql/armory/subscriptions/ably/publisher.rb +132 -0
- data/lib/graphql/armory/subscriptions/ably/token_request.rb +42 -0
- data/lib/graphql/armory/subscriptions/ably/webhook.rb +96 -0
- data/lib/graphql/armory/subscriptions/ably.rb +126 -0
- data/lib/graphql/armory/subscriptions/audit.rb +92 -0
- data/lib/graphql/armory/subscriptions/controls/dependencies.rb +37 -0
- data/lib/graphql/armory/subscriptions/controls/event_log.rb +30 -0
- data/lib/graphql/armory/subscriptions/controls/id.rb +26 -0
- data/lib/graphql/armory/subscriptions/controls/record.rb +34 -0
- data/lib/graphql/armory/subscriptions/controls/schema.rb +187 -0
- data/lib/graphql/armory/subscriptions/controls/viewer.rb +99 -0
- data/lib/graphql/armory/subscriptions/controls.rb +19 -0
- data/lib/graphql/armory/subscriptions/core/channels.rb +93 -0
- data/lib/graphql/armory/subscriptions/core/clock.rb +40 -0
- data/lib/graphql/armory/subscriptions/core/controls.rb +29 -0
- data/lib/graphql/armory/subscriptions/core/delivery.rb +250 -0
- data/lib/graphql/armory/subscriptions/core/settings.rb +182 -0
- data/lib/graphql/armory/subscriptions/core/trigger.rb +63 -0
- data/lib/graphql/armory/subscriptions/core/viewer.rb +153 -0
- data/lib/graphql/armory/subscriptions/core/write.rb +172 -0
- data/lib/graphql/armory/subscriptions/core.rb +200 -0
- data/lib/graphql/armory/subscriptions/errors.rb +30 -0
- data/lib/graphql/armory/subscriptions/http/null.rb +17 -0
- data/lib/graphql/armory/subscriptions/http/substitute.rb +56 -0
- data/lib/graphql/armory/subscriptions/http.rb +93 -0
- data/lib/graphql/armory/subscriptions/memory_store.rb +115 -0
- data/lib/graphql/armory/subscriptions/pusher/signature.rb +45 -0
- data/lib/graphql/armory/subscriptions/pusher.rb +192 -0
- data/lib/graphql/armory/subscriptions/redis_store/scripts.rb +115 -0
- data/lib/graphql/armory/subscriptions/redis_store.rb +157 -0
- data/lib/graphql/armory/subscriptions/store/null.rb +23 -0
- data/lib/graphql/armory/subscriptions/store/substitute.rb +155 -0
- data/lib/graphql/armory/subscriptions/store.rb +38 -0
- data/lib/graphql/armory/subscriptions/topic.rb +253 -0
- data/lib/graphql/armory/subscriptions/transport/null.rb +28 -0
- data/lib/graphql/armory/subscriptions/transport/substitute.rb +150 -0
- data/lib/graphql/armory/subscriptions/transport.rb +41 -0
- data/lib/graphql/armory/subscriptions/values/capabilities.rb +50 -0
- data/lib/graphql/armory/subscriptions/values/message.rb +31 -0
- data/lib/graphql/armory/subscriptions/values/outcome.rb +42 -0
- data/lib/graphql/armory/subscriptions/values/record.rb +176 -0
- data/lib/graphql/armory/subscriptions/webhook_app.rb +31 -0
- data/lib/graphql/armory/subscriptions.rb +33 -0
- data/lib/graphql/armory/telemetry.rb +8 -4
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory/versioning/audit.rb +1 -9
- data/lib/graphql/armory/versioning/integration.rb +60 -2
- data/lib/graphql/armory.rb +2 -0
- data/lib/tasks/graphql_armory.rake +9 -0
- metadata +110 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "graphql/armory/subscriptions/ably/key"
|
|
5
|
+
require "graphql/armory/subscriptions/ably/publisher"
|
|
6
|
+
require "graphql/armory/subscriptions/ably/token_request"
|
|
7
|
+
require "graphql/armory/subscriptions/ably/webhook"
|
|
8
|
+
|
|
9
|
+
module GraphQL
|
|
10
|
+
module Armory
|
|
11
|
+
module Subscriptions
|
|
12
|
+
# The Ably transport, over Ably's REST API and `Http` (no SDK).
|
|
13
|
+
#
|
|
14
|
+
# schema.use GraphQL::Armory::Subscriptions, transport: Ably.build(key: ENV["ABLY_KEY"])
|
|
15
|
+
#
|
|
16
|
+
# Ably authorises per connection, not per channel: the app's token endpoint answers
|
|
17
|
+
# `schema.subscriptions.authorize_viewer(context:)`, a signed TokenRequest whose capability names the viewer's
|
|
18
|
+
# channels (`<prefix>:<viewer digest>:*`) and no others. Channels are `<prefix>:<viewer digest>:<id>`. The
|
|
19
|
+
# presence webhook reports the clients that left.
|
|
20
|
+
#
|
|
21
|
+
# `Ably.new` is inert (no key, `Http::Null`): it publishes, authorizes and accepts nothing. `Ably.build` checks
|
|
22
|
+
# the settings and wires the real HTTP client.
|
|
23
|
+
class Ably
|
|
24
|
+
DIGEST = /\A(?:[0-9a-f]{64}|#{Record::ANONYMOUS})\z/
|
|
25
|
+
SEGMENT = /\A[A-Za-z0-9_-]+\z/
|
|
26
|
+
HOST = /\A[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?\z/
|
|
27
|
+
MAX_PAYLOAD_BYTES = 262_144
|
|
28
|
+
MAX_TOKEN_TTL = 3600
|
|
29
|
+
# https://ably.com/docs/api/rest-api : a request body is under 2 MiB (and a spec holds up to 100 channels).
|
|
30
|
+
MAX_REQUEST_BYTES = 2 * 1024 * 1024
|
|
31
|
+
MAX_SPECS = 100
|
|
32
|
+
# A payload is sent as a JSON string inside a JSON body: worst case every byte is escaped, plus the spec's own text.
|
|
33
|
+
SPEC_OVERHEAD_BYTES = 512
|
|
34
|
+
DEFAULT_CLOCK = -> { Time.now }
|
|
35
|
+
DEFAULT_NONCE = -> { SecureRandom.alphanumeric(24) }
|
|
36
|
+
|
|
37
|
+
def self.build(key:, prefix: "graphql", max_payload_bytes: 65_536, host: "rest.ably.io", http: Http.build, token_ttl: 3600, clock: DEFAULT_CLOCK, nonce: DEFAULT_NONCE, compress: false)
|
|
38
|
+
parsed = Key.parse(key)
|
|
39
|
+
check(SEGMENT.match?(prefix.to_s) && prefix.is_a?(String), "prefix:", "must be letters, digits, _ or - (no \":\"), got #{prefix.inspect}")
|
|
40
|
+
check(max_payload_bytes.is_a?(Integer) && max_payload_bytes.between?(1, MAX_PAYLOAD_BYTES), "max_payload_bytes:", "must be an Integer up to #{MAX_PAYLOAD_BYTES} (65_536; more only on plans that allow it), got #{max_payload_bytes.inspect}")
|
|
41
|
+
check(host.is_a?(String) && HOST.match?(host), "host:", "must be a host name such as \"rest.ably.io\", got #{host.inspect}")
|
|
42
|
+
check(http.respond_to?(:post), "http:", "must answer post(url, body, headers)")
|
|
43
|
+
check(token_ttl.is_a?(Integer) && token_ttl.between?(1, MAX_TOKEN_TTL), "token_ttl:", "must be 1 to #{MAX_TOKEN_TTL} seconds, got #{token_ttl.inspect}")
|
|
44
|
+
check(clock.respond_to?(:call), "clock:", "must answer call (returning a Time)")
|
|
45
|
+
check(nonce.respond_to?(:call), "nonce:", "must answer call (returning a String)")
|
|
46
|
+
check(compress == true || compress == false, "compress:", "must be true or false, got #{compress.inspect}")
|
|
47
|
+
|
|
48
|
+
new(key: parsed, prefix: prefix, max_payload_bytes: max_payload_bytes, host: host, http: http, token_ttl: token_ttl, clock: clock, nonce: nonce, compress: compress)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.check(condition, option, problem)
|
|
52
|
+
raise ConfigurationError, "#{option} #{problem}" unless condition
|
|
53
|
+
end
|
|
54
|
+
private_class_method :check
|
|
55
|
+
|
|
56
|
+
attr_reader :capabilities
|
|
57
|
+
|
|
58
|
+
def initialize(key: nil, prefix: "graphql", max_payload_bytes: 65_536, host: "rest.ably.io", http: Http::Null.new, token_ttl: 3600, clock: DEFAULT_CLOCK, nonce: DEFAULT_NONCE, compress: false)
|
|
59
|
+
@key = key
|
|
60
|
+
@prefix = prefix
|
|
61
|
+
@clock = clock
|
|
62
|
+
@capabilities = Capabilities.new(
|
|
63
|
+
viewer_bound: true, max_payload_bytes: max_payload_bytes, max_batch: max_batch_for(max_payload_bytes), webhooks: :presence, compress: compress
|
|
64
|
+
)
|
|
65
|
+
return unless key
|
|
66
|
+
|
|
67
|
+
@publisher = Publisher.new(key: key, url: "https://#{host}/messages", http: http, max_payload_bytes: max_payload_bytes, max_batch: @capabilities.max_batch)
|
|
68
|
+
@tokens = TokenRequest.new(key: key, prefix: prefix, ttl_seconds: token_ttl, clock: clock, nonce: nonce)
|
|
69
|
+
@webhook = Webhook.new(key: key, clock: clock, id_from: method(:id_from))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def authorization = :viewer
|
|
73
|
+
|
|
74
|
+
def channel_for(id, viewer_digest)
|
|
75
|
+
raise ArgumentError, "viewer_digest: must be 64 hex characters or #{Record::ANONYMOUS.inspect}" unless viewer_digest.is_a?(String) && DIGEST.match?(viewer_digest)
|
|
76
|
+
raise ArgumentError, "id: must be a subscription id" unless id.is_a?(String) && Record::ID.match?(id)
|
|
77
|
+
|
|
78
|
+
"#{@prefix}:#{viewer_digest}:#{id}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def id_from(channel)
|
|
82
|
+
return nil unless channel.is_a?(String)
|
|
83
|
+
|
|
84
|
+
prefix, digest, id, *rest = channel.split(":", -1)
|
|
85
|
+
return nil unless prefix == @prefix && rest.empty? && digest && id && DIGEST.match?(digest)
|
|
86
|
+
|
|
87
|
+
Record::ID.match?(id) ? id : nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def publish(messages)
|
|
91
|
+
raise ArgumentError, "publish takes an Array of Messages" unless messages.is_a?(Array) && messages.all?(Message)
|
|
92
|
+
return messages.each_index.map { |index| Outcome.new(status: :refused, index: index, reason: :not_configured) } unless @publisher
|
|
93
|
+
|
|
94
|
+
@publisher.call(messages)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# A signed TokenRequest for the channels of `viewer_digest` only, with that digest as its clientId. The core
|
|
98
|
+
# matched the viewer first.
|
|
99
|
+
def authorize_viewer(viewer_digest)
|
|
100
|
+
return nil unless @tokens && viewer_digest.is_a?(String) && DIGEST.match?(viewer_digest)
|
|
101
|
+
|
|
102
|
+
@tokens.call(viewer_digest)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def authorize_channel(channel:, socket_id: nil) = nil
|
|
106
|
+
|
|
107
|
+
def webhook(env)
|
|
108
|
+
return [:refused, :not_configured] unless @webhook
|
|
109
|
+
|
|
110
|
+
@webhook.call(env)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def inspect = "#<#{self.class.name} prefix=#{@prefix.inspect}>"
|
|
114
|
+
alias_method :to_s, :inspect
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# One spec per message; each payload may be escaped twice (a JSON string inside the JSON body), and the whole
|
|
119
|
+
# request must stay under 2 MiB.
|
|
120
|
+
def max_batch_for(max_payload_bytes)
|
|
121
|
+
(MAX_REQUEST_BYTES / (2 * max_payload_bytes + SPEC_OVERHEAD_BYTES)).clamp(1, MAX_SPECS)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
# Reads a schema's subscriptions setup, without running anything, and reports what could let a delivery reach
|
|
7
|
+
# someone it was not computed for, or run without the schema's plugins. Notices list what a reviewer should read
|
|
8
|
+
# (each stored context key with its reason, anonymous mode, fields marked broadcastable) and never fail it.
|
|
9
|
+
#
|
|
10
|
+
# report = GraphQL::Armory::Subscriptions::Audit.(AppSchema)
|
|
11
|
+
# report.ok? # false on an :error finding
|
|
12
|
+
# puts report
|
|
13
|
+
class Audit
|
|
14
|
+
# One finding: `kind` (a Symbol), `severity` (:error or :notice), `message`.
|
|
15
|
+
Finding = Struct.new(:kind, :severity, :message, keyword_init: true) do
|
|
16
|
+
def initialize(...)
|
|
17
|
+
super
|
|
18
|
+
freeze
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_s = "[#{severity}] #{kind}: #{message}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# What the audit found, in order.
|
|
25
|
+
class Report
|
|
26
|
+
attr_reader :findings
|
|
27
|
+
|
|
28
|
+
def initialize(findings = [])
|
|
29
|
+
@findings = findings.dup.freeze
|
|
30
|
+
freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def ok?
|
|
34
|
+
findings.none? { |finding| finding.severity == :error }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_s
|
|
38
|
+
return "subscriptions audit: nothing to report" if findings.empty?
|
|
39
|
+
|
|
40
|
+
(["subscriptions audit: #{ok? ? "ok" : "failed"}"] + findings.map(&:to_s)).join("\n")
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.build
|
|
45
|
+
new
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.call(schema)
|
|
49
|
+
build.(schema)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def call(schema)
|
|
53
|
+
findings = []
|
|
54
|
+
backend = schema.respond_to?(:subscriptions) ? schema.subscriptions : nil
|
|
55
|
+
unless backend.is_a?(Core)
|
|
56
|
+
findings << error(:plugin, "#{schema} does not use GraphQL::Armory::Subscriptions (#{backend.nil? ? "no subscriptions" : backend.class})")
|
|
57
|
+
return Report.new(findings)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
unless backend.schema.equal?(schema)
|
|
61
|
+
findings << error(:child_schema, "#{schema} shares the subscriptions of #{backend.schema}: its deliveries run on #{backend.schema} and its plugins, " \
|
|
62
|
+
"not on #{schema}'s; install GraphQL::Armory::Subscriptions on the schema that executes")
|
|
63
|
+
end
|
|
64
|
+
settings = backend.settings
|
|
65
|
+
if !settings.viewer.anonymous? && !backend.transport.capabilities.viewer_bound?
|
|
66
|
+
findings << error(:transport, "#{backend.transport.class} is not viewer_bound, with a viewer configured: one viewer could read another's channel")
|
|
67
|
+
end
|
|
68
|
+
findings.concat(broadcastable(schema))
|
|
69
|
+
findings << notice(:anonymous, "every subscription is viewer-independent: #{settings.viewer.reason}") if settings.viewer.anonymous?
|
|
70
|
+
settings.context_keys.each do |key, reason|
|
|
71
|
+
findings << notice(:context_key, "context[#{key.inspect}] is stored with every subscription and restored on each delivery: #{reason}")
|
|
72
|
+
end
|
|
73
|
+
Report.new(findings)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def broadcastable(schema)
|
|
79
|
+
root = schema.subscription
|
|
80
|
+
return [] unless root
|
|
81
|
+
|
|
82
|
+
root.all_field_definitions.select { |field| field.respond_to?(:broadcastable?) && field.broadcastable? == true }.map do |field|
|
|
83
|
+
notice(:broadcastable, "#{field.path} is marked broadcastable: true, which is ignored: every subscriber runs with its own context")
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def error(kind, message) = Finding.new(kind: kind, severity: :error, message: message)
|
|
88
|
+
def notice(kind, message) = Finding.new(kind: kind, severity: :notice, message: message)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
module Controls
|
|
7
|
+
# A transport's capabilities: viewer bound, Pusher-sized (10 KB, batches of 10), existence webhooks.
|
|
8
|
+
module Capabilities
|
|
9
|
+
def self.example(**overrides)
|
|
10
|
+
Subscriptions::Capabilities.new(**{ viewer_bound: true, max_payload_bytes: 10_240, max_batch: 10, webhooks: :existence, compress: false }, **overrides)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# A `Store::Substitute` on a fixed clock.
|
|
15
|
+
module Store
|
|
16
|
+
def self.example(now: 1_000_000)
|
|
17
|
+
Subscriptions::Store::Substitute.new(now: now)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# A `Transport::Substitute`: viewer bound, channel authorization unless told otherwise.
|
|
22
|
+
module Transport
|
|
23
|
+
def self.example(capabilities: Capabilities.example, authorization: :channel)
|
|
24
|
+
Subscriptions::Transport::Substitute.new(capabilities: capabilities, authorization: authorization)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# A message as the core publishes it: one result, more to come.
|
|
29
|
+
module Message
|
|
30
|
+
def self.example(channel: "substitute:#{Digest.example("viewer")}:#{Id.example}")
|
|
31
|
+
Subscriptions::Message.new(channel: channel, payload: { result: { "data" => { "messagePosted" => { "id" => "m1" } } }, more: true })
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
module Controls
|
|
7
|
+
# A bare graphql-ruby subscriptions backend that keeps the events graphql-ruby hands `write_subscription` and
|
|
8
|
+
# does nothing else: for tests that need a subscription operation to run (graphql-ruby refuses one without a
|
|
9
|
+
# backend) or the exact `Event` objects the write side sees, without our own backend.
|
|
10
|
+
#
|
|
11
|
+
# schema = Controls::Schema.example(subscriptions: {}, plugin: Controls::EventLog)
|
|
12
|
+
# schema.execute("subscription { roomUpdated(roomId: 5) { id } }", context: ...)
|
|
13
|
+
# schema.subscriptions.events.first.topic # graphql-ruby's own topic String
|
|
14
|
+
# schema.subscriptions.events.first.arguments # the coerced keyword arguments
|
|
15
|
+
class EventLog < GraphQL::Subscriptions
|
|
16
|
+
attr_reader :events
|
|
17
|
+
|
|
18
|
+
def initialize(**options)
|
|
19
|
+
super
|
|
20
|
+
@events = []
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def write_subscription(_query, events)
|
|
24
|
+
@events.concat(events)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "base64"
|
|
5
|
+
|
|
6
|
+
module GraphQL
|
|
7
|
+
module Armory
|
|
8
|
+
module Subscriptions
|
|
9
|
+
module Controls
|
|
10
|
+
# A subscription id in the shape `build_id` makes (32 bytes, urlsafe base64 without padding), fixed per index.
|
|
11
|
+
module Id
|
|
12
|
+
def self.example(index = 0)
|
|
13
|
+
Base64.urlsafe_encode64(::Digest::SHA256.digest("subscription-id-#{index}"), padding: false)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# A digest in the shape the core stores (64 lowercase hex characters), fixed per text.
|
|
18
|
+
module Digest
|
|
19
|
+
def self.example(text = "topic")
|
|
20
|
+
::Digest::SHA256.hexdigest("subscription-digest-#{text}")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
module Controls
|
|
7
|
+
# A record of `user-1` subscribed to `messagePosted(roomId: 5)` through the persisted operation
|
|
8
|
+
# "message-posted", on the interpreter, created at 1_000_000 ms for one hour. Any field can be overridden.
|
|
9
|
+
module Record
|
|
10
|
+
def self.example(**overrides)
|
|
11
|
+
fields = {
|
|
12
|
+
id: Id.example,
|
|
13
|
+
document: "subscription MessagePosted($roomId: ID!) { messagePosted(roomId: $roomId) { id body } }",
|
|
14
|
+
operation_name: "MessagePosted",
|
|
15
|
+
variables: '{"roomId":"5"}',
|
|
16
|
+
operation_id: "message-posted",
|
|
17
|
+
raw_query: false,
|
|
18
|
+
version: "2025-01-01",
|
|
19
|
+
visibility_profile: nil,
|
|
20
|
+
engine: :interpreter,
|
|
21
|
+
viewer_id: "user-1",
|
|
22
|
+
viewer_digest: Digest.example("viewer"),
|
|
23
|
+
context: { "tenant_id" => '"acme"' },
|
|
24
|
+
events: [{ graphql_topic: ":messagePosted:roomId:5", digest: Digest.example("topic") }],
|
|
25
|
+
created_at: 1_000_000,
|
|
26
|
+
expires_at: 1_000_000 + (3_600 * 1000)
|
|
27
|
+
}
|
|
28
|
+
Subscriptions::Record.new(**fields, **overrides)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Subscriptions
|
|
8
|
+
module Controls
|
|
9
|
+
# A schema with the armory plugins a subscription goes through on every delivery, built fresh on every call:
|
|
10
|
+
# Versioning (`Versioning::Controls::Versions`, default :latest), Authorization (`Policy` over a `Directory`,
|
|
11
|
+
# `on_unauthorized:` :null by default), PersistedQueries (strict over `DOCUMENTS`) and RateLimits::Points
|
|
12
|
+
# (charging `context[:rate_limit_key]`, never soft). The subscriptions plugin is installed only when
|
|
13
|
+
# `subscriptions:` options are given, through `plugin` (`GraphQL::Armory::Subscriptions` by default):
|
|
14
|
+
#
|
|
15
|
+
# schema = Controls::Schema.example(subscriptions: { store:, transport:, encoder:, viewer: Controls::Viewer.example(directory), ... })
|
|
16
|
+
# schema.armory_controls # => { directory:, adapter:, rate_store:, manifest:, types: }
|
|
17
|
+
#
|
|
18
|
+
# `subscriptions: {}, plugin: Controls::EventLog` installs a bare graphql-ruby backend that only keeps the
|
|
19
|
+
# events written, for running subscription operations without our own backend.
|
|
20
|
+
#
|
|
21
|
+
# The Subscription root:
|
|
22
|
+
#
|
|
23
|
+
# messagePosted(roomId: ID!, priority: Priority, on: Date, filter: MessageFilter = { priority: LOW }): Message
|
|
24
|
+
# nullable, subscription_scope :tenant_id (a context key the app stores)
|
|
25
|
+
# roomUpdated(roomId: ID!, thread: ID): Room!
|
|
26
|
+
# non-null, no scope
|
|
27
|
+
# inboxChanged: Inbox
|
|
28
|
+
# nullable, subscription_scope :current_user_id (set by Viewer's load)
|
|
29
|
+
#
|
|
30
|
+
# Message, Room and Inbox are read through the policy (a revoked user gets a null `messagePosted`, and no data
|
|
31
|
+
# at all for the non-null `roomUpdated`); `Message.edited` exists from 2025-01-01 on. The fields have no
|
|
32
|
+
# resolvers: on a delivery graphql-ruby's subscription extension answers the triggered object, and without the
|
|
33
|
+
# plugin the root value's method of the same name answers. `Query.ping` answers "pong". The block, when given,
|
|
34
|
+
# runs in the Query type with the types (`Types.example`) and the field options Execution::Next needs.
|
|
35
|
+
module Schema
|
|
36
|
+
Message = Struct.new(:id, :body, :room_id, :edited, keyword_init: true) do
|
|
37
|
+
def initialize(id:, body:, room_id:, edited: false)
|
|
38
|
+
super
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
Room = Struct.new(:id, :name, keyword_init: true)
|
|
42
|
+
Inbox = Struct.new(:unread, keyword_init: true)
|
|
43
|
+
|
|
44
|
+
DOCUMENTS = {
|
|
45
|
+
"ping" => "query Ping { ping }",
|
|
46
|
+
"message-posted" => "subscription MessagePosted($roomId: ID!) { messagePosted(roomId: $roomId) { id body } }",
|
|
47
|
+
"room-updated" => "subscription RoomUpdated($roomId: ID!) { roomUpdated(roomId: $roomId) { id name } }",
|
|
48
|
+
"inbox-changed" => "subscription InboxChanged { inboxChanged { unread } }"
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
NEXT_FIELD_OPTIONS =
|
|
52
|
+
if GraphQL::Schema::Field.instance_method(:initialize).parameters.any? { |_, name| name == :resolve_legacy_instance_method }
|
|
53
|
+
{ resolve_legacy_instance_method: true }.freeze
|
|
54
|
+
else
|
|
55
|
+
{}.freeze
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.example(subscriptions: nil, plugin: nil, directory: Directory.example, adapter: nil, on_unauthorized: :null, strict: true,
|
|
59
|
+
documents: DOCUMENTS, rate_store: nil, rate_limit: 1_000, types: Types.example, &fields)
|
|
60
|
+
adapter ||= Policy.new(directory)
|
|
61
|
+
rate_store ||= RateLimits::Store::Substitute.new
|
|
62
|
+
manifest = PersistedQueries::Manifest.new(documents, digest: false)
|
|
63
|
+
plugin ||= Armory::Subscriptions unless subscriptions.nil?
|
|
64
|
+
query = Class.new(types.fetch(:object)) do
|
|
65
|
+
graphql_name "Query"
|
|
66
|
+
authorize :none, reason: "the control app's public root", covers_fields: true
|
|
67
|
+
field :ping, String, null: false, **NEXT_FIELD_OPTIONS
|
|
68
|
+
define_method(:ping) { "pong" }
|
|
69
|
+
instance_exec(types, NEXT_FIELD_OPTIONS, &fields) if fields
|
|
70
|
+
end
|
|
71
|
+
subscription = Class.new(types.fetch(:object)) do
|
|
72
|
+
graphql_name "Subscription"
|
|
73
|
+
authorize :none, reason: "each payload type is read through the policy", covers_fields: true
|
|
74
|
+
field :message_posted, types.fetch(:message), null: true, subscription_scope: :tenant_id do
|
|
75
|
+
argument :room_id, GraphQL::Types::ID
|
|
76
|
+
argument :priority, types.fetch(:priority), required: false
|
|
77
|
+
argument :on, types.fetch(:date), required: false
|
|
78
|
+
argument :filter, types.fetch(:filter), required: false, default_value: { "priority" => "LOW" }
|
|
79
|
+
end
|
|
80
|
+
field :room_updated, types.fetch(:room), null: false do
|
|
81
|
+
argument :room_id, GraphQL::Types::ID
|
|
82
|
+
argument :thread, GraphQL::Types::ID, required: false
|
|
83
|
+
end
|
|
84
|
+
field :inbox_changed, types.fetch(:inbox), null: true, subscription_scope: :current_user_id
|
|
85
|
+
end
|
|
86
|
+
controls = { directory: directory, adapter: adapter, rate_store: rate_store, manifest: manifest, types: types }.freeze
|
|
87
|
+
armory = Armory
|
|
88
|
+
|
|
89
|
+
Class.new(GraphQL::Schema) do
|
|
90
|
+
use GraphQL::Execution::Next if defined?(GraphQL::Execution::Next)
|
|
91
|
+
query(query)
|
|
92
|
+
subscription(subscription)
|
|
93
|
+
use armory::Versioning, versions: armory::Versioning::Controls::Versions::LABELS,
|
|
94
|
+
retired: armory::Versioning::Controls::Versions::RETIRED, default: :latest
|
|
95
|
+
use armory::Authorization, adapter: adapter, on_unauthorized: on_unauthorized
|
|
96
|
+
use armory::PersistedQueries, store: manifest, strict: strict
|
|
97
|
+
use armory::RateLimits::Points, store: rate_store, limit: rate_limit, soft: false
|
|
98
|
+
use plugin, **subscriptions unless subscriptions.nil?
|
|
99
|
+
define_singleton_method(:armory_controls) { controls }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The types of the control schema, built fresh: base classes with Versioning's and Authorization's `Base`,
|
|
104
|
+
# a `Date` scalar (ISO 8601 in and out), the `Priority` enum (LOW and HIGH, Ruby values :low and :high), the
|
|
105
|
+
# `Window` and `MessageFilter` input objects and the Message, Room and Inbox payload types.
|
|
106
|
+
module Types
|
|
107
|
+
def self.example
|
|
108
|
+
versioning = Versioning::Base
|
|
109
|
+
auth = Authorization::Base
|
|
110
|
+
base_argument = Class.new(GraphQL::Schema::Argument) do
|
|
111
|
+
include versioning
|
|
112
|
+
include auth
|
|
113
|
+
end
|
|
114
|
+
base_field = Class.new(GraphQL::Schema::Field) do
|
|
115
|
+
include versioning
|
|
116
|
+
include auth
|
|
117
|
+
argument_class(base_argument)
|
|
118
|
+
end
|
|
119
|
+
base_object = Class.new(GraphQL::Schema::Object) do
|
|
120
|
+
include versioning
|
|
121
|
+
include auth
|
|
122
|
+
field_class(base_field)
|
|
123
|
+
default_action :read
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
date = Class.new(GraphQL::Schema::Scalar) do
|
|
127
|
+
graphql_name "Date"
|
|
128
|
+
description "An ISO 8601 calendar date"
|
|
129
|
+
|
|
130
|
+
def self.coerce_input(value, _context)
|
|
131
|
+
raise GraphQL::CoercionError, "Date must be an ISO 8601 String" unless value.is_a?(String)
|
|
132
|
+
|
|
133
|
+
::Date.iso8601(value)
|
|
134
|
+
rescue ::Date::Error
|
|
135
|
+
raise GraphQL::CoercionError, "Date must be an ISO 8601 date"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def self.coerce_result(value, _context)
|
|
139
|
+
raise GraphQL::Error, "Date answers a Date" unless value.is_a?(::Date)
|
|
140
|
+
|
|
141
|
+
value.iso8601
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
priority = Class.new(GraphQL::Schema::Enum) do
|
|
145
|
+
graphql_name "Priority"
|
|
146
|
+
value "LOW", value: :low
|
|
147
|
+
value "HIGH", value: :high
|
|
148
|
+
end
|
|
149
|
+
window = Class.new(GraphQL::Schema::InputObject) do
|
|
150
|
+
graphql_name "Window"
|
|
151
|
+
argument :from, date
|
|
152
|
+
argument :to, date, required: false
|
|
153
|
+
end
|
|
154
|
+
filter = Class.new(GraphQL::Schema::InputObject) do
|
|
155
|
+
graphql_name "MessageFilter"
|
|
156
|
+
argument :priority, priority, required: false, default_value: "LOW"
|
|
157
|
+
argument :priorities, [priority], required: false
|
|
158
|
+
argument :window, window, required: false
|
|
159
|
+
argument :min_score, Float, required: false
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
message = Class.new(base_object) do
|
|
163
|
+
graphql_name "Message"
|
|
164
|
+
field :id, GraphQL::Types::ID, null: false
|
|
165
|
+
field :body, String, null: false
|
|
166
|
+
field :room_id, GraphQL::Types::ID, null: false
|
|
167
|
+
field :edited, GraphQL::Types::Boolean, null: false, added: "2025-01-01"
|
|
168
|
+
end
|
|
169
|
+
room = Class.new(base_object) do
|
|
170
|
+
graphql_name "Room"
|
|
171
|
+
field :id, GraphQL::Types::ID, null: false
|
|
172
|
+
field :name, String, null: false
|
|
173
|
+
end
|
|
174
|
+
inbox = Class.new(base_object) do
|
|
175
|
+
graphql_name "Inbox"
|
|
176
|
+
field :unread, Integer, null: false
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
{ argument: base_argument, field: base_field, object: base_object, date: date, priority: priority, window: window, filter: filter,
|
|
180
|
+
message: message, room: room, inbox: inbox }
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
module Controls
|
|
7
|
+
# A user of the control app.
|
|
8
|
+
User = Struct.new(:id, :name, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
# The control app's users: `user-1` (Ada) and `user-2` (Grace). A test revokes a user (still loadable, refused
|
|
11
|
+
# by the policy) or removes one (gone: the viewer no longer loads).
|
|
12
|
+
class Directory
|
|
13
|
+
def self.example
|
|
14
|
+
new([User.new(id: "user-1", name: "Ada"), User.new(id: "user-2", name: "Grace")])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(users = [])
|
|
18
|
+
@users = users.to_h { |user| [user.id, user] }
|
|
19
|
+
@revoked = []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def find(id) = @users[id]
|
|
23
|
+
|
|
24
|
+
def add(user)
|
|
25
|
+
@users[user.id] = user
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def remove(id)
|
|
29
|
+
@users.delete(id)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def revoke(id)
|
|
33
|
+
@revoked << id
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def restore(id)
|
|
37
|
+
@revoked.delete(id)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def allowed?(user)
|
|
41
|
+
!user.nil? && @users.key?(user.id) && !@revoked.include?(user.id)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The `viewer:` option of `use GraphQL::Armory::Subscriptions` for the control app: `dump` answers the current
|
|
46
|
+
# user's id, `load` puts the user (nil when gone) and its id back into the context keys `sets` declares.
|
|
47
|
+
module Viewer
|
|
48
|
+
SETS = %i[current_user current_user_id].freeze
|
|
49
|
+
|
|
50
|
+
def self.example(directory = Directory.example)
|
|
51
|
+
{
|
|
52
|
+
dump: ->(context) { context[:current_user]&.id },
|
|
53
|
+
load: lambda do |id, context|
|
|
54
|
+
user = directory.find(id)
|
|
55
|
+
context[:current_user] = user
|
|
56
|
+
context[:current_user_id] = user&.id
|
|
57
|
+
end,
|
|
58
|
+
sets: SETS
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A Pundit-style Authorization adapter over a Directory: the actor is `context[:current_user]`, and a record
|
|
64
|
+
# is readable while that user exists and is not revoked. Records the questions it was asked.
|
|
65
|
+
class Policy
|
|
66
|
+
attr_reader :asked
|
|
67
|
+
|
|
68
|
+
def self.example(directory = Directory.example)
|
|
69
|
+
new(directory)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def initialize(directory)
|
|
73
|
+
@directory = directory
|
|
74
|
+
@asked = []
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def actor(context)
|
|
78
|
+
context[:current_user]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def authorized?(actor, subject, rule)
|
|
82
|
+
@asked << [actor&.id, subject.class, rule]
|
|
83
|
+
@directory.allowed?(actor)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def scope(actor, items, _rule)
|
|
87
|
+
return items if @directory.allowed?(actor)
|
|
88
|
+
|
|
89
|
+
items.respond_to?(:none) ? items.none : []
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def check(_type, _rule)
|
|
93
|
+
[]
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Subscriptions
|
|
6
|
+
# Test data for subscriptions, reusable by apps and other gems: ids, digests, records, capabilities, the stand-in
|
|
7
|
+
# dependencies, a viewer directory with its Pundit-style policy, and a control schema.
|
|
8
|
+
module Controls
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
require "graphql/armory/subscriptions/controls/id"
|
|
15
|
+
require "graphql/armory/subscriptions/controls/record"
|
|
16
|
+
require "graphql/armory/subscriptions/controls/dependencies"
|
|
17
|
+
require "graphql/armory/subscriptions/controls/viewer"
|
|
18
|
+
require "graphql/armory/subscriptions/controls/event_log"
|
|
19
|
+
require "graphql/armory/subscriptions/controls/schema"
|