Class: GraphQL::Field

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/field.rb,
lib/graphql/field/resolve.rb

Defined Under Namespace

Modules: DefaultLazyResolve, Resolve

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Constructor Details

#initializeField

Returns a new instance of Field.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphql/field.rb', line 104

def initialize
  @complexity = 1
  @arguments = {}
  @resolve_proc = build_default_resolver
  @lazy_resolve_proc = DefaultLazyResolve
  @relay_node_field = false
  @connection = false
  @connection_max_page_size = nil
  @edge_class = nil
  @trace = nil
  @introspection = false
end

Instance Attribute Details

#argumentsHash<String => GraphQL::Argument>

Returns Map String argument names to their Argument implementations.

Returns:



53
54
55
# File 'lib/graphql/field.rb', line 53

def arguments
  @arguments
end

#arguments_classObject



70
71
72
# File 'lib/graphql/field.rb', line 70

def arguments_class
  @arguments_class
end

#ast_nodeObject



81
82
83
# File 'lib/graphql/field.rb', line 81

def ast_node
  @ast_node
end

#complexityNumeric, Proc

Returns The complexity for this field (default: 1), as a constant or a proc like ->(query_ctx, args, child_complexity) { } # Numeric.

Returns:

  • (Numeric, Proc)

    The complexity for this field (default: 1), as a constant or a proc like ->(query_ctx, args, child_complexity) { } # Numeric



59
60
61
# File 'lib/graphql/field.rb', line 59

def complexity
  @complexity
end

#connection=(value) ⇒ Object (writeonly)



72
73
74
# File 'lib/graphql/field.rb', line 72

def connection=(value)
  @connection = value
end

#connection_max_page_sizenil, Integer

Returns:

  • (nil, Integer)


102
103
104
# File 'lib/graphql/field.rb', line 102

def connection_max_page_size
  @connection_max_page_size
end

#deprecation_reasonString?

Returns The client-facing reason why this field is deprecated (if present, the field is deprecated).

Returns:

  • (String, nil)

    The client-facing reason why this field is deprecated (if present, the field is deprecated)



50
51
52
# File 'lib/graphql/field.rb', line 50

def deprecation_reason
  @deprecation_reason
end

#descriptionString?

Returns The client-facing description of this field.

Returns:

  • (String, nil)

    The client-facing description of this field



47
48
49
# File 'lib/graphql/field.rb', line 47

def description
  @description
end

#edge_classnil, Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (nil, Class)


94
95
96
# File 'lib/graphql/field.rb', line 94

def edge_class
  @edge_class
end

#functionObject, GraphQL::Function

Returns The function used to derive this field.

Returns:



68
69
70
# File 'lib/graphql/field.rb', line 68

def function
  @function
end

#hash_keyObject?

Returns The key to access with obj.[] to resolve this field (overrides #name if present).

Returns:

  • (Object, nil)

    The key to access with obj.[] to resolve this field (overrides #name if present)



65
66
67
# File 'lib/graphql/field.rb', line 65

def hash_key
  @hash_key
end

#introspection=(value) ⇒ Object (writeonly)



73
74
75
# File 'lib/graphql/field.rb', line 73

def introspection=(value)
  @introspection = value
end

#lazy_resolve_proc<#call(obj, args, ctx)> (readonly)

Returns A proc-like object which can be called trigger a lazy resolution.

Returns:

  • (<#call(obj, args, ctx)>)

    A proc-like object which can be called trigger a lazy resolution



40
41
42
# File 'lib/graphql/field.rb', line 40

def lazy_resolve_proc
  @lazy_resolve_proc
end

#mutationGraphQL::Relay::Mutation?

Returns The mutation this field was derived from, if it was derived from a mutation.

Returns:



56
57
58
# File 'lib/graphql/field.rb', line 56

def mutation
  @mutation
end

#nameString Also known as: graphql_name

Returns The name of this field on its ObjectType (or InterfaceType).

Returns:



43
44
45
# File 'lib/graphql/field.rb', line 43

def name
  @name
end

#propertySymbol?

Returns The method to call on obj to return this field (overrides #name if present).

Returns:

  • (Symbol, nil)

    The method to call on obj to return this field (overrides #name if present)



62
63
64
# File 'lib/graphql/field.rb', line 62

def property
  @property
end

#relay_node_fieldBoolean

Returns True if this is the Relay find-by-id field.

Returns:

  • (Boolean)

    True if this is the Relay find-by-id field



31
32
33
# File 'lib/graphql/field.rb', line 31

def relay_node_field
  @relay_node_field
end

#relay_nodes_fieldBoolean

Returns True if this is the Relay find-by-ids field.

Returns:

  • (Boolean)

    True if this is the Relay find-by-ids field



34
35
36
# File 'lib/graphql/field.rb', line 34

def relay_nodes_field
  @relay_nodes_field
end

#resolve_proc<#call(obj, args, ctx)> (readonly)

Returns A proc-like object which can be called to return the field’s value.

Returns:

  • (<#call(obj, args, ctx)>)

    A proc-like object which can be called to return the field’s value



37
38
39
# File 'lib/graphql/field.rb', line 37

def resolve_proc
  @resolve_proc
end

#subscription_scopenil, String

Returns Prefix for subscription names from this field.

Returns:

  • (nil, String)

    Prefix for subscription names from this field



76
77
78
# File 'lib/graphql/field.rb', line 76

def subscription_scope
  @subscription_scope
end

#traceBoolean

Returns True if this field should be traced. By default, fields are only traced if they are not a ScalarType or EnumType.

Returns:

  • (Boolean)

    True if this field should be traced. By default, fields are only traced if they are not a ScalarType or EnumType.



79
80
81
# File 'lib/graphql/field.rb', line 79

def trace
  @trace
end

Instance Method Details

#connection?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/graphql/field.rb', line 88

def connection?
  @connection
end

#edges?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/graphql/field.rb', line 97

def edges?
  !!@edge_class
end

#get_argument(argument_name) ⇒ Object



210
211
212
# File 'lib/graphql/field.rb', line 210

def get_argument(argument_name)
  arguments[argument_name]
end

#initialize_copy(other) ⇒ Object



117
118
119
120
121
# File 'lib/graphql/field.rb', line 117

def initialize_copy(other)
  ensure_defined
  super
  @arguments = other.arguments.dup
end

#introspection?Boolean

Returns Is this field a predefined introspection field?.

Returns:

  • (Boolean)

    Is this field a predefined introspection field?



124
125
126
# File 'lib/graphql/field.rb', line 124

def introspection?
  @introspection
end

#lazy_resolve(obj, args, ctx) ⇒ Object

If #resolve returned an object which should be handled lazily, this method will be called later to force the object to return its value.

Parameters:

Returns:

  • (Object)

    The result of calling the registered method on obj



189
190
191
# File 'lib/graphql/field.rb', line 189

def lazy_resolve(obj, args, ctx)
  @lazy_resolve_proc.call(obj, args, ctx)
end

#lazy_resolve=(new_lazy_resolve_proc) ⇒ Object

Assign a new resolve proc to this field. Used for #lazy_resolve



194
195
196
# File 'lib/graphql/field.rb', line 194

def lazy_resolve=(new_lazy_resolve_proc)
  @lazy_resolve_proc = new_lazy_resolve_proc
end

#prepare_lazy(obj, args, ctx) ⇒ GraphQL::Execution::Lazy

Prepare a lazy value for this field. It may be then-ed and resolved later.

Returns:



200
201
202
203
204
# File 'lib/graphql/field.rb', line 200

def prepare_lazy(obj, args, ctx)
  GraphQL::Execution::Lazy.new {
    lazy_resolve(obj, args, ctx)
  }
end

#resolve(object, arguments, context) ⇒ Object

Get a value for this field

Examples:

resolving a field value

field.resolve(obj, args, ctx)

Parameters:

  • object (Object)

    The object this field belongs to

  • arguments (Hash)

    Arguments declared in the query

  • context (GraphQL::Query::Context)


135
136
137
# File 'lib/graphql/field.rb', line 135

def resolve(object, arguments, context)
  resolve_proc.call(object, arguments, context)
end

#resolve=(new_resolve_proc) ⇒ Object

Provide a new callable for this field’s resolve function. If nil, a new resolve proc will be build based on its #name, #property or #hash_key.

Parameters:

  • new_resolve_proc (<#call(obj, args, ctx)>, nil)


142
143
144
# File 'lib/graphql/field.rb', line 142

def resolve=(new_resolve_proc)
  @resolve_proc = new_resolve_proc || build_default_resolver
end

#to_sObject



179
180
181
# File 'lib/graphql/field.rb', line 179

def to_s
  "<Field name:#{name || "not-named"} desc:#{description} resolve:#{resolve_proc}>"
end

#typeObject

Get the return type for this field.



152
153
154
# File 'lib/graphql/field.rb', line 152

def type
  @clean_type ||= GraphQL::BaseType.resolve_related_type(@dirty_type)
end

#type=(new_return_type) ⇒ Object



146
147
148
149
# File 'lib/graphql/field.rb', line 146

def type=(new_return_type)
  @clean_type = nil
  @dirty_type = new_return_type
end

#type_classObject



206
207
208
# File 'lib/graphql/field.rb', line 206

def type_class
  [:type_class]
end