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.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +5 -5
  4. data/docs/authorization.md +4 -0
  5. data/docs/encrypted-ids.md +24 -2
  6. data/docs/persisted-queries.md +4 -0
  7. data/docs/rate-limiting.md +23 -0
  8. data/docs/response-caching.md +590 -0
  9. data/docs/subscriptions.md +261 -0
  10. data/docs/telemetry.md +39 -1
  11. data/docs/versioning.md +5 -0
  12. data/lib/graphql/armory/authorization/authorize.rb +3 -0
  13. data/lib/graphql/armory/authorization/scope.rb +2 -0
  14. data/lib/graphql/armory/caching/audit/command.rb +51 -0
  15. data/lib/graphql/armory/caching/audit/context_class.rb +117 -0
  16. data/lib/graphql/armory/caching/audit/declarations.rb +166 -0
  17. data/lib/graphql/armory/caching/audit/own_checks.rb +150 -0
  18. data/lib/graphql/armory/caching/audit/public_rules.rb +158 -0
  19. data/lib/graphql/armory/caching/audit/report.rb +113 -0
  20. data/lib/graphql/armory/caching/audit.rb +136 -0
  21. data/lib/graphql/armory/caching/base.rb +118 -0
  22. data/lib/graphql/armory/caching/canonical.rb +138 -0
  23. data/lib/graphql/armory/caching/charge.rb +86 -0
  24. data/lib/graphql/armory/caching/codec.rb +108 -0
  25. data/lib/graphql/armory/caching/context_methods.rb +88 -0
  26. data/lib/graphql/armory/caching/controls/app_schema.rb +70 -0
  27. data/lib/graphql/armory/caching/controls/context.rb +30 -0
  28. data/lib/graphql/armory/caching/controls/operations.rb +37 -0
  29. data/lib/graphql/armory/caching/controls/schema.rb +123 -0
  30. data/lib/graphql/armory/caching/controls/store.rb +16 -0
  31. data/lib/graphql/armory/caching/controls/walk_schema.rb +197 -0
  32. data/lib/graphql/armory/caching/controls.rb +19 -0
  33. data/lib/graphql/armory/caching/digester.rb +48 -0
  34. data/lib/graphql/armory/caching/errors.rb +16 -0
  35. data/lib/graphql/armory/caching/gate/admission.rb +67 -0
  36. data/lib/graphql/armory/caching/gate/signals.rb +65 -0
  37. data/lib/graphql/armory/caching/gate/store_check.rb +60 -0
  38. data/lib/graphql/armory/caching/gate/substitute.rb +61 -0
  39. data/lib/graphql/armory/caching/gate.rb +384 -0
  40. data/lib/graphql/armory/caching/integration/guard.rb +55 -0
  41. data/lib/graphql/armory/caching/integration/install.rb +262 -0
  42. data/lib/graphql/armory/caching/integration/query.rb +42 -0
  43. data/lib/graphql/armory/caching/integration.rb +141 -0
  44. data/lib/graphql/armory/caching/keyer.rb +208 -0
  45. data/lib/graphql/armory/caching/memory_store.rb +164 -0
  46. data/lib/graphql/armory/caching/nulls.rb +121 -0
  47. data/lib/graphql/armory/caching/purge.rb +98 -0
  48. data/lib/graphql/armory/caching/record_keys.rb +109 -0
  49. data/lib/graphql/armory/caching/recorder/context.rb +178 -0
  50. data/lib/graphql/armory/caching/recorder/log.rb +142 -0
  51. data/lib/graphql/armory/caching/recorder/null.rb +24 -0
  52. data/lib/graphql/armory/caching/recorder/shared_writes.rb +58 -0
  53. data/lib/graphql/armory/caching/recorder/substitute.rb +119 -0
  54. data/lib/graphql/armory/caching/recorder.rb +34 -0
  55. data/lib/graphql/armory/caching/redis_store/scripts.rb +159 -0
  56. data/lib/graphql/armory/caching/redis_store.rb +162 -0
  57. data/lib/graphql/armory/caching/rspec.rb +25 -0
  58. data/lib/graphql/armory/caching/schema_identity.rb +102 -0
  59. data/lib/graphql/armory/caching/store/null.rb +31 -0
  60. data/lib/graphql/armory/caching/store/substitute.rb +144 -0
  61. data/lib/graphql/armory/caching/store.rb +34 -0
  62. data/lib/graphql/armory/caching/tags.rb +39 -0
  63. data/lib/graphql/armory/caching/values/decision.rb +45 -0
  64. data/lib/graphql/armory/caching/values/declaration.rb +114 -0
  65. data/lib/graphql/armory/caching/values/entry.rb +108 -0
  66. data/lib/graphql/armory/caching/values/key.rb +39 -0
  67. data/lib/graphql/armory/caching/values/lookup.rb +35 -0
  68. data/lib/graphql/armory/caching/values/policy.rb +44 -0
  69. data/lib/graphql/armory/caching/viewer.rb +112 -0
  70. data/lib/graphql/armory/caching/walk/directives.rb +45 -0
  71. data/lib/graphql/armory/caching/walk/relay.rb +54 -0
  72. data/lib/graphql/armory/caching/walk/run.rb +130 -0
  73. data/lib/graphql/armory/caching/walk/tally.rb +38 -0
  74. data/lib/graphql/armory/caching/walk.rb +46 -0
  75. data/lib/graphql/armory/caching.rb +69 -0
  76. data/lib/graphql/armory/encoder/legacy.rb +14 -0
  77. data/lib/graphql/armory/encoder/null.rb +14 -1
  78. data/lib/graphql/armory/encoder/substitute.rb +54 -2
  79. data/lib/graphql/armory/encoder/versioned.rb +36 -0
  80. data/lib/graphql/armory/encoder.rb +80 -1
  81. data/lib/graphql/armory/persisted_queries.rb +8 -1
  82. data/lib/graphql/armory/rate_limits/cached_charge.rb +43 -0
  83. data/lib/graphql/armory/rate_limits/concurrency.rb +12 -5
  84. data/lib/graphql/armory/rate_limits/invalid_queries.rb +3 -2
  85. data/lib/graphql/armory/rate_limits/points.rb +21 -9
  86. data/lib/graphql/armory/rate_limits/time.rb +15 -8
  87. data/lib/graphql/armory/rate_limits.rb +9 -0
  88. data/lib/graphql/armory/subscriptions/ably/key.rb +49 -0
  89. data/lib/graphql/armory/subscriptions/ably/publisher.rb +132 -0
  90. data/lib/graphql/armory/subscriptions/ably/token_request.rb +42 -0
  91. data/lib/graphql/armory/subscriptions/ably/webhook.rb +96 -0
  92. data/lib/graphql/armory/subscriptions/ably.rb +126 -0
  93. data/lib/graphql/armory/subscriptions/audit.rb +92 -0
  94. data/lib/graphql/armory/subscriptions/controls/dependencies.rb +37 -0
  95. data/lib/graphql/armory/subscriptions/controls/event_log.rb +30 -0
  96. data/lib/graphql/armory/subscriptions/controls/id.rb +26 -0
  97. data/lib/graphql/armory/subscriptions/controls/record.rb +34 -0
  98. data/lib/graphql/armory/subscriptions/controls/schema.rb +187 -0
  99. data/lib/graphql/armory/subscriptions/controls/viewer.rb +99 -0
  100. data/lib/graphql/armory/subscriptions/controls.rb +19 -0
  101. data/lib/graphql/armory/subscriptions/core/channels.rb +93 -0
  102. data/lib/graphql/armory/subscriptions/core/clock.rb +40 -0
  103. data/lib/graphql/armory/subscriptions/core/controls.rb +29 -0
  104. data/lib/graphql/armory/subscriptions/core/delivery.rb +250 -0
  105. data/lib/graphql/armory/subscriptions/core/settings.rb +182 -0
  106. data/lib/graphql/armory/subscriptions/core/trigger.rb +63 -0
  107. data/lib/graphql/armory/subscriptions/core/viewer.rb +153 -0
  108. data/lib/graphql/armory/subscriptions/core/write.rb +172 -0
  109. data/lib/graphql/armory/subscriptions/core.rb +200 -0
  110. data/lib/graphql/armory/subscriptions/errors.rb +30 -0
  111. data/lib/graphql/armory/subscriptions/http/null.rb +17 -0
  112. data/lib/graphql/armory/subscriptions/http/substitute.rb +56 -0
  113. data/lib/graphql/armory/subscriptions/http.rb +93 -0
  114. data/lib/graphql/armory/subscriptions/memory_store.rb +115 -0
  115. data/lib/graphql/armory/subscriptions/pusher/signature.rb +45 -0
  116. data/lib/graphql/armory/subscriptions/pusher.rb +192 -0
  117. data/lib/graphql/armory/subscriptions/redis_store/scripts.rb +115 -0
  118. data/lib/graphql/armory/subscriptions/redis_store.rb +157 -0
  119. data/lib/graphql/armory/subscriptions/store/null.rb +23 -0
  120. data/lib/graphql/armory/subscriptions/store/substitute.rb +155 -0
  121. data/lib/graphql/armory/subscriptions/store.rb +38 -0
  122. data/lib/graphql/armory/subscriptions/topic.rb +253 -0
  123. data/lib/graphql/armory/subscriptions/transport/null.rb +28 -0
  124. data/lib/graphql/armory/subscriptions/transport/substitute.rb +150 -0
  125. data/lib/graphql/armory/subscriptions/transport.rb +41 -0
  126. data/lib/graphql/armory/subscriptions/values/capabilities.rb +50 -0
  127. data/lib/graphql/armory/subscriptions/values/message.rb +31 -0
  128. data/lib/graphql/armory/subscriptions/values/outcome.rb +42 -0
  129. data/lib/graphql/armory/subscriptions/values/record.rb +176 -0
  130. data/lib/graphql/armory/subscriptions/webhook_app.rb +31 -0
  131. data/lib/graphql/armory/subscriptions.rb +33 -0
  132. data/lib/graphql/armory/telemetry.rb +8 -4
  133. data/lib/graphql/armory/version.rb +1 -1
  134. data/lib/graphql/armory/versioning/audit.rb +1 -9
  135. data/lib/graphql/armory/versioning/integration.rb +60 -2
  136. data/lib/graphql/armory.rb +2 -0
  137. data/lib/tasks/graphql_armory.rake +9 -0
  138. metadata +110 -1
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Subscriptions
8
+ module Transport
9
+ # A transport that records what it published, answers the outcomes it is told, declares whatever capabilities
10
+ # and authorization mode it is given, and fails when told: for tests, instead of hand-written fakes. It keeps
11
+ # the transport contract it can check: a batch over `max_batch` or a payload over `max_payload_bytes` raises
12
+ # ArgumentError (a bug in the caller), and channels are `<prefix>:<viewer digest>:<id>`.
13
+ #
14
+ # transport = Transport::Substitute.new(authorization: :viewer)
15
+ # transport.answer_publish(:gone, channel: channel)
16
+ # transport.answer(:webhook, [:ok, [channel]])
17
+ # transport.published # => [Message, ...]
18
+ class Substitute
19
+ OPERATIONS = %i[publish authorize_channel authorize_viewer webhook].freeze
20
+ ANSWERABLE = %i[authorize_channel authorize_viewer webhook].freeze
21
+ SEGMENT = /\A[A-Za-z0-9_-]+\z/
22
+ DEFAULT_CAPABILITIES = Capabilities.new(viewer_bound: true, max_payload_bytes: 10_240, max_batch: 10, webhooks: :existence, compress: false)
23
+
24
+ attr_reader :capabilities, :authorization, :published, :batches, :calls
25
+
26
+ def initialize(capabilities: DEFAULT_CAPABILITIES, authorization: :channel, prefix: "substitute")
27
+ raise ArgumentError, "capabilities: must be a Capabilities, got #{capabilities.class}" unless capabilities.is_a?(Capabilities)
28
+ raise ArgumentError, "authorization: must be one of #{AUTHORIZATIONS.inspect}, got #{authorization.inspect}" unless AUTHORIZATIONS.include?(authorization)
29
+ raise ArgumentError, "prefix: must be letters, digits, _ or -" unless prefix.is_a?(String) && SEGMENT.match?(prefix)
30
+
31
+ @capabilities = capabilities
32
+ @authorization = authorization
33
+ @prefix = prefix
34
+ @published = []
35
+ @batches = []
36
+ @calls = []
37
+ @statuses = {}
38
+ @default_status = [:ok, nil]
39
+ @answers = {}
40
+ @failures = {}
41
+ end
42
+
43
+ # Answers `status` (with `reason`) for every message to `channel`, or to every channel without one of its own.
44
+ def answer_publish(status, channel: nil, reason: nil)
45
+ raise ArgumentError, "status must be one of #{Outcome::STATUSES.inspect}, got #{status.inspect}" unless Outcome::STATUSES.include?(status)
46
+
47
+ channel ? @statuses[channel] = [status, reason] : @default_status = [status, reason]
48
+ end
49
+
50
+ # Answers `value` from `operation` (an authorization or a webhook) instead of the real answer.
51
+ def answer(operation, value)
52
+ raise ArgumentError, "operation must be one of #{ANSWERABLE.inspect} (publish: answer_publish)" unless ANSWERABLE.include?(operation)
53
+
54
+ @answers[operation] = value
55
+ end
56
+
57
+ # Raises `error` from `on` (an operation, or :all) for the next `times` calls, or every call until `recover`.
58
+ def fail_with(error, on: :all, times: nil)
59
+ raise ArgumentError, "on: must be one of #{(OPERATIONS + [:all]).inspect}, got #{on.inspect}" unless (OPERATIONS + [:all]).include?(on)
60
+ raise ArgumentError, "times: must be nil (every call) or a positive Integer, got #{times.inspect}" unless times.nil? || (times.is_a?(Integer) && times.positive?)
61
+
62
+ @failures[on] = [error, times]
63
+ end
64
+
65
+ def recover
66
+ @failures.clear
67
+ end
68
+
69
+ # The messages published to `channel`, in order.
70
+ def delivered_to(channel)
71
+ published.select { |message| message.channel == channel }
72
+ end
73
+
74
+ def channel_for(id, viewer_digest)
75
+ [@prefix, viewer_digest, id].join(":")
76
+ end
77
+
78
+ def id_from(channel)
79
+ return nil unless channel.is_a?(String)
80
+
81
+ prefix, digest, id, *rest = channel.split(":", -1)
82
+ return nil unless prefix == @prefix && rest.empty? && [digest, id].all? { |part| part && SEGMENT.match?(part) }
83
+
84
+ id
85
+ end
86
+
87
+ def publish(messages)
88
+ check(messages)
89
+ perform(:publish, size: messages.size) do
90
+ @batches << messages.dup.freeze
91
+ @published.concat(messages)
92
+ messages.each_with_index.map do |message, index|
93
+ status, reason = @statuses.fetch(message.channel, @default_status)
94
+ Outcome.new(status: status, index: index, reason: reason)
95
+ end
96
+ end
97
+ end
98
+
99
+ def authorize_channel(channel:, socket_id: nil)
100
+ perform(:authorize_channel, channel: channel, socket_id: socket_id) do
101
+ authorization == :channel ? { auth: "substitute:#{socket_id}:#{channel}" } : nil
102
+ end
103
+ end
104
+
105
+ def authorize_viewer(viewer_digest)
106
+ perform(:authorize_viewer, viewer_digest: viewer_digest) do
107
+ authorization == :viewer ? { token: "substitute:#{viewer_digest}" } : nil
108
+ end
109
+ end
110
+
111
+ def webhook(env)
112
+ perform(:webhook, env: env) { [:ok, []] }
113
+ end
114
+
115
+ private
116
+
117
+ def check(messages)
118
+ raise ArgumentError, "publish takes a non-empty Array of Messages" unless messages.is_a?(Array) && !messages.empty? && messages.all?(Message)
119
+ raise ArgumentError, "publish got #{messages.size} messages, over max_batch #{capabilities.max_batch}" if messages.size > capabilities.max_batch
120
+
121
+ messages.each do |message|
122
+ bytes = JSON.generate(message.payload).bytesize
123
+ raise ArgumentError, "a payload of #{bytes} bytes is over max_payload_bytes #{capabilities.max_payload_bytes}" if bytes > capabilities.max_payload_bytes
124
+ end
125
+ end
126
+
127
+ def perform(operation, **arguments)
128
+ @calls << [operation, arguments]
129
+ fail!(operation)
130
+ return @answers[operation] if @answers.key?(operation)
131
+
132
+ yield
133
+ end
134
+
135
+ def fail!(operation)
136
+ on = @failures.key?(operation) ? operation : :all
137
+ error, times = @failures[on]
138
+ return unless error
139
+
140
+ if times
141
+ times -= 1
142
+ times.zero? ? @failures.delete(on) : @failures[on] = [error, times]
143
+ end
144
+ raise error
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # How messages reach clients: `Pusher`, `Ably`, or any object answering the same methods. Transports talk to
7
+ # their provider over `Http` and never require an SDK.
8
+ #
9
+ # transport.capabilities -> Capabilities # checked by `build` (viewer_bound, limits, webhooks)
10
+ # transport.authorization -> :channel | :viewer # how a client is let onto its channels
11
+ # transport.channel_for(id, viewer_digest) -> String # the channel of one subscription (nil: none)
12
+ # transport.id_from(channel) -> id | nil # the subscription id a channel name holds
13
+ # transport.publish([Message, ...]) -> [Outcome, ...] # one Outcome per message, by index
14
+ # transport.authorize_channel(channel:, socket_id:) -> Hash | nil # :channel transports
15
+ # transport.authorize_viewer(viewer_digest) -> Hash | nil # :viewer transports
16
+ # transport.webhook(rack_env) -> [:ok, [channel, ...]] | [:refused, reason]
17
+ #
18
+ # The contract:
19
+ #
20
+ # - `channel_for` binds the channel to the viewer digest as well as the id (the id alone is a bearer token on a
21
+ # hosted service), and `id_from(channel_for(id, digest)) == id`. `id_from` answers nil for any other text.
22
+ # - `publish` is called with at most `capabilities.max_batch` messages, each payload at most
23
+ # `capabilities.max_payload_bytes` once JSON-encoded. It answers `:gone` only when the provider says the channel
24
+ # is gone, and raises `TransportError` (never a provider's own error) when the whole call failed.
25
+ # - `authorize_channel` is called only after the core matched the stored viewer digest with the requester's; it
26
+ # signs what the provider needs for that channel. `authorize_viewer` answers credentials (a token request) that
27
+ # reach that viewer's channels only. Each answers nil in the mode it does not implement.
28
+ # - `webhook` verifies the provider's signature in constant time and refuses an old, oversized or unsigned body
29
+ # before reading what it reports; it answers the channels the provider reports gone, nothing else.
30
+ module Transport
31
+ AUTHORIZATIONS = %i[channel viewer].freeze
32
+
33
+ # The transport's declared capabilities: the same value as `Subscriptions::Capabilities`.
34
+ Capabilities = Subscriptions::Capabilities
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ require "graphql/armory/subscriptions/transport/null"
41
+ require "graphql/armory/subscriptions/transport/substitute"
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # What a transport can do, declared by the transport and checked by `build`:
7
+ #
8
+ # viewer_bound the transport can refuse a client a channel (Pusher private channels, Ably capability
9
+ # tokens); required when a viewer is configured
10
+ # max_payload_bytes the largest message the provider accepts, in bytes (0: nothing fits)
11
+ # max_batch how many messages one `publish` may carry
12
+ # webhooks :presence, :existence or nil: what the provider reports when a client goes away
13
+ # compress whether the client accepts `compressed_result` (deflate + base64)
14
+ class Capabilities
15
+ WEBHOOKS = [nil, :presence, :existence].freeze
16
+
17
+ attr_reader :max_payload_bytes, :max_batch, :webhooks
18
+
19
+ def initialize(viewer_bound:, max_payload_bytes:, max_batch:, webhooks: nil, compress: false)
20
+ raise ArgumentError, "Capabilities viewer_bound: must be true or false" unless viewer_bound == true || viewer_bound == false
21
+ raise ArgumentError, "Capabilities max_payload_bytes: must be a non-negative Integer" unless max_payload_bytes.is_a?(Integer) && max_payload_bytes >= 0
22
+ raise ArgumentError, "Capabilities max_batch: must be a positive Integer" unless max_batch.is_a?(Integer) && max_batch.positive?
23
+ raise ArgumentError, "Capabilities webhooks: must be one of #{WEBHOOKS.inspect}" unless WEBHOOKS.include?(webhooks)
24
+ raise ArgumentError, "Capabilities compress: must be true or false" unless compress == true || compress == false
25
+
26
+ @viewer_bound = viewer_bound
27
+ @max_payload_bytes = max_payload_bytes
28
+ @max_batch = max_batch
29
+ @webhooks = webhooks
30
+ @compress = compress
31
+ freeze
32
+ end
33
+
34
+ def viewer_bound? = @viewer_bound
35
+ def compress? = @compress
36
+
37
+ def to_h
38
+ { viewer_bound: @viewer_bound, max_payload_bytes: max_payload_bytes, max_batch: max_batch, webhooks: webhooks, compress: @compress }
39
+ end
40
+
41
+ def ==(other)
42
+ other.is_a?(Capabilities) && other.to_h == to_h
43
+ end
44
+ alias_method :eql?, :==
45
+
46
+ def hash = to_h.hash
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # One message for a transport to publish: `payload` (`{ result: ... | compressed_result: ..., more: ... }`, the
7
+ # graphql-ruby client's wire format) sent on `channel` under `event` ("update", the event its PusherLink and
8
+ # AblyLink listen to). The payload is kept as given, not copied.
9
+ class Message
10
+ EVENT = "update"
11
+
12
+ attr_reader :channel, :event, :payload
13
+
14
+ def initialize(channel:, payload:, event: EVENT)
15
+ raise ArgumentError, "Message channel: must be a non-empty String" unless channel.is_a?(String) && !channel.empty?
16
+ raise ArgumentError, "Message payload: must be a Hash" unless payload.is_a?(Hash)
17
+ raise ArgumentError, "Message event: must be a non-empty String" unless event.is_a?(String) && !event.empty?
18
+
19
+ @channel = channel.dup.freeze
20
+ @event = event.dup.freeze
21
+ @payload = payload
22
+ freeze
23
+ end
24
+
25
+ # The channel holds the subscription id: never in a log.
26
+ def inspect = "#<#{self.class.name} event=#{event.inspect}>"
27
+ alias_method :to_s, :inspect
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # What happened to the message at `index` of one `publish`:
7
+ #
8
+ # :ok accepted by the provider
9
+ # :gone the channel no longer exists for the provider: the core deletes the subscription
10
+ # :retry a transient failure (rate limited, a timeout): kept, not retried inline
11
+ # :refused the provider refused it for good (too large, a bad request); `reason` says why
12
+ class Outcome
13
+ STATUSES = %i[ok gone retry refused].freeze
14
+
15
+ attr_reader :status, :index, :reason
16
+
17
+ def initialize(status:, index:, reason: nil)
18
+ raise ArgumentError, "Outcome status: must be one of #{STATUSES.inspect}" unless STATUSES.include?(status)
19
+ raise ArgumentError, "Outcome index: must be a non-negative Integer" unless index.is_a?(Integer) && index >= 0
20
+ raise ArgumentError, "Outcome reason: must be a Symbol or nil" unless reason.nil? || reason.is_a?(Symbol)
21
+
22
+ @status = status
23
+ @index = index
24
+ @reason = reason
25
+ freeze
26
+ end
27
+
28
+ def ok? = status == :ok
29
+ def gone? = status == :gone
30
+ def retry? = status == :retry
31
+ def refused? = status == :refused
32
+
33
+ def ==(other)
34
+ other.is_a?(Outcome) && [other.status, other.index, other.reason] == [status, index, reason]
35
+ end
36
+ alias_method :eql?, :==
37
+
38
+ def hash = [status, index, reason].hash
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # One subscription, as the store keeps it (sealed). Built when the subscription is created, read on every
7
+ # delivery. Nothing else from the request's context is kept: the viewer is reloaded from `viewer_id`, and only
8
+ # the `context_keys:` values are stored, already dumped.
9
+ #
10
+ # id the server-made id: 32 random bytes, urlsafe base64 (also the transport's channel id)
11
+ # document the query string
12
+ # operation_name String or nil
13
+ # variables the client's variables as plain JSON (a String), never graphql-ruby's Serialize format
14
+ # operation_id the persisted operation's id, or nil
15
+ # raw_query whether the request ran with `armory_allow_raw_query: true`
16
+ # version the label Versioning decided for the request, or nil
17
+ # visibility_profile String or nil
18
+ # engine :interpreter or :next
19
+ # viewer_id what `viewer: { dump: }` answered, nil when anonymous
20
+ # viewer_digest 64 hex characters (the keyed digest of viewer_id), or ANONYMOUS
21
+ # context { "key" => dumped value } for each stored context key
22
+ # events one Event per root field: graphql-ruby's own `event.topic` and our topic digest
23
+ # created_at Integer milliseconds
24
+ # expires_at Integer milliseconds, after created_at
25
+ #
26
+ # `to_h` answers JSON values under String keys; `from_h` takes exactly that shape back and refuses anything
27
+ # else with ArgumentError (a record that does not parse is never loaded).
28
+ class Record
29
+ FORMAT = 1
30
+ ANONYMOUS = "anonymous"
31
+ ENGINES = %i[interpreter next].freeze
32
+ ID = /\A[A-Za-z0-9_-]{22,128}\z/
33
+ HEX = /\A[0-9a-f]{64}\z/
34
+ FIELDS = %i[id document operation_name variables operation_id raw_query version visibility_profile engine viewer_id
35
+ viewer_digest context events created_at expires_at].freeze
36
+ KEYS = (["v"] + FIELDS.map(&:to_s)).freeze
37
+
38
+ # One root field of the subscription: graphql-ruby's topic String, passed back as `subscription_topic:` on a
39
+ # delivery so its field extension fires, and our digest, the store's topic index key.
40
+ Event = Struct.new(:graphql_topic, :digest, keyword_init: true) do
41
+ def initialize(graphql_topic:, digest:)
42
+ raise ArgumentError, "Record events: each graphql_topic must be a non-empty String" unless graphql_topic.is_a?(String) && !graphql_topic.empty?
43
+ raise ArgumentError, "Record events: each digest must be 64 hex characters" unless digest.is_a?(String) && HEX.match?(digest)
44
+
45
+ super(graphql_topic: graphql_topic.dup.freeze, digest: digest.dup.freeze)
46
+ freeze
47
+ end
48
+
49
+ def inspect = "#<#{self.class.name}>"
50
+ end
51
+
52
+ attr_reader(*FIELDS)
53
+
54
+ def initialize(id:, document:, operation_name:, variables:, operation_id:, raw_query:, version:, visibility_profile:, engine:,
55
+ viewer_id:, viewer_digest:, context:, events:, created_at:, expires_at:)
56
+ raise ArgumentError, "Record id: must be 22 to 128 urlsafe base64 characters" unless id.is_a?(String) && ID.match?(id)
57
+ raise ArgumentError, "Record document: must be a non-empty String" unless text?(document)
58
+ raise ArgumentError, "Record operation_name: must be a non-empty String or nil" unless operation_name.nil? || text?(operation_name)
59
+ raise ArgumentError, "Record variables: must be a JSON String" unless variables.is_a?(String)
60
+ raise ArgumentError, "Record operation_id: must be a non-empty String or nil" unless operation_id.nil? || text?(operation_id)
61
+ raise ArgumentError, "Record raw_query: must be true or false" unless raw_query == true || raw_query == false
62
+ raise ArgumentError, "Record version: must be a non-empty String or nil" unless version.nil? || text?(version)
63
+ raise ArgumentError, "Record visibility_profile: must be a non-empty String or nil" unless visibility_profile.nil? || text?(visibility_profile)
64
+ raise ArgumentError, "Record engine: must be one of #{ENGINES.inspect}" unless ENGINES.include?(engine)
65
+ raise ArgumentError, "Record viewer_id: must be a non-empty String or nil" unless viewer_id.nil? || text?(viewer_id)
66
+ raise ArgumentError, "Record viewer_digest: must be 64 hex characters or #{ANONYMOUS.inspect}" unless viewer_digest == ANONYMOUS || (viewer_digest.is_a?(String) && HEX.match?(viewer_digest))
67
+ raise ArgumentError, "Record viewer_id: must be nil exactly when viewer_digest is #{ANONYMOUS.inspect}" unless viewer_id.nil? == (viewer_digest == ANONYMOUS)
68
+ raise ArgumentError, "Record context: must be a Hash of String keys to dumped String values" unless context.is_a?(Hash) && context.all? { |key, value| text?(key) && value.is_a?(String) }
69
+ raise ArgumentError, "Record created_at: must be non-negative Integer milliseconds" unless created_at.is_a?(Integer) && created_at >= 0
70
+ raise ArgumentError, "Record expires_at: must be Integer milliseconds after created_at" unless expires_at.is_a?(Integer) && expires_at > created_at
71
+
72
+ @id = id.dup.freeze
73
+ @document = document.dup.freeze
74
+ @operation_name = operation_name&.dup&.freeze
75
+ @variables = variables.dup.freeze
76
+ @operation_id = operation_id&.dup&.freeze
77
+ @raw_query = raw_query
78
+ @version = version&.dup&.freeze
79
+ @visibility_profile = visibility_profile&.dup&.freeze
80
+ @engine = engine
81
+ @viewer_id = viewer_id&.dup&.freeze
82
+ @viewer_digest = viewer_digest.dup.freeze
83
+ @context = context.to_h { |key, value| [key.dup.freeze, value.dup.freeze] }.freeze
84
+ @events = build_events(events)
85
+ @created_at = created_at
86
+ @expires_at = expires_at
87
+ freeze
88
+ end
89
+
90
+ # Takes back what `to_h` answered (after a JSON round trip): exactly its keys, as Strings, with the same
91
+ # types. Raises ArgumentError for anything else.
92
+ def self.from_h(hash)
93
+ raise ArgumentError, "Record.from_h: needs a Hash, got #{hash.class}" unless hash.is_a?(Hash)
94
+ raise ArgumentError, "Record.from_h: needs exactly the keys #{KEYS.inspect}" unless hash.keys.sort == KEYS.sort
95
+ raise ArgumentError, "Record.from_h: format #{FORMAT} only" unless hash["v"] == FORMAT
96
+
97
+ engine = ENGINES.find { |name| name.to_s == hash["engine"] }
98
+ raise ArgumentError, "Record engine: must be one of #{ENGINES.map(&:to_s).inspect}" unless engine
99
+
100
+ events = hash["events"]
101
+ unless events.is_a?(Array) && events.all? { |event| event.is_a?(Hash) && event.keys.sort == %w[digest graphql_topic] }
102
+ raise ArgumentError, "Record events: must be an Array of { \"graphql_topic\", \"digest\" } objects"
103
+ end
104
+
105
+ new(**(FIELDS - %i[engine events]).to_h { |field| [field, hash[field.to_s]] },
106
+ engine: engine, events: events.map { |event| { graphql_topic: event["graphql_topic"], digest: event["digest"] } })
107
+ end
108
+
109
+ def to_h
110
+ {
111
+ "v" => FORMAT, "id" => id, "document" => document, "operation_name" => operation_name, "variables" => variables,
112
+ "operation_id" => operation_id, "raw_query" => raw_query, "version" => version, "visibility_profile" => visibility_profile,
113
+ "engine" => engine.to_s, "viewer_id" => viewer_id, "viewer_digest" => viewer_digest, "context" => context,
114
+ "events" => events.map { |event| { "graphql_topic" => event.graphql_topic, "digest" => event.digest } },
115
+ "created_at" => created_at, "expires_at" => expires_at
116
+ }
117
+ end
118
+
119
+ # The topic digests, the store's index keys.
120
+ def topics
121
+ events.map(&:digest)
122
+ end
123
+
124
+ # graphql-ruby's topic stored beside `digest`, or nil when this record was not written under that digest
125
+ # (the index and the record drifted: never execute it for that topic).
126
+ def graphql_topic(digest)
127
+ events.find { |event| event.digest == digest }&.graphql_topic
128
+ end
129
+
130
+ def expired?(now)
131
+ now >= expires_at
132
+ end
133
+
134
+ def anonymous?
135
+ viewer_digest == ANONYMOUS
136
+ end
137
+
138
+ def ==(other)
139
+ other.is_a?(Record) && other.to_h == to_h
140
+ end
141
+ alias_method :eql?, :==
142
+
143
+ def hash = to_h.hash
144
+
145
+ # No document, variables, viewer or id: a record may end up in a log.
146
+ def inspect = "#<#{self.class.name} engine=#{engine.inspect} events=#{events.size}>"
147
+ alias_method :to_s, :inspect
148
+
149
+ private
150
+
151
+ def text?(value)
152
+ value.is_a?(String) && !value.empty?
153
+ end
154
+
155
+ def build_events(events)
156
+ raise ArgumentError, "Record events: must be a non-empty Array" unless events.is_a?(Array) && !events.empty?
157
+
158
+ built = events.map do |event|
159
+ case event
160
+ when Event then event
161
+ when Hash then Event.new(**event.transform_keys(&:to_sym))
162
+ else raise ArgumentError, "Record events: each must be a Record::Event or a Hash"
163
+ end
164
+ rescue ArgumentError => error
165
+ raise error if error.message.start_with?("Record events")
166
+
167
+ raise ArgumentError, "Record events: each must have graphql_topic: and digest: only"
168
+ end
169
+ raise ArgumentError, "Record events: each digest at most once" unless built.map(&:digest).uniq.size == built.size
170
+
171
+ built.freeze
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Subscriptions
6
+ # A Rack app for the provider's webhook endpoint: hands the request to the schema's backend (`Core#webhook`),
7
+ # which verifies it through the transport and ends the subscriptions it reports gone.
8
+ #
9
+ # mount GraphQL::Armory::Subscriptions::WebhookApp.new(AppSchema), at: "/graphql/webhooks"
10
+ #
11
+ # `new` with no schema, or a schema without this backend, answers 404 to everything.
12
+ class WebhookApp
13
+ NOT_FOUND = [404, { "content-type" => "application/json" }.freeze, ["{}"].freeze].freeze
14
+
15
+ def initialize(schema = nil)
16
+ @schema = schema
17
+ freeze
18
+ end
19
+
20
+ def call(env)
21
+ backend = @schema.respond_to?(:subscriptions) ? @schema.subscriptions : nil
22
+ return [NOT_FOUND[0], NOT_FOUND[1].dup, NOT_FOUND[2].dup] unless backend.is_a?(Core)
23
+
24
+ backend.webhook(env)
25
+ end
26
+
27
+ def inspect = "#<#{self.class.name}>"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ # graphql-ruby subscriptions delivered through hosted pub/sub services (Pusher, Ably, or any transport that keeps
6
+ # the `Transport` contract), with a store that survives processes, viewer-bound channels and every delivery run
7
+ # as the subscriber, through Authorization, Versioning and PersistedQueries.
8
+ #
9
+ # This file holds the namespace and the parts every other piece builds on: errors, values, the topic digest and
10
+ # the Store, Transport and Http contracts with their Null and Substitute.
11
+ module Subscriptions
12
+ end
13
+ end
14
+ end
15
+
16
+ require "graphql/armory/subscriptions/errors"
17
+ require "graphql/armory/subscriptions/values/record"
18
+ require "graphql/armory/subscriptions/values/capabilities"
19
+ require "graphql/armory/subscriptions/values/message"
20
+ require "graphql/armory/subscriptions/values/outcome"
21
+ require "graphql/armory/subscriptions/topic"
22
+ require "graphql/armory/subscriptions/store"
23
+ require "graphql/armory/subscriptions/transport"
24
+ require "graphql/armory/subscriptions/http"
25
+ require "graphql/armory/subscriptions/controls"
26
+
27
+ require "graphql/armory/subscriptions/core"
28
+ require "graphql/armory/subscriptions/audit"
29
+ require "graphql/armory/subscriptions/webhook_app"
30
+ require "graphql/armory/subscriptions/memory_store"
31
+ require "graphql/armory/subscriptions/redis_store"
32
+ require "graphql/armory/subscriptions/pusher"
33
+ require "graphql/armory/subscriptions/ably"
@@ -54,15 +54,19 @@ module GraphQL
54
54
  end
55
55
 
56
56
  # Every sink gets the same frozen data, so one sink (or a subscriber) cannot change what the next one sees.
57
+ # Sinks run inside `Caching.neutral`: what a sink reads from the context (a request id to log) changes no answer,
58
+ # so response caching does not count it. The same key read by the app's own code still counts.
57
59
  def record(signal, context: nil, **data)
58
60
  return nil if @sinks.empty?
59
61
 
60
62
  data = data.to_h { |key, value| [key, value.is_a?(String) ? value.dup.freeze : value] }.freeze
61
63
  view = ContextView.new(context || {}) if @sinks.any? { |_sink, takes_context| takes_context }
62
- @sinks.each do |sink, takes_context|
63
- takes_context ? sink.call(signal, data, view) : sink.call(signal, data)
64
- rescue StandardError => error
65
- @on_sink_error.call(error, signal)
64
+ Caching.neutral(context) do
65
+ @sinks.each do |sink, takes_context|
66
+ takes_context ? sink.call(signal, data, view) : sink.call(signal, data)
67
+ rescue StandardError => error
68
+ @on_sink_error.call(error, signal)
69
+ end
66
70
  end
67
71
  nil
68
72
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Armory
5
- VERSION = "0.1.0.pre13"
5
+ VERSION = "0.1.0.pre15"
6
6
  end
7
7
  end
@@ -98,7 +98,7 @@ module GraphQL
98
98
 
99
99
  refused = schema_refusals(schema, findings)
100
100
  definitions = Integration.definitions(schema)
101
- members = definitions.flat_map { |type| members_of(type) }.uniq
101
+ members = definitions.flat_map { |type| Integration.members_of(type) }.uniq
102
102
  members.concat(schema.directives.values.flat_map(&:all_argument_definitions))
103
103
  undeclared(schema, members, findings)
104
104
  guarded(findings, "Schema") { OwnChecks.(schema, definitions, members, findings) }
@@ -142,14 +142,6 @@ module GraphQL
142
142
  end.positive?
143
143
  end
144
144
 
145
- def members_of(type)
146
- members = [type, *Armory::Definitions.fields_at_every_level(type), *Integration.arguments_in(type)]
147
- members.concat(type.all_enum_value_definitions) if type.kind.enum?
148
- members.concat(type.type_memberships) if type.kind.union?
149
- members.concat(type.interface_type_memberships.select { |membership| membership.respond_to?(:visible?) }) if type.respond_to?(:interface_type_memberships)
150
- members
151
- end
152
-
153
145
  # A label no version has fails the member's check whenever it is asked: reported even for a member no query asks.
154
146
  def undeclared(schema, members, findings)
155
147
  labels = schema.armory_versioning.versions.map(&:label)