module GraphQL::Schema::Member::HasFields
Shared code for Objects, Interfaces, Mutations, Subscriptions
Public Instance Methods
# File lib/graphql/schema/member/has_fields.rb, line 78 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
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 theGraphQLAPI);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) βFielddescription;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 isinstance_evalβd on the newField.
Options
-
kwargs.:name(Symbol) β The underscore-cased version of this field name (will be camelized for theGraphQLAPI); 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 totrue)trueif this field may returnnull,falseif it is nevernull -
kwargs.:description(String) βFielddescription; positional argument also accepted -
kwargs.:comment(String) βFieldcomment -
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 toname) -
kwargs.:hash_key(String, Symbol) β The hash key to lookup on the underlying object (if its a Hash) to resolve this field (defaults tonameorname.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 toname) -
kwargs.:resolve_static(Symbol, true) β Used bySchema.execute_nextto produce a single value, shared by all objects which resolve this field. Called on the owner type class withcontext, **arguments -
kwargs.:resolve_batch(Symbol, true) β Used bySchema.execute_nextmapobjectsto a same-sized Array of results. Called on the owner type class withobjects, context, **arguments. -
kwargs.:resolve_each(Symbol, true) β Used bySchema.execute_nextto get a value value for each item. Called on the owner type class withobject, context, **arguments. -
kwargs.:resolve_legacy_instance_method(Symbol, true) β Used bySchema.execute_nextto get a value value for each item. Calls an instance method on the object type class. -
kwargs.:connection(Boolean) βtrueif this field should get automagic connection behavior; default is to infer by*Connectionin the return type name -
kwargs.:connection_extension(Class) β The extension to add, to implement connections. Ifnil, no extension is added. -
kwargs.:max_page_size(Integer, nil) β For connections, the maximum number of items to return from this field, ornilto allow unlimited results. -
kwargs.:default_page_size(Integer, nil) β For connections, the default number of items to return from this field, ornilto 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_itemsmethod will be called on the return value -
kwargs.:subscription_scope(Symbol, String) β A key incontextwhich 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, aGraphQL::Tracingtracer 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