Skip to content

Commit d314505

Browse files
authored
Merge pull request #4434 from rmosolgo/consolidate-empty-objects
Consolidate empty value constants
2 parents d9cf7d4 + df218db commit d314505

File tree

21 files changed

+53
-66
lines changed

21 files changed

+53
-66
lines changed

lib/graphql.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def self.scan_with_ruby(graphql_string)
6969

7070
NOT_CONFIGURED = Object.new
7171
private_constant :NOT_CONFIGURED
72+
module EmptyObjects
73+
EMPTY_HASH = {}.freeze
74+
EMPTY_ARRAY = [].freeze
75+
end
7276
end
7377

7478
# Order matters for these:

lib/graphql/execution/interpreter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Interpreter
1414
class << self
1515
# Used internally to signal that the query shouldn't be executed
1616
# @api private
17-
NO_OPERATION = {}.freeze
17+
NO_OPERATION = GraphQL::EmptyObjects::EMPTY_HASH
1818

1919
# @param schema [GraphQL::Schema]
2020
# @param queries [Array<GraphQL::Query, Hash>]

lib/graphql/execution/interpreter/arguments.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def merge_extras(extra_args)
8080
)
8181
end
8282

83-
NO_ARGS = {}.freeze
83+
NO_ARGS = GraphQL::EmptyObjects::EMPTY_HASH
8484
EMPTY = self.new(argument_values: nil, keyword_arguments: NO_ARGS).freeze
8585
end
8686
end

lib/graphql/execution/interpreter/arguments_cache.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ def dataload_for(ast_node, argument_owner, parent_object, &block)
5555

5656
private
5757

58-
NO_ARGUMENTS = {}.freeze
59-
60-
NO_VALUE_GIVEN = Object.new
58+
NO_ARGUMENTS = GraphQL::EmptyObjects::EMPTY_HASH
59+
NO_VALUE_GIVEN = NOT_CONFIGURED
6160

6261
def self.prepare_args_hash(query, ast_arg_or_hash_or_value)
6362
case ast_arg_or_hash_or_value

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def gather_selections(owner_object, owner_type, selections, selections_to_run =
379379
selections_to_run || selections_by_name
380380
end
381381

382-
NO_ARGS = {}.freeze
382+
NO_ARGS = GraphQL::EmptyObjects::EMPTY_HASH
383383

384384
# @return [void]
385385
def evaluate_selections(owner_object, owner_type, is_eager_selection, gathered_selections, selections_result, target_result, parent_object) # rubocop:disable Metrics/ParameterLists

lib/graphql/language/nodes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module GraphQL
33
module Language
44
module Nodes
5-
NONE = [].freeze
5+
NONE = GraphQL::EmptyObjects::EMPTY_ARRAY
66
# {AbstractNode} is the base class for all nodes in a GraphQL AST.
77
#
88
# It provides some APIs for working with ASTs:
@@ -51,7 +51,7 @@ def ==(other)
5151
other.children == self.children
5252
end
5353

54-
NO_CHILDREN = [].freeze
54+
NO_CHILDREN = GraphQL::EmptyObjects::EMPTY_ARRAY
5555

5656
# @return [Array<GraphQL::Language::Nodes::AbstractNode>] all nodes in the tree below this one
5757
def children

lib/graphql/pagination/connection.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def after
5858
# @param arguments [Hash] The arguments to the field that returned the collection wrapped by this connection
5959
# @param max_page_size [Integer, nil] A configured value to cap the result size. Applied as `first` if neither first or last are given and no `default_page_size` is set.
6060
# @param default_page_size [Integer, nil] A configured value to determine the result size when neither first or last are given.
61-
def initialize(items, parent: nil, field: nil, context: nil, first: nil, after: nil, max_page_size: :not_given, default_page_size: :not_given, last: nil, before: nil, edge_class: nil, arguments: nil)
61+
def initialize(items, parent: nil, field: nil, context: nil, first: nil, after: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, last: nil, before: nil, edge_class: nil, arguments: nil)
6262
@items = items
6363
@parent = parent
6464
@context = context
@@ -71,14 +71,14 @@ def initialize(items, parent: nil, field: nil, context: nil, first: nil, after:
7171
@edge_class = edge_class || self.class::Edge
7272
# This is only true if the object was _initialized_ with an override
7373
# or if one is assigned later.
74-
@has_max_page_size_override = max_page_size != :not_given
75-
@max_page_size = if max_page_size == :not_given
74+
@has_max_page_size_override = max_page_size != NOT_CONFIGURED
75+
@max_page_size = if max_page_size == NOT_CONFIGURED
7676
nil
7777
else
7878
max_page_size
7979
end
80-
@has_default_page_size_override = default_page_size != :not_given
81-
@default_page_size = if default_page_size == :not_given
80+
@has_default_page_size_override = default_page_size != NOT_CONFIGURED
81+
@default_page_size = if default_page_size == NOT_CONFIGURED
8282
nil
8383
else
8484
default_page_size

lib/graphql/query.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ def warden
318318
# @param value [Object] Any runtime value
319319
# @return [GraphQL::ObjectType, nil] The runtime type of `value` from {Schema#resolve_type}
320320
# @see {#possible_types} to apply filtering from `only` / `except`
321-
def resolve_type(abstract_type, value = :__undefined__)
322-
if value.is_a?(Symbol) && value == :__undefined__
321+
def resolve_type(abstract_type, value = NOT_CONFIGURED)
322+
if value.is_a?(Symbol) && value == NOT_CONFIGURED
323323
# Old method signature
324324
value = abstract_type
325325
abstract_type = nil

lib/graphql/query/context.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def initialize(query:, schema: query.schema, values:, object:)
9191
end
9292

9393
class ScopedContext
94-
NO_PATH = [].freeze
95-
NO_CONTEXT = {}.freeze
94+
NO_PATH = GraphQL::EmptyObjects::EMPTY_ARRAY
95+
NO_CONTEXT = GraphQL::EmptyObjects::EMPTY_HASH
9696

9797
def initialize(query_context)
9898
@query_context = query_context

lib/graphql/schema/argument.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class Argument
77
include GraphQL::Schema::Member::HasDirectives
88
include GraphQL::Schema::Member::HasDeprecationReason
99
include GraphQL::Schema::Member::HasValidators
10-
include GraphQL::Schema::FindInheritedValue::EmptyObjects
11-
12-
NO_DEFAULT = :__no_default__
10+
include GraphQL::EmptyObjects
1311

1412
# @return [String] the GraphQL name for this argument, camelized unless `camelize: false` is provided
1513
attr_reader :name
@@ -20,8 +18,8 @@ class Argument
2018

2119
# @param new_prepare [Method, Proc]
2220
# @return [Symbol] A method or proc to call to transform this value before sending it to field resolution method
23-
def prepare(new_prepare = NO_DEFAULT)
24-
if new_prepare != NO_DEFAULT
21+
def prepare(new_prepare = NOT_CONFIGURED)
22+
if new_prepare != NOT_CONFIGURED
2523
@prepare = new_prepare
2624
end
2725
@prepare
@@ -52,7 +50,7 @@ def from_resolver?
5250
# @param deprecation_reason [String]
5351
# @param validates [Hash, nil] Options for building validators, if any should be applied
5452
# @param replace_null_with_default [Boolean] if `true`, incoming values of `null` will be replaced with the configured `default_value`
55-
def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, owner:, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, &definition_block)
53+
def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NOT_CONFIGURED, as: nil, from_resolver: false, camelize: true, prepare: nil, owner:, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, &definition_block)
5654
arg_name ||= name
5755
@name = -(camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s)
5856
@type_expr = type_expr || type
@@ -104,16 +102,16 @@ def inspect
104102

105103
# @param default_value [Object] The value to use when the client doesn't provide one
106104
# @return [Object] the value used when the client doesn't provide a value for this argument
107-
def default_value(new_default_value = NO_DEFAULT)
108-
if new_default_value != NO_DEFAULT
105+
def default_value(new_default_value = NOT_CONFIGURED)
106+
if new_default_value != NOT_CONFIGURED
109107
@default_value = new_default_value
110108
end
111109
@default_value
112110
end
113111

114112
# @return [Boolean] True if this argument has a default value
115113
def default_value?
116-
@default_value != NO_DEFAULT
114+
@default_value != NOT_CONFIGURED
117115
end
118116

119117
def replace_null_with_default?

0 commit comments

Comments
 (0)