elasticgraph-query_interceptor 0.18.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fcd371ef8748406bce58e08d51a6290db092b9bf3e85fdf37d060e8a1976d918
4
+ data.tar.gz: e4f38da27953ca3a88a9781ee94762aa10d5dcfa87c40219e5ed58a886ae02c6
5
+ SHA512:
6
+ metadata.gz: a9ee19636384e1e334e51641815535bec5aee0094cfc0e89f45e42e6536bb5569e26bd0a4b4aeebe5dc0396d5ffe48dc23b41d6b0eef485f9fbd8ab1046b3edf
7
+ data.tar.gz: 2527e2aa119caee3ceab406a140df68786964a890c0ff45fcffae0593607a3e3d6181166fed4047d678f6fb2c76def99a3cb7b41285cde87b67676594f5c7c85
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Block, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # ElasticGraph::QueryInterceptor
2
+
3
+ An ElasticGraph extension library that lets you intercept datastore
4
+ queries before they are submitted in order to customize/modify them some
5
+ how.
6
+
7
+ ## Setup
8
+
9
+ First, add `elasticgraph-query_interceptor` to your `Gemfile`:
10
+
11
+ ``` ruby
12
+ gem "elasticgraph-query_interceptor"
13
+ ```
14
+
15
+ Next, configure this library in your ElasticGraph config YAML files.
16
+ An optional "config" dictionary can be provided to pass in values to
17
+ your interceptor when it is initialized.
18
+
19
+ ``` yaml
20
+ extension_modules:
21
+ - require_path: elastic_graph/query_interceptor/graphql_extension
22
+ extension_name: ElasticGraph::QueryInterceptor::GraphQLExtension
23
+ query_interceptor:
24
+ interceptors:
25
+ - require_path: ./my_app/example_interceptor
26
+ extension_name: MyApp::ExampleInterceptor
27
+ config: # Optional
28
+ foo: bar
29
+ ```
30
+
31
+ Define your interceptors at the configured paths. Each interceptor must
32
+ implement this interface:
33
+
34
+ ``` ruby
35
+ module YourApp
36
+ class ExampleInterceptor
37
+ def initialize(elasticgraph_graphql:, config:)
38
+ # elasticgraph_graphql is the `ElasticGraph::GraphQL` instance and has access
39
+ # to things like the datastore client in case you need it in your interceptor.
40
+ end
41
+
42
+ def intercept(query, field:, args:, http_request:, context:)
43
+ # Call `query.merge_with(...)` as desired to merge in query overrides like filters.
44
+ # This method must return a query.
45
+ end
46
+ end
47
+ end
48
+ ```
@@ -0,0 +1,20 @@
1
+ # Copyright 2024 Block, Inc.
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+ #
7
+ # frozen_string_literal: true
8
+
9
+ require_relative "../gemspec_helper"
10
+
11
+ ElasticGraphGemspecHelper.define_elasticgraph_gem(gemspec_file: __FILE__, category: :extension) do |spec, eg_version|
12
+ spec.summary = "An ElasticGraph extension for intercepting datastore queries."
13
+
14
+ spec.add_dependency "elasticgraph-graphql", eg_version
15
+ spec.add_dependency "elasticgraph-schema_artifacts", eg_version
16
+
17
+ spec.add_development_dependency "elasticgraph-elasticsearch", eg_version
18
+ spec.add_development_dependency "elasticgraph-opensearch", eg_version
19
+ spec.add_development_dependency "elasticgraph-schema_definition", eg_version
20
+ end
@@ -0,0 +1,61 @@
1
+ # Copyright 2024 Block, Inc.
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+ #
7
+ # frozen_string_literal: true
8
+
9
+ require "elastic_graph/schema_artifacts/runtime_metadata/extension_loader"
10
+
11
+ module ElasticGraph
12
+ module QueryInterceptor
13
+ # Defines configuration for elasticgraph-query_interceptor
14
+ class Config < ::Data.define(:interceptors)
15
+ # Builds Config from parsed YAML config.
16
+ def self.from_parsed_yaml(parsed_config_hash, parsed_runtime_metadata_hashes: [])
17
+ interceptor_hashes = parsed_runtime_metadata_hashes.flat_map { |h| h["interceptors"] || [] }
18
+
19
+ if (extension_config = parsed_config_hash["query_interceptor"])
20
+ extra_keys = extension_config.keys - EXPECTED_KEYS
21
+
22
+ unless extra_keys.empty?
23
+ raise ConfigError, "Unknown `query_interceptor` config settings: #{extra_keys.join(", ")}"
24
+ end
25
+
26
+ interceptor_hashes += extension_config.fetch("interceptors")
27
+ end
28
+
29
+ loader = SchemaArtifacts::RuntimeMetadata::ExtensionLoader.new(InterceptorInterface)
30
+
31
+ interceptors = interceptor_hashes.map do |hash|
32
+ empty_config = {} # : ::Hash[::Symbol, untyped]
33
+ ext = loader.load(hash.fetch("extension_name"), from: hash.fetch("require_path"), config: empty_config)
34
+ config = hash["config"] || {} # : ::Hash[::String, untyped]
35
+ InterceptorData.new(klass: ext.extension_class, config: config)
36
+ end
37
+
38
+ new(interceptors)
39
+ end
40
+
41
+ DEFAULT = new([])
42
+ EXPECTED_KEYS = members.map(&:to_s)
43
+
44
+ # Defines a data structure to hold interceptor klass and config
45
+ InterceptorData = ::Data.define(:klass, :config)
46
+
47
+ # Defines the interceptor interface, which our extension loader will validate against.
48
+ class InterceptorInterface
49
+ def initialize(elasticgraph_graphql:, config:)
50
+ # must be defined, but nothing to do
51
+ end
52
+
53
+ def intercept(query, field:, args:, http_request:, context:)
54
+ # :nocov: -- must return a query to satisfy Steep type checking but never called
55
+ query
56
+ # :nocov:
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,28 @@
1
+ # Copyright 2024 Block, Inc.
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+ #
7
+ # frozen_string_literal: true
8
+
9
+ module ElasticGraph
10
+ module QueryInterceptor
11
+ class DatastoreQueryAdapter
12
+ # @dynamic interceptors
13
+ attr_reader :interceptors
14
+
15
+ def initialize(interceptors)
16
+ @interceptors = interceptors
17
+ end
18
+
19
+ def call(query:, args:, lookahead:, field:, context:)
20
+ http_request = context[:http_request]
21
+
22
+ interceptors.reduce(query) do |accum, interceptor|
23
+ interceptor.intercept(accum, field: field, args: args, http_request: http_request, context: context)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright 2024 Block, Inc.
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+ #
7
+ # frozen_string_literal: true
8
+
9
+ require "elastic_graph/query_interceptor/config"
10
+ require "elastic_graph/query_interceptor/datastore_query_adapter"
11
+ require "elastic_graph/support/hash_util"
12
+
13
+ module ElasticGraph
14
+ module QueryInterceptor
15
+ module GraphQLExtension
16
+ def datastore_query_adapters
17
+ @datastore_query_adapters ||= begin
18
+ runtime_metadata_configs = runtime_metadata.graphql_extension_modules.filter_map do |ext_mod|
19
+ Support::HashUtil.stringify_keys(ext_mod.extension_config) if ext_mod.extension_class == GraphQLExtension
20
+ end
21
+
22
+ interceptors = Config
23
+ .from_parsed_yaml(config.extension_settings, parsed_runtime_metadata_hashes: runtime_metadata_configs)
24
+ .interceptors
25
+ .map { |data| data.klass.new(elasticgraph_graphql: self, config: data.config) }
26
+
27
+ super + [DatastoreQueryAdapter.new(interceptors)]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,286 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticgraph-query_interceptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.18.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Myron Marston
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop-factory_bot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.26'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.26'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.39.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.39.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: steep
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coderay
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: flatware-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.3.2
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '3.0'
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.3.2
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.13'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '3.13'
131
+ - !ruby/object:Gem::Dependency
132
+ name: super_diff
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 0.12.1
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 0.12.1
145
+ - !ruby/object:Gem::Dependency
146
+ name: simplecov
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '0.22'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '0.22'
159
+ - !ruby/object:Gem::Dependency
160
+ name: simplecov-console
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: 0.9.1
166
+ - - "<"
167
+ - !ruby/object:Gem::Version
168
+ version: '1.0'
169
+ type: :development
170
+ prerelease: false
171
+ version_requirements: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 0.9.1
176
+ - - "<"
177
+ - !ruby/object:Gem::Version
178
+ version: '1.0'
179
+ - !ruby/object:Gem::Dependency
180
+ name: elasticgraph-graphql
181
+ requirement: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - '='
184
+ - !ruby/object:Gem::Version
185
+ version: 0.18.0.0
186
+ type: :runtime
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - '='
191
+ - !ruby/object:Gem::Version
192
+ version: 0.18.0.0
193
+ - !ruby/object:Gem::Dependency
194
+ name: elasticgraph-schema_artifacts
195
+ requirement: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - '='
198
+ - !ruby/object:Gem::Version
199
+ version: 0.18.0.0
200
+ type: :runtime
201
+ prerelease: false
202
+ version_requirements: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - '='
205
+ - !ruby/object:Gem::Version
206
+ version: 0.18.0.0
207
+ - !ruby/object:Gem::Dependency
208
+ name: elasticgraph-elasticsearch
209
+ requirement: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - '='
212
+ - !ruby/object:Gem::Version
213
+ version: 0.18.0.0
214
+ type: :development
215
+ prerelease: false
216
+ version_requirements: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - '='
219
+ - !ruby/object:Gem::Version
220
+ version: 0.18.0.0
221
+ - !ruby/object:Gem::Dependency
222
+ name: elasticgraph-opensearch
223
+ requirement: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - '='
226
+ - !ruby/object:Gem::Version
227
+ version: 0.18.0.0
228
+ type: :development
229
+ prerelease: false
230
+ version_requirements: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - '='
233
+ - !ruby/object:Gem::Version
234
+ version: 0.18.0.0
235
+ - !ruby/object:Gem::Dependency
236
+ name: elasticgraph-schema_definition
237
+ requirement: !ruby/object:Gem::Requirement
238
+ requirements:
239
+ - - '='
240
+ - !ruby/object:Gem::Version
241
+ version: 0.18.0.0
242
+ type: :development
243
+ prerelease: false
244
+ version_requirements: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - '='
247
+ - !ruby/object:Gem::Version
248
+ version: 0.18.0.0
249
+ description:
250
+ email:
251
+ - myron@squareup.com
252
+ executables: []
253
+ extensions: []
254
+ extra_rdoc_files: []
255
+ files:
256
+ - LICENSE.txt
257
+ - README.md
258
+ - elasticgraph-query_interceptor.gemspec
259
+ - lib/elastic_graph/query_interceptor/config.rb
260
+ - lib/elastic_graph/query_interceptor/datastore_query_adapter.rb
261
+ - lib/elastic_graph/query_interceptor/graphql_extension.rb
262
+ homepage:
263
+ licenses:
264
+ - MIT
265
+ metadata:
266
+ gem_category: extension
267
+ post_install_message:
268
+ rdoc_options: []
269
+ require_paths:
270
+ - lib
271
+ required_ruby_version: !ruby/object:Gem::Requirement
272
+ requirements:
273
+ - - "~>"
274
+ - !ruby/object:Gem::Version
275
+ version: '3.2'
276
+ required_rubygems_version: !ruby/object:Gem::Requirement
277
+ requirements:
278
+ - - ">="
279
+ - !ruby/object:Gem::Version
280
+ version: '0'
281
+ requirements: []
282
+ rubygems_version: 3.5.9
283
+ signing_key:
284
+ specification_version: 4
285
+ summary: An ElasticGraph extension for intercepting datastore queries.
286
+ test_files: []