graphql-stitching 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d90338dfde5c26515a55afdee7f5bd936a4563b2b8f83e1ffc1a4420c56a5cf
4
- data.tar.gz: d728c5dfabb5e0250c38a6718ac7b535cfa7a183872dc60fa95a9b91b777a64a
3
+ metadata.gz: fbeabcd8c2327504cc5ccc613942d163bddddb2087e7eb90c966861777d67b9d
4
+ data.tar.gz: cf03589e91ef96fb8dc9b74ca2703c20095e586bab45f91a89b3579376edbdac
5
5
  SHA512:
6
- metadata.gz: cc5ff912f354e08b9f204028d5e6eed85a81bf283bd5dc13bb7ee9b69b83d0bb39ba83f9fbaac66be1363e86ce3f6bd1b3f7cc4466ef83998a751e3dbccf9013
7
- data.tar.gz: 055e1c09f88e8f893a3ebb0aa3d7558156ec5c4a33b704cca69499efee4fd02e3a70e6efedeca1332f92392c7093ea8024ba9aefb159ce77b4f11510cd8d7a1c
6
+ metadata.gz: 78b5d6bbf4abccb99795c6225e1a61807ed74f94ad9ce5bdde0be534133e101c7d51f677d3c89aebaad7ebe2b125ff11a192a3d1a29cdacb3d3b51049801212f
7
+ data.tar.gz: ade024c4ace1474d9c7a7cd80fb8820fc71e456e566d89fe656d07bf4b64e60db83561546a908b5d2fcbec9d88ac740a7080946e981a74f7bd904b3da0aeebfb
data/docs/client.md CHANGED
@@ -61,16 +61,16 @@ Arguments for the `execute` method include:
61
61
  The client provides cache hooks to enable caching query plans across requests. Without caching, every request made to the client will be planned individually. With caching, a query may be planned once, cached, and then executed from cache for subsequent requests. Cache keys are a normalized digest of each query string.
62
62
 
63
63
  ```ruby
64
- client.on_cache_read do |key, _context|
64
+ client.on_cache_read do |key, _context, _request|
65
65
  $redis.get(key) # << 3P code
66
66
  end
67
67
 
68
- client.on_cache_write do |key, payload, _context|
68
+ client.on_cache_write do |key, payload, _context, _request|
69
69
  $redis.set(key, payload) # << 3P code
70
70
  end
71
71
  ```
72
72
 
73
- Note that inlined input data works against caching, so you should _avoid_ this:
73
+ Note that inlined input data works against caching, so you should _avoid_ this when possible:
74
74
 
75
75
  ```graphql
76
76
  query {
@@ -78,7 +78,7 @@ query {
78
78
  }
79
79
  ```
80
80
 
81
- Instead, always leverage variables in queries so that the document body remains consistent across requests:
81
+ Instead, leverage query variables so that the document body remains consistent across requests:
82
82
 
83
83
  ```graphql
84
84
  query($id: ID!) {
@@ -75,14 +75,14 @@ module GraphQL
75
75
 
76
76
  def fetch_plan(request)
77
77
  if @on_cache_read
78
- cached_plan = @on_cache_read.call(request.digest, request.context)
78
+ cached_plan = @on_cache_read.call(request.digest, request.context, request)
79
79
  return GraphQL::Stitching::Plan.from_json(JSON.parse(cached_plan)) if cached_plan
80
80
  end
81
81
 
82
82
  plan = yield
83
83
 
84
84
  if @on_cache_write
85
- @on_cache_write.call(request.digest, JSON.generate(plan.as_json), request.context)
85
+ @on_cache_write.call(request.digest, JSON.generate(plan.as_json), request.context, request)
86
86
  end
87
87
 
88
88
  plan
@@ -53,7 +53,7 @@ module GraphQL
53
53
  end
54
54
  end
55
55
 
56
- # "Typename" => merged_directive
56
+ # "directive_name" => merged_directive
57
57
  @schema_directives = @candidate_directives_by_name_and_location.each_with_object({}) do |(directive_name, directives_by_location), memo|
58
58
  memo[directive_name] = build_directive(directive_name, directives_by_location)
59
59
  end
@@ -88,19 +88,20 @@ module GraphQL
88
88
 
89
89
  # "Typename" => merged_type
90
90
  schema_types = @candidate_types_by_name_and_location.each_with_object({}) do |(type_name, types_by_location), memo|
91
- kinds = types_by_location.values.map { _1.kind.name }.uniq
91
+ kinds = types_by_location.values.map { _1.kind.name }.tap(&:uniq!)
92
92
 
93
93
  if kinds.length > 1
94
94
  raise ComposerError, "Cannot merge different kinds for `#{type_name}`. Found: #{kinds.join(", ")}."
95
95
  end
96
96
 
97
+ extract_boundaries(type_name, types_by_location) if type_name == @query_name
98
+
97
99
  memo[type_name] = case kinds.first
98
100
  when "SCALAR"
99
101
  build_scalar_type(type_name, types_by_location)
100
102
  when "ENUM"
101
103
  build_enum_type(type_name, types_by_location, enum_usage)
102
104
  when "OBJECT"
103
- extract_boundaries(type_name, types_by_location) if type_name == @query_name
104
105
  build_object_type(type_name, types_by_location)
105
106
  when "INTERFACE"
106
107
  build_interface_type(type_name, types_by_location)
@@ -200,7 +201,7 @@ module GraphQL
200
201
  graphql_name(directive_name)
201
202
  description(builder.merge_descriptions(directive_name, directives_by_location))
202
203
  repeatable(directives_by_location.values.any?(&:repeatable?))
203
- locations(*directives_by_location.values.flat_map(&:locations).uniq)
204
+ locations(*directives_by_location.values.flat_map(&:locations).tap(&:uniq!))
204
205
  builder.build_merged_arguments(directive_name, directives_by_location, self, directive_name: directive_name)
205
206
  end
206
207
  end
@@ -262,7 +263,7 @@ module GraphQL
262
263
  description(builder.merge_descriptions(type_name, types_by_location))
263
264
 
264
265
  interface_names = types_by_location.values.flat_map { _1.interfaces.map(&:graphql_name) }
265
- interface_names.uniq.each do |interface_name|
266
+ interface_names.tap(&:uniq!).each do |interface_name|
266
267
  implements(builder.build_type_binding(interface_name))
267
268
  end
268
269
 
@@ -280,7 +281,7 @@ module GraphQL
280
281
  description(builder.merge_descriptions(type_name, types_by_location))
281
282
 
282
283
  interface_names = types_by_location.values.flat_map { _1.interfaces.map(&:graphql_name) }
283
- interface_names.uniq.each do |interface_name|
284
+ interface_names.tap(&:uniq!).each do |interface_name|
284
285
  implements(builder.build_type_binding(interface_name))
285
286
  end
286
287
 
@@ -296,7 +297,7 @@ module GraphQL
296
297
  graphql_name(type_name)
297
298
  description(builder.merge_descriptions(type_name, types_by_location))
298
299
 
299
- possible_names = types_by_location.values.flat_map { _1.possible_types.map(&:graphql_name) }.uniq
300
+ possible_names = types_by_location.values.flat_map { _1.possible_types.map(&:graphql_name) }.tap(&:uniq!)
300
301
  possible_types(*possible_names.map { builder.build_type_binding(_1) })
301
302
  builder.build_merged_directives(type_name, types_by_location, self)
302
303
  end
@@ -160,7 +160,8 @@ module GraphQL
160
160
  errors_result.concat(pathed_errors_by_object_id.values)
161
161
  end
162
162
  end
163
- errors_result.flatten!
163
+
164
+ errors_result.tap(&:flatten!)
164
165
  end
165
166
 
166
167
  private
@@ -4,14 +4,13 @@ module GraphQL
4
4
  module Stitching
5
5
  class Request
6
6
  SUPPORTED_OPERATIONS = ["query", "mutation"].freeze
7
+ SKIP_INCLUDE_DIRECTIVE = /@(?:skip|include)/
7
8
 
8
9
  attr_reader :document, :variables, :operation_name, :context
9
10
 
10
11
  def initialize(document, operation_name: nil, variables: nil, context: nil)
11
- @may_contain_runtime_directives = true
12
-
13
12
  @document = if document.is_a?(String)
14
- @may_contain_runtime_directives = document.include?("@")
13
+ @string = document
15
14
  GraphQL.parse(document)
16
15
  else
17
16
  document
@@ -23,13 +22,21 @@ module GraphQL
23
22
  end
24
23
 
25
24
  def string
26
- @string ||= @document.to_query_string
25
+ @string || normalized_string
26
+ end
27
+
28
+ def normalized_string
29
+ @normalized_string ||= @document.to_query_string
27
30
  end
28
31
 
29
32
  def digest
30
33
  @digest ||= Digest::SHA2.hexdigest(string)
31
34
  end
32
35
 
36
+ def normalized_digest
37
+ @normalized_digest ||= Digest::SHA2.hexdigest(normalized_string)
38
+ end
39
+
33
40
  def operation
34
41
  @operation ||= begin
35
42
  operation_defs = @document.definitions.select do |d|
@@ -69,18 +76,15 @@ module GraphQL
69
76
 
70
77
  def prepare!
71
78
  operation.variables.each do |v|
72
- @variables[v.name] = v.default_value if @variables[v.name].nil?
79
+ @variables[v.name] = v.default_value if @variables[v.name].nil? && !v.default_value.nil?
73
80
  end
74
81
 
75
- if @may_contain_runtime_directives
76
- @document, modified = SkipInclude.render(@document, @variables)
77
-
78
- if modified
79
- @string = nil
80
- @digest = nil
81
- @operation = nil
82
- @variable_definitions = nil
83
- @fragment_definitions = nil
82
+ if @string.nil? || @string.match?(SKIP_INCLUDE_DIRECTIVE)
83
+ SkipInclude.render(@document, @variables) do |modified_ast|
84
+ @document = modified_ast
85
+ @string = @normalized_string = nil
86
+ @digest = @normalized_digest = nil
87
+ @operation = @operation_directives = @variable_definitions = nil
84
88
  end
85
89
  end
86
90
 
@@ -15,7 +15,11 @@ module GraphQL
15
15
  definition
16
16
  end
17
17
 
18
- return document.merge(definitions: definitions), changed
18
+ return document unless changed
19
+
20
+ document = document.merge(definitions: definitions)
21
+ yield(document) if block_given?
22
+ document
19
23
  end
20
24
 
21
25
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Stitching
5
- VERSION = "1.0.5"
5
+ VERSION = "1.0.6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-stitching
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg MacWilliam
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-19 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql