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,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ # A schema around `Query` fields the caller writes, for end-to-end examples that need resolvers of their own
8
+ # (reading or writing the context, raising, adding extensions), built fresh on every call:
9
+ #
10
+ # schema = Controls::AppSchema.example({ "op" => "query Op { greeting }" }, caching: { store: store, release: "r1" }) do |options|
11
+ # field :greeting, String, null: true, **options
12
+ # define_method(:greeting) { "hello #{context[:locale]}" }
13
+ # end
14
+ #
15
+ # `Query` is public (`authorize :none`, covering its fields, and `cache_fields ttl: 60, scope: :public`); the
16
+ # block runs in it and receives the field options `Execution::Next` needs for a field resolved by a method of
17
+ # the type. PersistedQueries is strict over `documents` (id => document). `caching:` are `use
18
+ # GraphQL::Armory::Caching` options; without it caching is not installed. `adapter:` is Authorization's
19
+ # (default: an allowing `Adapters::Substitute`); `before` runs in the schema class before the armory plugins,
20
+ # `after` after them (a `rescue_from` registered after caching, say). `Bases.example` gives the base classes, to
21
+ # build types of your own.
22
+ module AppSchema
23
+ def self.example(documents, caching: nil, adapter: nil, mutation: nil, before: nil, after: nil, &fields)
24
+ options = Schema::NEXT_FIELD_OPTIONS
25
+ query = Class.new(Bases.example.fetch(:object)) do
26
+ graphql_name "Query"
27
+ authorize :none, reason: "test root", covers_fields: true
28
+ cache_fields ttl: 60, scope: :public, reason: "the same for everyone"
29
+ instance_exec(options, &fields) if fields
30
+ end
31
+ adapter ||= Authorization::Adapters::Substitute.new(allow: true)
32
+ manifest = PersistedQueries::Manifest.new(documents, digest: false)
33
+ armory = Armory
34
+ Class.new(GraphQL::Schema) do
35
+ use GraphQL::Execution::Next if defined?(GraphQL::Execution::Next)
36
+ query(query)
37
+ mutation(mutation) if mutation
38
+ class_exec(&before) if before
39
+ use armory::Authorization, adapter: adapter
40
+ use armory::PersistedQueries, store: manifest, strict: true
41
+ use armory::Caching, **caching if caching
42
+ class_exec(&after) if after
43
+ end
44
+ end
45
+
46
+ # Base classes with Authorization's and caching's `Base`: `{ argument:, field:, object: }`.
47
+ module Bases
48
+ def self.example
49
+ auth = Authorization::Base
50
+ cache = Caching::Base
51
+ argument = Class.new(GraphQL::Schema::Argument) { include auth }
52
+ field = Class.new(GraphQL::Schema::Field) do
53
+ include auth
54
+ include cache
55
+ argument_class(argument)
56
+ end
57
+ object = Class.new(GraphQL::Schema::Object) do
58
+ include auth
59
+ include cache
60
+ field_class(field)
61
+ default_action :read
62
+ end
63
+ { argument: argument, field: field, object: object }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ # Principals of two classes with the same id: `User 7` and `AdminUser 7` must never share an entry.
8
+ module Viewer
9
+ User = Struct.new(:id)
10
+ AdminUser = Struct.new(:id)
11
+
12
+ def self.example(id: 7, admin: false)
13
+ (admin ? AdminUser : User).new(id)
14
+ end
15
+ end
16
+
17
+ # A request's context: the persisted operation to run, the viewer (`current_user`) and a locale.
18
+ module Context
19
+ def self.example(operation: "catalog", viewer: Viewer.example, locale: "en", **extra)
20
+ { operation_id: operation, current_user: viewer, locale: locale }.merge(extra).compact
21
+ end
22
+
23
+ def self.anonymous(operation: "catalog", locale: "en", **extra)
24
+ example(operation: operation, viewer: nil, locale: locale, **extra)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ # The persisted operations of `Controls::Schema`, by id: one cacheable `:public`, one cacheable `:viewer`, one
8
+ # with a declared variable, and one per refusal the walk or the gate must answer.
9
+ module Operations
10
+ ALL = {
11
+ # Cacheable, :public, TTL 60 (Product.price narrows the catalog's 300), tags catalog and pricing.
12
+ "catalog" => "query Catalog { products { name price { amount currency } } }",
13
+ # Cacheable, :public, TTL 300.
14
+ "names" => "query Names { products { name } }",
15
+ # Cacheable, :viewer, TTL 30: invoices go through an Authorization rule.
16
+ "invoices" => "query Invoices { invoices { number total } }",
17
+ # Cacheable, :public, with a declared variable.
18
+ "product" => "query Product($name: String!) { product(name: $name) { name } }",
19
+ # Refused by the walk: stockLevel is `cache: false`.
20
+ "stock" => "query Stock { products { name stockLevel } }",
21
+ # Refused: introspection.
22
+ "introspection" => "query Introspection { __schema { queryType { name } } }",
23
+ # Refused: a mutation.
24
+ "touch" => "mutation Touch { touch }"
25
+ }.freeze
26
+
27
+ def self.document(id) = ALL.fetch(id)
28
+
29
+ # A PersistedQueries manifest holding every operation above (ids are not digests).
30
+ def self.manifest
31
+ PersistedQueries::Manifest.new(ALL, digest: false)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ # The application objects `Controls::Schema` resolves.
8
+ module Records
9
+ Product = Struct.new(:name, :price, :stock_level)
10
+ Money = Struct.new(:amount, :currency)
11
+ Invoice = Struct.new(:number, :total)
12
+
13
+ def self.products
14
+ [Product.new("Lamp", Money.new(1200, "EUR"), 3), Product.new("Desk", Money.new(30_000, "EUR"), 0)]
15
+ end
16
+
17
+ def self.invoices
18
+ [Invoice.new("A-1", 1200), Invoice.new("A-2", 30_000)]
19
+ end
20
+ end
21
+
22
+ # A minimal schema for the caching specs, built fresh on every call:
23
+ #
24
+ # - `Product` (public catalog, `authorize :none`, `cache_fields ttl: 300, scope: :public, tags: ["catalog"]`) with
25
+ # `price` narrowed to TTL 60 and tag "pricing", and `stockLevel` declared `cache: false`;
26
+ # - `Money` (`cache_fields ttl: 60, scope: :public`);
27
+ # - `Invoice` (`cache_fields ttl: 30`, scope `:viewer`) behind an Authorization rule;
28
+ # - `Query` (`products`, `product(name:)`, `invoices`) and `Mutation` (`touch`);
29
+ # - PersistedQueries with `Operations.manifest`, strict.
30
+ #
31
+ # `authorization: false` leaves Authorization out; `adapter:` is its adapter (default: an allowing
32
+ # `Authorization::Adapters::Substitute`). `next_default: true` makes `Execution::Next` the default engine.
33
+ # `caching:` are `use GraphQL::Armory::Caching` options; without it caching is not installed (units that test
34
+ # declarations and the walk do not need it).
35
+ module Schema
36
+ def self.example(authorization: true, adapter: nil, next_default: false, caching: nil, sinks: [])
37
+ auth = authorization
38
+ armory_auth = Authorization
39
+ caching_base = Caching::Base
40
+ legacy = NEXT_FIELD_OPTIONS
41
+
42
+ base_field = Class.new(GraphQL::Schema::Field) do
43
+ include armory_auth::Base if auth
44
+ include caching_base
45
+ end
46
+ base_object = Class.new(GraphQL::Schema::Object) do
47
+ include armory_auth::Base if auth
48
+ include caching_base
49
+ field_class base_field
50
+ default_action :read if auth
51
+ end
52
+
53
+ money = Class.new(base_object) do
54
+ graphql_name "Money"
55
+ authorize :none, reason: "a price is public with its product", covers_fields: true if auth
56
+ cache_fields ttl: 60, scope: :public, reason: "prices are the same for every viewer and tenant"
57
+ field :amount, Integer, null: false
58
+ field :currency, String, null: false
59
+ end
60
+
61
+ product = Class.new(base_object) do
62
+ graphql_name "Product"
63
+ authorize :none, reason: "public catalog", covers_fields: true if auth
64
+ cache_fields ttl: 300, scope: :public, tags: ["catalog"], reason: "the catalog is the same for every viewer and tenant"
65
+ field :name, String, null: false
66
+ field :price, money, null: false, cache: { ttl: 60, tags: ["pricing"] }
67
+ field :stock_level, Integer, null: false, cache: false
68
+ end
69
+
70
+ invoice = Class.new(base_object) do
71
+ graphql_name "Invoice"
72
+ cache_fields ttl: 30
73
+ field :number, String, null: false
74
+ field :total, Integer, null: false
75
+ end
76
+
77
+ root_rule = auth ? { authorize: :none, reason: "root field; its type decides" } : {}
78
+ query = Class.new(base_object) do
79
+ graphql_name "Query"
80
+ authorize :none, reason: "root type; each field declares its own rule" if auth
81
+ cache_fields ttl: 300, scope: :public, reason: "root fields: the types they return decide"
82
+ field :products, [product], null: false, **root_rule, **legacy
83
+ field :product, product, **root_rule, **legacy do
84
+ argument :name, String
85
+ end
86
+ field :invoices, [invoice], null: false, cache: { ttl: 30, scope: :viewer }, **root_rule, **legacy
87
+
88
+ define_method(:products) { Records.products }
89
+ define_method(:product) { |name:| Records.products.find { |item| item.name == name } }
90
+ define_method(:invoices) { Records.invoices }
91
+ end
92
+
93
+ mutation = Class.new(base_object) do
94
+ graphql_name "Mutation"
95
+ authorize :none, reason: "test mutation" if auth
96
+ field :touch, GraphQL::Types::Boolean, null: false, **root_rule, **legacy
97
+ define_method(:touch) { true }
98
+ end
99
+
100
+ adapter ||= Authorization::Adapters::Substitute.new(allow: true) if auth
101
+ manifest = Operations.manifest
102
+ Class.new(GraphQL::Schema) do
103
+ use GraphQL::Execution::Next, **(next_default ? { as_default: true } : {}) if defined?(GraphQL::Execution::Next)
104
+ query(query)
105
+ mutation(mutation)
106
+ use armory_auth, adapter: adapter, sinks: sinks if auth
107
+ use PersistedQueries, store: manifest, strict: true, sinks: sinks
108
+ use Caching, **caching if caching
109
+ end
110
+ end
111
+
112
+ # Root fields resolve with the type's instance methods; Execution::Next needs to be told so.
113
+ NEXT_FIELD_OPTIONS =
114
+ if GraphQL::Schema::Field.instance_method(:initialize).parameters.any? { |_, name| name == :resolve_legacy_instance_method }
115
+ { resolve_legacy_instance_method: true }.freeze
116
+ else
117
+ {}.freeze
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ module Store
8
+ # A `Store::Substitute` on a fixed clock.
9
+ def self.example(now: 1_000_000)
10
+ Caching::Store::Substitute.new(now: now)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ module Controls
7
+ # The shapes the walk must judge, built fresh on every call (no Authorization, no PersistedQueries: the walk
8
+ # reads only declarations and `query.types`):
9
+ #
10
+ # - `Node` (interface, `cache_fields ttl: 300, scope: :public`) with `id`, `label` and `summary`, implemented
11
+ # by `Product` (overrides `label` narrower: TTL 10, tag "labels"), `Invoice` (`cache_fields ttl: 30`, scope
12
+ # `:viewer`, which reaches the `label` it redefines but not the `id` and `summary` it inherits) and `Secret`
13
+ # (overrides `summary` with `cache: false`; its own `code` is undeclared);
14
+ # - `SearchResult`, a union of `Product` and `Invoice`;
15
+ # - `Parent` (no `cache_fields`) with `secret`, and `Child < Parent` with a wider `cache_fields ttl: 3600,
16
+ # scope: :public` and its own `name`: the inherited `secret` stays undeclared;
17
+ # - Relay connections: `Query.products` (graphql-ruby's generated connection and edge, on the app's own base
18
+ # connection), `Query.counted` (adds `totalCount`, undeclared) and `Query.curated` (overrides `nodes` with
19
+ # its own resolver method, so `nodes` needs a declaration);
20
+ # - `Query.internalNote`, hidden when the context holds `hide_internal_note: true` (a member `query.types`
21
+ # does not show);
22
+ # - directives `@hint` (`cache_safe`) and `@flagged` (not), on every executable location;
23
+ # - `Mutation.touch` and `Subscription.ticked`.
24
+ #
25
+ # `partial: true` adds `Query.partial`, whose `Partial.code` declares `cache: { tags: ["x"] }` on a type with
26
+ # no `cache_fields`: a definition error that `armory_cache` raises when the walk reaches it. `replaced_skip: true`
27
+ # registers an app directive class named `skip` (a subclass of graphql-ruby's) in place of the built-in one.
28
+ # `next_default: true` makes `Execution::Next` the default engine.
29
+ module WalkSchema
30
+ module Records
31
+ Product = Struct.new(:id, :label, :name)
32
+ Invoice = Struct.new(:id, :label, :number)
33
+
34
+ def self.products
35
+ [Product.new("P1", "lamp", "Lamp"), Product.new("P2", "desk", "Desk")]
36
+ end
37
+ end
38
+
39
+ LOCATIONS = %i[QUERY FIELD FRAGMENT_DEFINITION FRAGMENT_SPREAD INLINE_FRAGMENT VARIABLE_DEFINITION].freeze
40
+
41
+ def self.example(next_default: false, partial: false, replaced_skip: false)
42
+ caching_base = Caching::Base
43
+ legacy = Schema::NEXT_FIELD_OPTIONS
44
+ records = Records
45
+ locations = LOCATIONS.map { |name| GraphQL::Schema::Directive.const_get(name) }
46
+
47
+ base_field = Class.new(GraphQL::Schema::Field) { include caching_base }
48
+ base_object = Class.new(GraphQL::Schema::Object) do
49
+ include caching_base
50
+ field_class base_field
51
+ end
52
+ base_edge = Class.new(base_object) { include GraphQL::Types::Relay::EdgeBehaviors }
53
+ base_connection = Class.new(base_object) { include GraphQL::Types::Relay::ConnectionBehaviors }
54
+ base_object.edge_type_class(base_edge)
55
+ base_object.connection_type_class(base_connection)
56
+
57
+ node = Module.new do
58
+ include GraphQL::Schema::Interface
59
+ include caching_base
60
+ graphql_name "Node"
61
+ field_class base_field
62
+ cache_fields ttl: 300, scope: :public, reason: "ids and labels are the same for every viewer"
63
+ field :id, GraphQL::Types::ID, null: false
64
+ field :label, String, null: false
65
+ field :summary, String, null: false
66
+ end
67
+
68
+ product = Class.new(base_object) do
69
+ graphql_name "Product"
70
+ implements node
71
+ cache_fields ttl: 300, scope: :public, tags: ["catalog"], reason: "the catalog is the same for every viewer"
72
+ field :name, String, null: false
73
+ field :label, String, null: false, cache: { ttl: 10, tags: ["labels"] }
74
+ end
75
+
76
+ invoice = Class.new(base_object) do
77
+ graphql_name "Invoice"
78
+ implements node
79
+ cache_fields ttl: 30
80
+ field :number, String, null: false
81
+ field :label, String, null: false
82
+ end
83
+
84
+ secret = Class.new(base_object) do
85
+ graphql_name "Secret"
86
+ implements node
87
+ field :summary, String, null: false, cache: false
88
+ field :code, String, null: false
89
+ end
90
+
91
+ search_result = Class.new(GraphQL::Schema::Union) do
92
+ graphql_name "SearchResult"
93
+ possible_types product, invoice
94
+ end
95
+
96
+ parent = Class.new(base_object) do
97
+ graphql_name "Parent"
98
+ field :secret, String, null: false
99
+ end
100
+ child = Class.new(parent) do
101
+ graphql_name "Child"
102
+ cache_fields ttl: 3600, scope: :public, reason: "the child's own fields are public"
103
+ field :name, String, null: false
104
+ end
105
+
106
+ counted = Class.new(base_connection) do
107
+ graphql_name "CountedProductConnection"
108
+ edge_type product.edge_type
109
+ field :total_count, Integer, null: false
110
+ end
111
+
112
+ curated = Class.new(base_connection) do
113
+ graphql_name "CuratedProductConnection"
114
+ edge_type product.edge_type, nodes_field: false
115
+ field :nodes, [product], null: false
116
+ define_method(:nodes) { records.products.reverse }
117
+ end
118
+
119
+ partial_type = if partial
120
+ Class.new(base_object) do
121
+ graphql_name "Partial"
122
+ field :code, String, null: false, cache: { tags: ["x"] }
123
+ end
124
+ end
125
+
126
+ query = Class.new(base_object) do
127
+ graphql_name "Query"
128
+ cache_fields ttl: 300, scope: :public, reason: "root fields: the types they return decide"
129
+ field :node, node, **legacy do
130
+ argument :id, GraphQL::Types::ID
131
+ end
132
+ field :nodes, [node], null: false, **legacy
133
+ field :search, [search_result], null: false, **legacy
134
+ field :child, child, **legacy
135
+ field :products, product.connection_type, null: false, **legacy
136
+ field :counted, counted, null: false, **legacy
137
+ field :curated, curated, null: false, **legacy
138
+ field :invoices, [invoice], null: false, cache: { ttl: 30, scope: :viewer }, **legacy
139
+ field :internal_note, String, **legacy do
140
+ define_singleton_method(:visible?) { |context| super(context) && !context[:hide_internal_note] }
141
+ end
142
+ field :partial, partial_type, **legacy if partial_type
143
+
144
+ define_method(:node) { |id:| records.products.find { |item| item.id == id } }
145
+ define_method(:nodes) { records.products }
146
+ define_method(:search) { records.products }
147
+ define_method(:child) { nil }
148
+ define_method(:products) { records.products }
149
+ define_method(:counted) { records.products }
150
+ define_method(:curated) { records.products }
151
+ define_method(:invoices) { [] }
152
+ define_method(:internal_note) { "note" }
153
+ define_method(:partial) { nil }
154
+ end
155
+
156
+ mutation = Class.new(base_object) do
157
+ graphql_name "Mutation"
158
+ field :touch, GraphQL::Types::Boolean, null: false, **legacy
159
+ define_method(:touch) { true }
160
+ end
161
+
162
+ subscription = Class.new(base_object) do
163
+ graphql_name "Subscription"
164
+ field :ticked, GraphQL::Types::Boolean, null: false, **legacy
165
+ end
166
+
167
+ hint = Class.new(GraphQL::Schema::Directive) do
168
+ include caching_base
169
+ graphql_name "hint"
170
+ locations(*locations)
171
+ cache_safe reason: "a client hint; it changes nothing in the answer"
172
+ end
173
+ flagged = Class.new(GraphQL::Schema::Directive) do
174
+ include caching_base
175
+ graphql_name "flagged"
176
+ locations(*locations)
177
+ end
178
+
179
+ # An app class that takes over the name `skip`: it is not graphql-ruby's, so the walk does not ignore it.
180
+ own_skip = (Class.new(GraphQL::Schema::Directive::Skip) { graphql_name "skip" } if replaced_skip)
181
+
182
+ product_type = product
183
+ Class.new(GraphQL::Schema) do
184
+ use GraphQL::Execution::Next, **(next_default ? { as_default: true } : {}) if defined?(GraphQL::Execution::Next)
185
+ query(query)
186
+ mutation(mutation)
187
+ subscription(subscription)
188
+ orphan_types(invoice, secret)
189
+ directives(hint, flagged, *own_skip)
190
+ define_singleton_method(:resolve_type) { |_abstract, _object, _context| product_type }
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ # Test data for response caching, reusable by apps and other gems. Units that need more shapes (interfaces,
7
+ # unions, connections, directives) add their own files under `caching/controls/`.
8
+ module Controls
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ require "graphql/armory/caching/controls/operations"
15
+ require "graphql/armory/caching/controls/context"
16
+ require "graphql/armory/caching/controls/store"
17
+ require "graphql/armory/caching/controls/schema"
18
+ require "graphql/armory/caching/controls/walk_schema"
19
+ require "graphql/armory/caching/controls/app_schema"
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "securerandom"
5
+
6
+ module GraphQL
7
+ module Armory
8
+ module Caching
9
+ # Keyed digests (HMAC-SHA256, hex) for store keys and tags, so whoever reads the store cannot confirm a guess
10
+ # such as `viewer:User:7`.
11
+ #
12
+ # Digester.new # a random key for this process: entries are not shared across processes
13
+ # Digester.build(encoder: AppEncoder) # the encoder's `mac(bytes, purpose: "cache-digest")`, shared by every process
14
+ # digester.("viewer:User:7") # => "9f86d0..." (64 hex characters)
15
+ class Digester
16
+ PURPOSE = "cache-digest"
17
+
18
+ # `encoder.mac(bytes, purpose:)` answers the raw 32-byte HMAC (an Encoder).
19
+ def self.build(encoder:)
20
+ raise ConfigurationError, "encoder: must answer mac(bytes, purpose:) (an Encoder subclass)" unless encoder.respond_to?(:mac)
21
+
22
+ digester = new(mac: ->(bytes) { encoder.mac(bytes, purpose: PURPOSE) })
23
+ # Once at boot: an encoder that cannot digest (Legacy, Null, no key) fails here, not on the first request.
24
+ digester.("")
25
+ digester
26
+ end
27
+
28
+ def initialize(mac: nil)
29
+ raise ConfigurationError, "mac: must be a callable taking the bytes" unless mac.nil? || mac.respond_to?(:call)
30
+
31
+ key = SecureRandom.random_bytes(32)
32
+ @mac = mac || ->(bytes) { OpenSSL::HMAC.digest("SHA256", key, bytes) }
33
+ end
34
+
35
+ def call(string)
36
+ raise ArgumentError, "a Digester digests Strings, got #{string.class}" unless string.is_a?(String)
37
+
38
+ raw = @mac.call(string.b)
39
+ raise ConfigurationError, "the mac answered #{raw.class}, not 32 bytes" unless raw.is_a?(String) && raw.bytesize == 32
40
+
41
+ raw.unpack1("H*")
42
+ end
43
+
44
+ def inspect = "#<#{self.class.name}>"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ class Error < Armory::Error; end
7
+
8
+ # A declaration, a `use` option or the schema is not set up the way caching needs. Raised at definition, at boot,
9
+ # or on the first request for what can only be checked then (PersistedQueries installed, a viewer's id).
10
+ class ConfigurationError < Error; end
11
+
12
+ # The store failed during a purge. The app retries: a purge that silently failed serves stale data until the TTL.
13
+ class PurgeError < Error; end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ class Gate
7
+ # The rows of "Never cached" that are known before a query runs (docs/response-caching.md "What is cached and when"). A query that
8
+ # matches one is never looked up: it runs uncached, with the row's reason.
9
+ module Admission
10
+ # Per-request options that make a query uncacheable. `max_depth:` and `max_complexity:` are key components.
11
+ REQUEST_OPTIONS = %i[static_validator validate warden use_visibility_profile root_value].freeze
12
+
13
+ # Context keys whose presence alone makes a request uncacheable: a full-schema request sees members hidden
14
+ # from everyone else, and an ability object is something the key cannot identify. Their absence is checked
15
+ # here, so a later read of either is covered.
16
+ ABSENT_KEYS = { armory_full_schema: :full_schema, current_ability: :current_ability }.freeze
17
+
18
+ module_function
19
+
20
+ def mutation?(query)
21
+ query.respond_to?(:mutation?) && query.mutation? == true
22
+ end
23
+
24
+ # The reason `query` must run uncached, or nil when it may be looked up. `mutation_in_call` is whether any
25
+ # query of the same call is a mutation: its writes may be meant for the others.
26
+ def refusal(query, mutation_in_call:)
27
+ return :built_query unless guard_built?(query)
28
+ return :mutation if mutation?(query)
29
+ return :multiplex_mutation if mutation_in_call
30
+ return :subscription if query.subscription?
31
+ # PersistedQueries and Versioning refuse through static_validator; an app-supplied one is refused too.
32
+ return :refused if query.static_validator
33
+ return :options if options?(query)
34
+ return :not_persisted unless RecordKeys.persisted?(query.context)
35
+
36
+ present_key(query.context)
37
+ end
38
+
39
+ # Only queries the guard built from the request's options are cached: never one the app built itself, nor a
40
+ # `Query::Partial`, whose context the guard did not wrap.
41
+ def guard_built?(query)
42
+ query.is_a?(GraphQL::Query) && RecordKeys.guard_built?(query.context)
43
+ end
44
+
45
+ # What graphql-ruby keeps of the per-request options, read through its public readers.
46
+ def options?(query)
47
+ return true unless query.root_value.nil?
48
+ return true if query.validate == false
49
+
50
+ profiles = query.schema.respond_to?(:use_visibility_profile?) && query.schema.use_visibility_profile?
51
+ profiled = query.respond_to?(:visibility_profile) && !query.visibility_profile.nil?
52
+ # `warden:` or `use_visibility_profile:` changed how this query sees the schema.
53
+ profiles != profiled
54
+ end
55
+
56
+ # Checked inside `Caching.neutral`: the check itself is not one of the run's reads.
57
+ def present_key(context)
58
+ Caching.neutral(context) do
59
+ ABSENT_KEYS.each { |key, reason| return reason if context.key?(key) }
60
+ nil
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end