Class: GraphQL::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/function.rb

Overview

A reusable container for field logic, including arguments, resolve, return type, and documentation.

Class-level values defined with the DSL will be inherited, so Functions can extend one another.

It's OK to override the instance methods here in order to customize behavior of instances.

Examples:

A reusable GraphQL::Function attached as a field

class FindRecord < GraphQL::Function
  attr_reader :type

  def initialize(model:, type:)
    @model = model
    @type = type
  end

  argument :id, GraphQL::ID_TYPE

  def resolve(obj, args, ctx)
     @model.find(args.id)
  end
end

QueryType = GraphQL::ObjectType.define do
  name "Query"
  field :post, function: FindRecord.new(model: Post, type: PostType)
  field :comment, function: FindRecord.new(model: Comment, type: CommentType)
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.argument(*args, **kwargs, &block) ⇒ void

This method returns an undefined value.

Define an argument for this function & its subclasses

See Also:

  • same arguments as the `argument` definition helper


66
67
68
69
70
# File 'lib/graphql/function.rb', line 66

def argument(*args, **kwargs, &block)
  argument = GraphQL::Argument.from_dsl(*args, **kwargs, &block)
  own_arguments[argument.name] = argument
  nil
end

.argumentsHash<String => GraphQL::Argument>



73
74
75
76
77
78
79
# File 'lib/graphql/function.rb', line 73

def arguments
  if parent_function?
    own_arguments.merge(superclass.arguments)
  else
    own_arguments.dup
  end
end

.complexity(new_value = nil) ⇒ Object

Get or set this class's complexity



119
# File 'lib/graphql/function.rb', line 119

inherited_value(:complexity)

.deprecation_reason(new_value = nil) ⇒ Object

Get or set this class's deprecation_reason



116
# File 'lib/graphql/function.rb', line 116

inherited_value(:deprecation_reason)

.description(new_value = nil) ⇒ Object

Get or set this class's description



113
# File 'lib/graphql/function.rb', line 113

inherited_value(:description)

.inherited_value(name) ⇒ 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.

Class-level reader/writer which is inherited



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/graphql/function.rb', line 97

def self.inherited_value(name)
  self.class_eval "def \#{name}(new_value = nil)\nif new_value\n@\#{name} = new_value\nelsif parent_function?\n@\#{name} || superclass.\#{name}\nelse\n@\#{name}\nend\nend\n"
end

.type(premade_type = nil, &block) ⇒ GraphQL::BaseType

Get or set the return type for this function class & descendants



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/function.rb', line 83

def type(premade_type = nil, &block)
  if block_given?
    @type = GraphQL::ObjectType.define(&block)
  elsif premade_type
    @type = premade_type
  elsif parent_function?
    @type || superclass.type
  else
    @type
  end
end

Instance Method Details

#argumentsHash<String => GraphQL::Argument>



33
34
35
# File 'lib/graphql/function.rb', line 33

def arguments
  self.class.arguments
end

#call(obj, args, ctx) ⇒ Object

Returns This function's resolver

Raises:

  • (NotImplementedError)


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

def call(obj, args, ctx)
  raise NotImplementedError
end

#complexityInteger, Proc



58
59
60
# File 'lib/graphql/function.rb', line 58

def complexity
  self.class.complexity || 1
end

#deprecation_reasonString?



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

def deprecation_reason
  self.class.deprecation_reason
end

#descriptionString?



48
49
50
# File 'lib/graphql/function.rb', line 48

def description
  self.class.description
end

#typeGraphQL::BaseType



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

def type
  self.class.type
end