Class: GraphQL::Schema::Field
- Inherits:
-
Object
- Object
- GraphQL::Schema::Field
- Extended by:
- FindInheritedValue
- Includes:
- GraphQL::Schema::FindInheritedValue::EmptyObjects, Member::HasArguments, Member::HasAstNode, Member::HasDeprecationReason, Member::HasDirectives, Member::HasPath, Member::HasValidators
- Defined in:
- lib/graphql/schema/field.rb,
lib/graphql/schema/field/scope_extension.rb,
lib/graphql/schema/field/connection_extension.rb
Defined Under Namespace
Classes: ConnectionExtension, FieldImplementationFailed, MissingReturnTypeError, ScopeExtension
Constant Summary
Constants included from Member::HasDirectives
Member::HasDirectives::NO_DIRECTIVES
Constants included from GraphQL::Schema::FindInheritedValue::EmptyObjects
GraphQL::Schema::FindInheritedValue::EmptyObjects::EMPTY_ARRAY, GraphQL::Schema::FindInheritedValue::EmptyObjects::EMPTY_HASH
Constants included from Member::HasArguments
Member::HasArguments::NO_ARGUMENTS
Instance Attribute Summary collapse
-
#description(text = nil) ⇒ String
-
#method_str ⇒ String
readonly
Method or hash key on the underlying object to look up.
-
#method_sym ⇒ Symbol
readonly
Method or hash key on the underlying object to look up.
-
#name ⇒ String
(also: #graphql_name)
readonly
The GraphQL name for this field, camelized unless
camelize: false
is provided. -
#original_name ⇒ Symbol
readonly
The original name of the field, passed in by the user.
-
#owner ⇒ Class
The thing this field was defined on (type, mutation, resolver).
-
#relay_node_field ⇒ Object
readonly
Boolean.
-
#subscription_scope ⇒ String?
-
#trace ⇒ Boolean
readonly
Apply tracing to this field? (Default: skip scalars, this is the override value).
-
#type ⇒ Object
Class Method Summary collapse
-
.connection_extension(new_extension_class = nil) ⇒ Class
This extension is applied to fields when #connection? is true.
-
.from_options(name = nil, type = nil, desc = nil, resolver: nil, mutation: nil, subscription: nil, **kwargs, &block) ⇒ GraphQL::Schema:Field
Create a field instance from a list of arguments, keyword arguments, and a block.
Instance Method Summary collapse
-
#accessible?(context) ⇒ Boolean
-
#authorized?(object, args, context) ⇒ Boolean
-
#broadcastable? ⇒ Boolean?
If true, subscription updates with this field can be shared between viewers.
-
#calculate_complexity(query:, nodes:, child_complexity:) ⇒ Object
-
#complexity(new_complexity = nil) ⇒ Object
-
#connection? ⇒ Boolean
Can be set with
connection: true|false
or inferred from a type name ending in*Connection
. -
#extension(extension_class, options = nil) ⇒ void
Add
extension
to this field, initialized withoptions
if provided. -
#extensions(new_extensions = nil) ⇒ Array<GraphQL::Schema::FieldExtension>
Read extension instances from this field, or add new classes/options to be initialized on this field.
-
#extras(new_extras = nil) ⇒ Array<Symbol>
Read extras (as symbols) from this field, or add new extras to be opted into by this field’s resolver.
-
#fetch_extra(extra_name, ctx) ⇒ Object
-
#has_max_page_size? ⇒ Boolean
True if this field’s #max_page_size should override the schema default.
-
#initialize(type: nil, name: nil, owner: nil, null: nil, description: :not_given, deprecation_reason: nil, method: nil, hash_key: nil, dig: nil, resolver_method: nil, connection: nil, max_page_size: :not_given, scope: nil, introspection: false, camelize: true, trace: nil, complexity: nil, ast_node: nil, extras: EMPTY_ARRAY, extensions: EMPTY_ARRAY, connection_extension: self.class.connection_extension, resolver_class: nil, subscription_scope: nil, relay_node_field: false, relay_nodes_field: false, method_conflict_warning: true, broadcastable: nil, arguments: EMPTY_HASH, directives: EMPTY_HASH, validates: EMPTY_ARRAY, &definition_block) ⇒ Field
constructor
A new instance of Field.
-
#inspect ⇒ Object
-
#introspection? ⇒ Boolean
Is this field a predefined introspection field?.
-
#max_page_size ⇒ Integer?
Applied to connections if #has_max_page_size?.
-
#method_conflict_warning? ⇒ Boolean
Should we warn if this field’s name conflicts with a built-in method?.
-
#owner_type ⇒ Class
The GraphQL type this field belongs to.
-
#resolve(object, args, query_ctx) ⇒ Object
This method is called by the interpreter for each field.
-
#resolver ⇒ Class?
(also: #mutation)
The Resolver this field was derived from, if there is one.
-
#resolver_method ⇒ Symbol
The method on the type to look up.
-
#scoped? ⇒ Boolean
If true, the return type’s
.scope_items
method will be applied to this field’s return value. -
#visible?(context) ⇒ Boolean
Methods included from Member::HasDeprecationReason
#deprecation_reason, #deprecation_reason=
Methods included from Member::HasDirectives
#directive, #directives, #remove_directive
Methods included from Member::HasValidators
Methods included from Member::HasPath
Methods included from Member::HasAstNode
Methods included from Member::HasArguments
#add_argument, #all_argument_definitions, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, #get_argument, #own_arguments, #remove_argument, #validate_directive_argument
Constructor Details
#initialize(type: nil, name: nil, owner: nil, null: nil, description: :not_given, deprecation_reason: nil, method: nil, hash_key: nil, dig: nil, resolver_method: nil, connection: nil, max_page_size: :not_given, scope: nil, introspection: false, camelize: true, trace: nil, complexity: nil, ast_node: nil, extras: EMPTY_ARRAY, extensions: EMPTY_ARRAY, connection_extension: self.class.connection_extension, resolver_class: nil, subscription_scope: nil, relay_node_field: false, relay_nodes_field: false, method_conflict_warning: true, broadcastable: nil, arguments: EMPTY_HASH, directives: EMPTY_HASH, validates: EMPTY_ARRAY, &definition_block) ⇒ Field
Returns a new instance of Field.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/graphql/schema/field.rb', line 214 def initialize(type: nil, name: nil, owner: nil, null: nil, description: :not_given, deprecation_reason: nil, method: nil, hash_key: nil, dig: nil, resolver_method: nil, connection: nil, max_page_size: :not_given, scope: nil, introspection: false, camelize: true, trace: nil, complexity: nil, ast_node: nil, extras: EMPTY_ARRAY, extensions: EMPTY_ARRAY, connection_extension: self.class.connection_extension, resolver_class: nil, subscription_scope: nil, relay_node_field: false, relay_nodes_field: false, method_conflict_warning: true, broadcastable: nil, arguments: EMPTY_HASH, directives: EMPTY_HASH, validates: EMPTY_ARRAY, &definition_block) if name.nil? raise ArgumentError, "missing first `name` argument or keyword `name:`" end if !(resolver_class) if type.nil? raise ArgumentError, "missing second `type` argument or keyword `type:`" end end @original_name = name name_s = -name.to_s @underscored_name = -Member::BuildType.underscore(name_s) @name = -(camelize ? Member::BuildType.camelize(name_s) : name_s) if description != :not_given @description = description end self.deprecation_reason = deprecation_reason if method && hash_key && dig raise ArgumentError, "Provide `method:`, `hash_key:` _or_ `dig:`, not multiple. (called with: `method: #{method.inspect}, hash_key: #{hash_key.inspect}, dig: #{dig.inspect}`)" end if resolver_method if method raise ArgumentError, "Provide `method:` _or_ `resolver_method:`, not both. (called with: `method: #{method.inspect}, resolver_method: #{resolver_method.inspect}`)" end if hash_key || dig raise ArgumentError, "Provide `hash_key:`, `dig:`, _or_ `resolver_method:`, not multiple. (called with: `hash_key: #{hash_key.inspect}, dig: #{dig.inspect}, resolver_method: #{resolver_method.inspect}`)" end end # TODO: I think non-string/symbol hash keys are wrongly normalized (eg `1` will not work) method_name = method || hash_key || name_s @dig_keys = dig resolver_method ||= name_s.to_sym @method_str = -method_name.to_s @method_sym = method_name.to_sym @resolver_method = resolver_method @complexity = complexity @return_type_expr = type @return_type_null = if !null.nil? null elsif resolver_class nil else true end @connection = connection @has_max_page_size = max_page_size != :not_given @max_page_size = max_page_size == :not_given ? nil : max_page_size @introspection = introspection @extras = extras if !broadcastable.nil? @broadcastable = broadcastable end @resolver_class = resolver_class @scope = scope @trace = trace @relay_node_field = relay_node_field @relay_nodes_field = relay_nodes_field @ast_node = ast_node @method_conflict_warning = method_conflict_warning arguments.each do |name, arg| case arg when Hash argument(name: name, **arg) when GraphQL::Schema::Argument add_argument(arg) when Array arg.each { |a| add_argument(a) } else raise ArgumentError, "Unexpected argument config (#{arg.class}): #{arg.inspect}" end end @owner = owner @subscription_scope = subscription_scope @extensions = EMPTY_ARRAY @call_after_define = false # This should run before connection extension, # but should it run after the definition block? if scoped? self.extension(ScopeExtension) end # The problem with putting this after the definition_block # is that it would override arguments if connection? && connection_extension self.extension(connection_extension) end # Do this last so we have as much context as possible when initializing them: if extensions.any? self.extensions(extensions) end if resolver_class && resolver_class.extensions.any? self.extensions(resolver_class.extensions) end if directives.any? directives.each do |(dir_class, )| self.directive(dir_class, **) end end if !validates.empty? self.validates(validates) end if definition_block if definition_block.arity == 1 yield self else instance_eval(&definition_block) end end self.extensions.each(&:after_define_apply) @call_after_define = true end |
Instance Attribute Details
#description(text = nil) ⇒ String
355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/graphql/schema/field.rb', line 355 def description(text = nil) if text @description = text elsif defined?(@description) @description elsif @resolver_class @description || @resolver_class.description else nil end end |
#method_str ⇒ String (readonly)
Returns Method or hash key on the underlying object to look up.
29 30 31 |
# File 'lib/graphql/schema/field.rb', line 29 def method_str @method_str end |
#method_sym ⇒ Symbol (readonly)
Returns Method or hash key on the underlying object to look up.
26 27 28 |
# File 'lib/graphql/schema/field.rb', line 26 def method_sym @method_sym end |
#name ⇒ String (readonly) Also known as: graphql_name
Returns the GraphQL name for this field, camelized unless camelize: false
is provided.
20 21 22 |
# File 'lib/graphql/schema/field.rb', line 20 def name @name end |
#original_name ⇒ Symbol (readonly)
Returns the original name of the field, passed in by the user.
55 56 57 |
# File 'lib/graphql/schema/field.rb', line 55 def original_name @original_name end |
#owner ⇒ Class
Returns The thing this field was defined on (type, mutation, resolver).
41 42 43 |
# File 'lib/graphql/schema/field.rb', line 41 def owner @owner end |
#relay_node_field ⇒ Object (readonly)
Returns Boolean.
180 181 182 |
# File 'lib/graphql/schema/field.rb', line 180 def relay_node_field @relay_node_field end |
#subscription_scope ⇒ String?
77 78 79 |
# File 'lib/graphql/schema/field.rb', line 77 def subscription_scope @subscription_scope || (@resolver_class.respond_to?(:subscription_scope) ? @resolver_class.subscription_scope : nil) end |
#trace ⇒ Boolean (readonly)
Returns Apply tracing to this field? (Default: skip scalars, this is the override value).
74 75 76 |
# File 'lib/graphql/schema/field.rb', line 74 def trace @trace end |
#type ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/graphql/schema/field.rb', line 538 def type if @resolver_class return_type = @return_type_expr || @resolver_class.type_expr if return_type.nil? raise MissingReturnTypeError, "Can't determine the return type for #{self.path} (it has `resolver: #{@resolver_class}`, perhaps that class is missing a `type ...` declaration, or perhaps its type causes a cyclical loading issue)" end nullable = @return_type_null.nil? ? @resolver_class.null : @return_type_null Member::BuildType.parse_type(return_type, null: nullable) else @type ||= Member::BuildType.parse_type(@return_type_expr, null: @return_type_null) end rescue GraphQL::Schema::InvalidDocumentError, MissingReturnTypeError => err # Let this propagate up raise err rescue StandardError => err raise MissingReturnTypeError, "Failed to build return type for #{@owner.graphql_name}.#{name} from #{@return_type_expr.inspect}: (#{err.class}) #{err.}", err.backtrace end |
Class Method Details
.connection_extension(new_extension_class = nil) ⇒ Class
This extension is applied to fields when #connection? is true.
You can override it in your base field definition.
171 172 173 174 175 176 177 |
# File 'lib/graphql/schema/field.rb', line 171 def self.connection_extension(new_extension_class = nil) if new_extension_class @connection_extension = new_extension_class else @connection_extension ||= find_inherited_value(:connection_extension, ConnectionExtension) end end |
.from_options(name = nil, type = nil, desc = nil, resolver: nil, mutation: nil, subscription: nil, **kwargs, &block) ⇒ GraphQL::Schema:Field
Create a field instance from a list of arguments, keyword arguments, and a block.
This method implements prioritization between the resolver
or mutation
defaults
and the local overrides via other keywords.
It also normalizes positional arguments into keywords for #initialize.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/graphql/schema/field.rb', line 93 def self.(name = nil, type = nil, desc = nil, resolver: nil, mutation: nil, subscription: nil,**kwargs, &block) if (resolver_class = resolver || mutation || subscription) # Add a reference to that parent class kwargs[:resolver_class] = resolver_class end if name kwargs[:name] = name end if !type.nil? if desc if kwargs[:description] raise ArgumentError, "Provide description as a positional argument or `description:` keyword, but not both (#{desc.inspect}, #{kwargs[:description].inspect})" end kwargs[:description] = desc kwargs[:type] = type elsif (resolver || mutation) && type.is_a?(String) # The return type should be copied from the resolver, and the second positional argument is the description kwargs[:description] = type else kwargs[:type] = type end if type.is_a?(Class) && type < GraphQL::Schema::Mutation raise ArgumentError, "Use `field #{name.inspect}, mutation: Mutation, ...` to provide a mutation to this field instead" end end new(**kwargs, &block) end |
Instance Method Details
#accessible?(context) ⇒ Boolean
564 565 566 567 568 569 570 |
# File 'lib/graphql/schema/field.rb', line 564 def accessible?(context) if @resolver_class @resolver_class.accessible?(context) else true end end |
#authorized?(object, args, context) ⇒ Boolean
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/graphql/schema/field.rb', line 572 def (object, args, context) if @resolver_class # The resolver _instance_ will check itself during `resolve()` @resolver_class.(object, context) else if (arg_values = context[:current_arguments]) # ^^ that's provided by the interpreter at runtime, and includes info about whether the default value was used or not. using_arg_values = true arg_values = arg_values.argument_values else arg_values = args using_arg_values = false end # Faster than `.any?` arguments(context).each_value do |arg| arg_key = arg.keyword if arg_values.key?(arg_key) arg_value = arg_values[arg_key] if using_arg_values if arg_value.default_used? # pass -- no auth required for default used next else application_arg_value = arg_value.value if application_arg_value.is_a?(GraphQL::Execution::Interpreter::Arguments) application_arg_value.keyword_arguments end end else application_arg_value = arg_value end if !arg.(object, application_arg_value, context) return false end end end true end end |
#broadcastable? ⇒ Boolean?
If true, subscription updates with this field can be shared between viewers
343 344 345 346 347 348 349 350 351 |
# File 'lib/graphql/schema/field.rb', line 343 def broadcastable? if defined?(@broadcastable) @broadcastable elsif @resolver_class @resolver_class.broadcastable? else nil end end |
#calculate_complexity(query:, nodes:, child_complexity:) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/graphql/schema/field.rb', line 442 def calculate_complexity(query:, nodes:, child_complexity:) if respond_to?(:complexity_for) lookahead = GraphQL::Execution::Lookahead.new(query: query, field: self, ast_nodes: nodes, owner_type: owner) complexity_for(child_complexity: child_complexity, query: query, lookahead: lookahead) elsif connection? arguments = query.arguments_for(nodes.first, self) max_possible_page_size = nil if arguments.respond_to?(:[]) # It might have been an error if arguments[:first] max_possible_page_size = arguments[:first] end if arguments[:last] && (max_possible_page_size.nil? || arguments[:last] > max_possible_page_size) max_possible_page_size = arguments[:last] end end if max_possible_page_size.nil? max_possible_page_size = max_page_size || query.schema.default_max_page_size end if max_possible_page_size.nil? raise GraphQL::Error, "Can't calculate complexity for #{path}, no `first:`, `last:`, `max_page_size` or `default_max_page_size`" else = 0 lookahead = GraphQL::Execution::Lookahead.new(query: query, field: self, ast_nodes: nodes, owner_type: owner) if (page_info_lookahead = lookahead.selection(:page_info)).selected? += 1 # pageInfo += page_info_lookahead.selections.size # subfields end if lookahead.selects?(:total) || lookahead.selects?(:total_count) || lookahead.selects?(:count) += 1 end nodes_edges_complexity = 0 nodes_edges_complexity += 1 if lookahead.selects?(:edges) nodes_edges_complexity += 1 if lookahead.selects?(:nodes) # Possible bug: selections on `edges` and `nodes` are _both_ multiplied here. Should they be? items_complexity = child_complexity - - nodes_edges_complexity # Add 1 for _this_ field 1 + (max_possible_page_size * items_complexity) + + nodes_edges_complexity end else defined_complexity = complexity case defined_complexity when Proc arguments = query.arguments_for(nodes.first, self) defined_complexity.call(query.context, arguments.keyword_arguments, child_complexity) when Numeric defined_complexity + child_complexity else raise("Invalid complexity: #{defined_complexity.inspect} on #{path} (#{inspect})") end end end |
#complexity(new_complexity = nil) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/graphql/schema/field.rb', line 501 def complexity(new_complexity = nil) case new_complexity when Proc if new_complexity.parameters.size != 3 fail( "A complexity proc should always accept 3 parameters: ctx, args, child_complexity. "\ "E.g.: complexity ->(ctx, args, child_complexity) { child_complexity * args[:limit] }" ) else @complexity = new_complexity end when Numeric @complexity = new_complexity when nil if @resolver_class @complexity || @resolver_class.complexity || 1 else @complexity || 1 end else raise("Invalid complexity: #{new_complexity.inspect} on #{@name}") end end |
#connection? ⇒ Boolean
Can be set with connection: true|false
or inferred from a type name ending in *Connection
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/graphql/schema/field.rb', line 126 def connection? if @connection.nil? # Provide default based on type name return_type_name = if @resolver_class && @resolver_class.type Member::BuildType.to_type_name(@resolver_class.type) elsif @return_type_expr Member::BuildType.to_type_name(@return_type_expr) else # As a last ditch, try to force loading the return type: type.unwrap.name end @connection = return_type_name.end_with?("Connection") else @connection end end |
#extension(extension_class, options = nil) ⇒ void
This method returns an undefined value.
Add extension
to this field, initialized with options
if provided.
407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/graphql/schema/field.rb', line 407 def extension(extension_class, = nil) extension_inst = extension_class.new(field: self, options: ) if @extensions.frozen? @extensions = @extensions.dup end if @call_after_define extension_inst.after_define_apply end @extensions << extension_inst nil end |
#extensions(new_extensions = nil) ⇒ Array<GraphQL::Schema::FieldExtension>
Read extension instances from this field, or add new classes/options to be initialized on this field. Extensions are executed in the order they are added.
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/graphql/schema/field.rb', line 382 def extensions(new_extensions = nil) if new_extensions new_extensions.each do |extension_config| if extension_config.is_a?(Hash) extension_class, = *extension_config.to_a[0] self.extension(extension_class, ) else self.extension(extension_config) end end end @extensions end |
#extras(new_extras = nil) ⇒ Array<Symbol>
Read extras (as symbols) from this field, or add new extras to be opted into by this field’s resolver.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/graphql/schema/field.rb', line 424 def extras(new_extras = nil) if new_extras.nil? # Read the value field_extras = @extras if @resolver_class && @resolver_class.extras.any? field_extras + @resolver_class.extras else field_extras end else if @extras.frozen? @extras = @extras.dup end # Append to the set of extras on this field @extras.concat(new_extras) end end |
#fetch_extra(extra_name, ctx) ⇒ Object
709 710 711 712 713 714 715 716 717 |
# File 'lib/graphql/schema/field.rb', line 709 def fetch_extra(extra_name, ctx) if extra_name != :path && extra_name != :ast_node && respond_to?(extra_name) self.public_send(extra_name) elsif ctx.respond_to?(extra_name) ctx.public_send(extra_name) else raise GraphQL::RequiredImplementationMissingError, "Unknown field extra for #{self.path}: #{extra_name.inspect}" end end |
#has_max_page_size? ⇒ Boolean
Returns True if this field’s #max_page_size should override the schema default.
526 527 528 |
# File 'lib/graphql/schema/field.rb', line 526 def has_max_page_size? @has_max_page_size || (@resolver_class && @resolver_class.has_max_page_size?) end |
#inspect ⇒ Object
67 68 69 |
# File 'lib/graphql/schema/field.rb', line 67 def inspect "#<#{self.class} #{path}#{all_argument_definitions.any? ? "(...)" : ""}: #{type.to_type_signature}>" end |
#introspection? ⇒ Boolean
Returns Is this field a predefined introspection field?.
63 64 65 |
# File 'lib/graphql/schema/field.rb', line 63 def introspection? @introspection end |
#max_page_size ⇒ Integer?
Returns Applied to connections if #has_max_page_size?.
531 532 533 |
# File 'lib/graphql/schema/field.rb', line 531 def max_page_size @max_page_size || (@resolver_class && @resolver_class.max_page_size) end |
#method_conflict_warning? ⇒ Boolean
Returns Should we warn if this field’s name conflicts with a built-in method?.
183 184 185 |
# File 'lib/graphql/schema/field.rb', line 183 def method_conflict_warning? @method_conflict_warning end |
#owner_type ⇒ Class
Returns The GraphQL type this field belongs to. (For fields defined on mutations, it’s the payload type).
44 45 46 47 48 49 50 51 52 |
# File 'lib/graphql/schema/field.rb', line 44 def owner_type @owner_type ||= if owner.nil? raise GraphQL::InvariantError, "Field #{original_name.inspect} (graphql name: #{graphql_name.inspect}) has no owner, but all fields should have an owner. How did this happen?!" elsif owner < GraphQL::Schema::Mutation owner.payload_type else owner end end |
#resolve(object, args, query_ctx) ⇒ Object
This method is called by the interpreter for each field. You can extend it in your base field classes.
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
# File 'lib/graphql/schema/field.rb', line 618 def resolve(object, args, query_ctx) # Unwrap the GraphQL object to get the application object. application_object = object.object method_receiver = nil method_to_call = nil method_args = nil Schema::Validator.validate!(validators, application_object, query_ctx, args) query_ctx.schema.after_lazy(self.(application_object, args, query_ctx)) do || if with_extensions(object, args, query_ctx) do |obj, ruby_kwargs| method_args = ruby_kwargs if @resolver_class if obj.is_a?(GraphQL::Schema::Object) obj = obj.object end obj = @resolver_class.new(object: obj, context: query_ctx, field: self) end # Find a way to resolve this field, checking: # # - A method on the type instance; # - Hash keys, if the wrapped object is a hash; # - A method on the wrapped object; # - Or, raise not implemented. # if obj.respond_to?(resolver_method) method_to_call = resolver_method method_receiver = obj # Call the method with kwargs, if there are any if ruby_kwargs.any? obj.public_send(resolver_method, **ruby_kwargs) else obj.public_send(resolver_method) end elsif obj.object.is_a?(Hash) inner_object = obj.object if @dig_keys inner_object.dig(*@dig_keys) elsif inner_object.key?(@method_sym) inner_object[@method_sym] else inner_object[@method_str] end elsif obj.object.respond_to?(@method_sym) method_to_call = @method_sym method_receiver = obj.object if ruby_kwargs.any? obj.object.public_send(@method_sym, **ruby_kwargs) else obj.object.public_send(@method_sym) end else raise <<-ERR Failed to implement #{@owner.graphql_name}.#{@name}, tried: - `#{obj.class}##{resolver_method}`, which did not exist - `#{obj.object.class}##{@method_sym}`, which did not exist - Looking up hash key `#{@method_sym.inspect}` or `#{@method_str.inspect}` on `#{obj.object}`, but it wasn't a Hash To implement this field, define one of the methods above (and check for typos) ERR end end else raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: query_ctx, field: self) end end rescue GraphQL::UnauthorizedFieldError => err err.field ||= self begin query_ctx.schema.(err) rescue GraphQL::ExecutionError => err err end rescue GraphQL::UnauthorizedError => err begin query_ctx.schema.(err) rescue GraphQL::ExecutionError => err err end rescue ArgumentError if method_receiver && method_to_call assert_satisfactory_implementation(method_receiver, method_to_call, method_args) end # if the line above doesn't raise, re-raise raise end |
#resolver ⇒ Class? Also known as: mutation
Returns The Resolver this field was derived from, if there is one.
58 59 60 |
# File 'lib/graphql/schema/field.rb', line 58 def resolver @resolver_class end |
#resolver_method ⇒ Symbol
Returns The method on the type to look up.
32 33 34 35 36 37 38 |
# File 'lib/graphql/schema/field.rb', line 32 def resolver_method if @resolver_class @resolver_class.resolver_method else @resolver_method end end |
#scoped? ⇒ Boolean
Returns if true, the return type’s .scope_items
method will be applied to this field’s return value.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/graphql/schema/field.rb', line 144 def scoped? if !@scope.nil? # The default was overridden @scope elsif @return_type_expr # Detect a list return type, but don't call `type` since that may eager-load an otherwise lazy-loaded type @return_type_expr.is_a?(Array) || (@return_type_expr.is_a?(String) && @return_type_expr.include?("[")) || connection? elsif @resolver_class resolver_type = @resolver_class.type_expr resolver_type.is_a?(Array) || (resolver_type.is_a?(String) && resolver_type.include?("[")) || connection? else false end end |
#visible?(context) ⇒ Boolean
556 557 558 559 560 561 562 |
# File 'lib/graphql/schema/field.rb', line 556 def visible?(context) if @resolver_class @resolver_class.visible?(context) else true end end |