Class: GraphQL::Schema::Resolver Private

Inherits:
Object
  • Object
show all
Extended by:
Member::BaseDSLMethods, Member::HasArguments
Includes:
Member::GraphQLTypeNames
Defined in:
lib/graphql/schema/resolver.rb

Overview

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

A class-based container for field configuration and resolution logic. It supports:

  • Arguments, via .argument(...) helper, which will be applied to the field.
  • Return type, via .type(..., null: ...), which will be applied to the field.
  • Description, via .description(...), which will be applied to the field
  • Resolution, via #resolve(**args) method, which will be called to resolve the field.
  • #object and #context accessors for use during #resolve.

Resolvers can be attached with the resolver: option in a field(...) call.

A resolver’s configuration may be overridden with other keywords in the field(...) call.

See the Resolver.field_options to see how a Resolver becomes a set of field configuration options.

See Also:

  • for a concrete subclass of `Resolver`.
  • `Resolver` is a replacement for `GraphQL::Function`

Direct Known Subclasses

Mutation

Constant Summary

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::HasArguments

argument, argument_class, arguments, own_arguments

Methods included from Member::BaseDSLMethods

description, graphql_name, introspection, mutation, name, overridden_graphql_name, to_graphql, unwrap

Constructor Details

#initialize(object:, context:) ⇒ Resolver

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 a new instance of Resolver

Parameters:



29
30
31
32
# File 'lib/graphql/schema/resolver.rb', line 29

def initialize(object:, context:)
  @object = object
  @context = context
end

Instance Attribute Details

#contextGraphQL::Query::Context (readonly)

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.



38
39
40
# File 'lib/graphql/schema/resolver.rb', line 38

def context
  @context
end

#objectObject (readonly)

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 The application object this field is being resolved on

Returns:

  • (Object)

    The application object this field is being resolved on



35
36
37
# File 'lib/graphql/schema/resolver.rb', line 35

def object
  @object
end

Class Method Details

.extras(new_extras = nil) ⇒ Object

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.

Additional info injected into #resolve

See Also:

  • {GraphQL{GraphQL::Schema{GraphQL::Schema::Field{GraphQL::Schema::Field#extras}


58
59
60
61
62
63
64
# File 'lib/graphql/schema/resolver.rb', line 58

def extras(new_extras = nil)
  if new_extras
    @own_extras = new_extras
  end
  own_extras = @own_extras || []
  own_extras + (superclass.respond_to?(:extras) ? superclass.extras : [])
end

.field_optionsObject

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.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/graphql/schema/resolver.rb', line 102

def field_options
  {
    type: type_expr,
    description: description,
    extras: extras,
    method: resolve_method,
    resolver_class: self,
    arguments: arguments,
    null: null,
  }
end

.null(allow_null = nil) ⇒ Object

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.

Specifies whether or not the field is nullable. Defaults to true TODO unify with #type

Parameters:

  • allow_null (Boolean) (defaults to: nil)

    Whether or not the response can be null



69
70
71
72
73
74
75
# File 'lib/graphql/schema/resolver.rb', line 69

def null(allow_null = nil)
  if !allow_null.nil?
    @null = allow_null
  end

  @null.nil? ? (superclass.respond_to?(:null) ? superclass.null : true) : @null
end

.resolve_method(new_method = nil) ⇒ Symbol

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.

Default :resolve set below.

Returns:

  • (Symbol)

    The method to call on instances of this object to resolve the field



49
50
51
52
53
54
# File 'lib/graphql/schema/resolver.rb', line 49

def resolve_method(new_method = nil)
  if new_method
    @resolve_method = new_method
  end
  @resolve_method || (superclass.respond_to?(:resolve_method) ? superclass.resolve_method : :resolve)
end

.type(new_type = nil, null: nil) ⇒ 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.

Call this method to get the return type of the field, or use it as a configuration method to assign a return type instead of generating one. TODO unify with #null

Parameters:

  • new_type (Class, nil) (defaults to: nil)

    If a type definition class is provided, it will be used as the return type of the field

  • null (true, false)

    Whether or not the field may return nil

Returns:

  • (Class)

    The type which this field returns.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/graphql/schema/resolver.rb', line 84

def type(new_type = nil, null: nil)
  if new_type
    if null.nil?
      raise ArgumentError, "required argument `null:` is missing"
    end
    @type_expr = new_type
    @null = null
  else
    if @type_expr
      GraphQL::Schema::Member::BuildType.parse_type(@type_expr, null: @null)
    elsif superclass.respond_to?(:type)
      superclass.type
    else
      nil
    end
  end
end

.type_exprObject

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.

A non-normalized type configuration, without null applied



115
116
117
# File 'lib/graphql/schema/resolver.rb', line 115

def type_expr
  @type_expr || (superclass.respond_to?(:type_expr) ? superclass.type_expr : nil)
end

Instance Method Details

#resolve(**args) ⇒ Object

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.

Do the work. Everything happens here.

Returns:

  • (Object)

    An object corresponding to the return type

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/graphql/schema/resolver.rb', line 42

def resolve(**args)
  raise NotImplementedError, "#{self.class.name}#resolve should execute the field's logic"
end