Module: GraphQL::Schema::Member::HasFields

Includes:
EmptyObjects
Included in:
Interface::DefinitionMethods, GraphQL::Schema::Mutation, Object, Subscription
Defined in:
lib/graphql/schema/member/has_fields.rb

Overview

Shared code for Objects, Interfaces, Mutations, Subscriptions

@requires_ancestor: Kernel @requires_ancestor: Module @requires_ancestor: GraphQL::Schema::Member::BaseDSLMethods

Defined Under Namespace

Modules: InterfaceMethods, ObjectMethods

Constant Summary collapse

RUBY_KEYWORDS =

A list of Ruby keywords.

:nodoc:

[:class, :module, :def, :undef, :begin, :rescue, :ensure, :end, :if, :unless, :then, :elsif, :else, :case, :when, :while, :until, :for, :break, :next, :redo, :retry, :in, :do, :return, :yield, :super, :self, :nil, :true, :false, :and, :or, :not, :alias, :defined?, :BEGIN, :END, :__LINE__, :__FILE__]
GRAPHQL_RUBY_KEYWORDS =

A list of GraphQL-Ruby keywords.

:nodoc:

[:context, :object, :raw_value]
CONFLICT_FIELD_NAMES =

A list of field names that we should advise users to pick a different resolve method name.

:nodoc:

Set.new(GRAPHQL_RUBY_KEYWORDS + RUBY_KEYWORDS + Object.instance_methods)

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(child_class) ⇒ Object



351
352
353
354
# File 'lib/graphql/schema/member/has_fields.rb', line 351

def self.extended(child_class)
  child_class.extend(ObjectMethods)
  super
end

.included(child_class) ⇒ Object



345
346
347
348
349
# File 'lib/graphql/schema/member/has_fields.rb', line 345

def self.included(child_class)
  # Included in an interface definition methods module
  child_class.include(InterfaceMethods)
  super
end

Instance Method Details

#add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?) ⇒ Object

Register this field with the class, overriding a previous one if needed.

Parameters

  • field_defn (GraphQL::Schema::Field)

Returns

  • void

:call-seq:

add_field(GraphQL::Schema::Field field_defn, method_conflict_warning:) -> void


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/graphql/schema/member/has_fields.rb', line 143

def add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?)
  # Check that `field_defn.original_name` equals `resolver_method` and `method_sym` --
  # that shows that no override value was given manually.
  if method_conflict_warning &&
      CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) &&
      field_defn.original_name == field_defn.resolver_method &&
      field_defn.original_name == field_defn.method_sym &&
      field_defn.hash_key == NOT_CONFIGURED &&
      field_defn.dig_keys.nil?
    warn(conflict_field_name_warning(field_defn))
  end
  prev_defn = own_fields[field_defn.name]

  case prev_defn
  when nil
    own_fields[field_defn.name] = field_defn
  when Array
    prev_defn << field_defn
  when GraphQL::Schema::Field
    own_fields[field_defn.name] = [prev_defn, field_defn]
  else
    raise "Invariant: unexpected previous field definition for #{field_defn.name.inspect}: #{prev_defn.inspect}"
  end

  nil
end

#all_field_definitions ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/graphql/schema/member/has_fields.rb', line 234

def all_field_definitions
  all_fields = {}
  all_ancestors = ancestors #: Array[untyped]
  all_ancestors.reverse_each do |ancestor|
    if ancestor.respond_to?(:own_fields)
      all_fields.merge!(ancestor.own_fields)
    end
  end
  all_fields = all_fields.values
  all_fields.flatten!
  all_fields
end

#field(name_positional = nil, type_positional = nil, desc_positional = nil, **kwargs, &definition_block) ⇒ Object

Add a field to this object or interface with the given definition

Parameters

  • name_positional (Symbol) — The underscore-cased version of this field name (will be camelized for the GraphQL API); name: keyword is also accepted
  • type_positional (Class, GraphQL::BaseType, Array) — The return type of this field; type: keyword is also accepted
  • desc_positional (String) — Field description; description: keyword is also accepted
  • kwargs (Hash) — Keywords for defining the field. Any not documented here will be passed to your base field class where they must be handled.
  • definition_block (Proc) — an additional block for configuring the field. Receive the field as a block param, or, if no block params are defined, then the block is instance_eval'd on the new Field.

Options

  • kwargs.:name (Symbol) — The underscore-cased version of this field name (will be camelized for the GraphQL API); positional argument also accepted
  • kwargs.:type (Class, GraphQL::BaseType, Array) — The return type of this field; positional argument is also accepted
  • kwargs.:null (Boolean) — (defaults to true) true if this field may return null, false if it is never null
  • kwargs.:description (String) — Field description; positional argument also accepted
  • kwargs.:comment (String) — Field comment
  • kwargs.:deprecation_reason (String) — If present, the field is marked "deprecated" with this message
  • kwargs.:method (Symbol) — The method to call on the underlying object to resolve this field (defaults to name)
  • kwargs.:hash_key (String, Symbol) — The hash key to lookup on the underlying object (if its a Hash) to resolve this field (defaults to name or name.to_s)
  • kwargs.:dig (Array<String, Symbol>) — The nested hash keys to lookup on the underlying hash to resolve this field using dig
  • kwargs.:resolver_method (Symbol, true) — The method on the type to call to resolve this field (defaults to name)
  • kwargs.:resolve_static (Symbol, true) — Used by Schema.execute_next to produce a single value, shared by all objects which resolve this field. Called on the owner type class with context, **arguments
  • kwargs.:resolve_batch (Symbol, true) — Used by Schema.execute_next map objects to a same-sized Array of results. Called on the owner type class with objects, context, **arguments.
  • kwargs.:resolve_each (Symbol, true) — Used by Schema.execute_next to get a value value for each item. Called on the owner type class with object, context, **arguments.
  • kwargs.:resolve_legacy_instance_method (Symbol, true) — Used by Schema.execute_next to get a value value for each item. Calls an instance method on the object type class.
  • kwargs.:connection (Boolean) — true if this field should get automagic connection behavior; default is to infer by *Connection in the return type name
  • kwargs.:connection_extension (Class) — The extension to add, to implement connections. If nil, no extension is added.
  • kwargs.:max_page_size (Integer, nil) — For connections, the maximum number of items to return from this field, or nil to allow unlimited results.
  • kwargs.:default_page_size (Integer, nil) — For connections, the default number of items to return from this field, or nil to return unlimited results.
  • kwargs.:introspection (Boolean) — If true, this field will be marked as #introspection? and the name may begin with __
  • kwargs.:arguments ({String=>GraphQL::Schema::Argument, Hash}) — Arguments for this field (may be added in the block, also)
  • kwargs.:camelize (Boolean) — If true, the field name will be camelized when building the schema
  • kwargs.:complexity (Numeric) — When provided, set the complexity for this field
  • kwargs.:scope (Boolean) — If true, the return type's .scope_items method will be called on the return value
  • kwargs.:subscription_scope (Symbol, String) — A key in context which will be used to scope subscription payloads
  • kwargs.:extensions (Array<Class, Hash<Class => Object>>) — Named extensions to apply to this field (see also Field#extension)
  • kwargs.:directives (Hash{Class => Hash}) — Directives to apply to this field
  • kwargs.:trace (Boolean) — If true, a GraphQL::Tracing tracer will measure this scalar field
  • kwargs.:broadcastable (Boolean) — Whether or not this field can be distributed in subscription broadcasts
  • kwargs.:ast_node (Language::Nodes::FieldDefinition, nil) — If this schema was parsed from definition, this AST node defined the field
  • kwargs.:method_conflict_warning (Boolean) — If false, skip the warning if this field's method conflicts with a built-in method
  • kwargs.:validates (Array<Hash>) — Configurations for validating this field
  • kwargs.:fallback_value (Object) — A fallback value if the method is not defined
  • kwargs.:mutation (Class<GraphQL::Schema::Mutation>)
  • kwargs.:resolver (Class<GraphQL::Schema::Resolver>)
  • kwargs.:subscription (Class<GraphQL::Schema::Subscription>)
  • kwargs.:dynamic_introspection (Boolean) — (Private, used by GraphQL-Ruby)
  • kwargs.:relay_node_field (Boolean) — (Private, used by GraphQL-Ruby)
  • kwargs.:relay_nodes_field (Boolean) — (Private, used by GraphQL-Ruby)
  • kwargs.:dataload (Class, Hash) — Shorthand for dataloader lookups
  • kwargs.:extras (Array<:ast_node, :parent, :lookahead, :owner, :execution_errors, :graphql_name, :argument_details, Symbol>) — Extra arguments to be injected into the resolver for this field

Yields

  • field (GraphQL::Schema::Field) — The newly-created field instance
  • void

Returns

  • GraphQL::Schema::Field

:call-seq:

field(Symbol name_positional, Class | GraphQL::BaseType | Array type_positional, String desc_positional, Hash **kwargs, Proc &definition_block) -> GraphQL::Schema::Field


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/graphql/schema/member/has_fields.rb', line 79

def field(name_positional = nil, type_positional = nil, desc_positional = nil, **kwargs, &definition_block)
  resolver = kwargs.delete(:resolver)
  mutation = kwargs.delete(:mutation)
  subscription = kwargs.delete(:subscription)
  if (resolver_class = resolver || mutation || subscription)
    # Add a reference to that parent class
    kwargs[:resolver_class] = resolver_class
  end

  kwargs[:name] ||= name_positional
  if !type_positional.nil?
    if desc_positional
      if kwargs[:description]
        raise ArgumentError, "Provide description as a positional argument or `description:` keyword, but not both (#{desc_positional.inspect}, #{kwargs[:description].inspect})"
      end

      kwargs[:description] = desc_positional
      kwargs[:type] = type_positional
    elsif (resolver || mutation) && type_positional.is_a?(String)
      # The return type should be copied from the resolver, and the second positional argument is the description
      kwargs[:description] = type_positional
    else
      kwargs[:type] = type_positional
    end

    if type_positional.is_a?(Class) && type_positional < GraphQL::Schema::Mutation
      raise ArgumentError, "Use `field #{name_positional.inspect}, mutation: Mutation, ...` to provide a mutation to this field instead"
    end
  end

  kwargs[:owner] = self
  field_defn = field_class.new(**kwargs, &definition_block)
  add_field(field_defn)
  field_defn
end

#field_class(new_field_class = nil) ⇒ Object

Returns

  • Class — The class to initialize when adding fields to this kind of schema member

:call-seq:

field_class(new_field_class) -> Class


176
177
178
179
180
181
182
183
184
# File 'lib/graphql/schema/member/has_fields.rb', line 176

def field_class(new_field_class = nil)
  if new_field_class
    @field_class = new_field_class
  elsif defined?(@field_class) && @field_class
    @field_class
  else
    find_inherited_value(:field_class, GraphQL::Schema::Field)
  end
end

#global_id_field(field_name, **kwargs) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/graphql/schema/member/has_fields.rb', line 186

def global_id_field(field_name, **kwargs)
  type = self
  field field_name, "ID", **kwargs, null: false, resolve_each: true
  define_method(field_name) do
    #: self as GraphQL::Schema::Object
    context.schema.id_from_object(object, type, context)
  end

  define_singleton_method(field_name) do |object, context|
    context.schema.id_from_object(object, type, context)
  end
end

#has_no_fields(new_has_no_fields) ⇒ Object

Parameters

  • new_has_no_fields (Boolean) — Call with true to make this Object type ignore the requirement to have any defined fields.

Returns

  • void

:call-seq:

has_no_fields(bool new_has_no_fields) -> void


209
210
211
212
# File 'lib/graphql/schema/member/has_fields.rb', line 209

def has_no_fields(new_has_no_fields)
  @has_no_fields = new_has_no_fields
  nil
end

#has_no_fields? ⇒ Boolean

Returns

  • Boolean — true if has_no_fields(true) was configued

:call-seq:

has_no_fields?() -> bool

Returns:



220
221
222
# File 'lib/graphql/schema/member/has_fields.rb', line 220

def has_no_fields?
  @has_no_fields
end

#own_fields ⇒ Object

Returns

  • Hash<String => GraphQL::Schema::Field, Array<GraphQL::Schema::Field>> — Fields defined on this class specifically, not parent classes

:call-seq:

own_fields() -> Hash[String, GraphQL::Schema::Field | Array[GraphQL::Schema::Field]]


230
231
232
# File 'lib/graphql/schema/member/has_fields.rb', line 230

def own_fields
  @own_fields ||= {}
end