Class: GraphQL::Function
- Inherits:
-
Object
- Object
- GraphQL::Function
- 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.
Class Method Summary collapse
-
.argument(*args, **kwargs, &block) ⇒ void
Define an argument for this function & its subclasses.
-
.arguments ⇒ Hash<String => GraphQL::Argument>
Arguments for this function class, including inherited arguments.
-
.build_field(function) ⇒ Object
-
.complexity(new_value = nil) ⇒ Object
Get or set this class’s complexity.
-
.deprecation_reason(new_value = nil) ⇒ Object
Get or set this class’s deprecation_reason.
-
.description(new_value = nil) ⇒ Object
Get or set this class’s description.
-
.inherited_value(name) ⇒ Object
private
Class-level reader/writer which is inherited.
-
.type(premade_type = nil, &block) ⇒ GraphQL::BaseType
Get or set the return type for this function class & descendants.
-
.types ⇒ Object
Provides shorthand access to GraphQL’s built-in types.
Instance Method Summary collapse
-
#arguments ⇒ Hash<String => GraphQL::Argument>
Arguments, keyed by name.
-
#call(obj, args, ctx) ⇒ Object
This function’s resolver.
-
#complexity ⇒ Integer, Proc
-
#deprecation_reason ⇒ String?
-
#description ⇒ String?
-
#type ⇒ GraphQL::BaseType
Return type.
Class Method Details
.argument(*args, **kwargs, &block) ⇒ void
This method returns an undefined value.
Define an argument for this function & its subclasses
68 69 70 71 72 |
# File 'lib/graphql/function.rb', line 68 def argument(*args, **kwargs, &block) argument = GraphQL::Argument.from_dsl(*args, **kwargs, &block) own_arguments[argument.name] = argument nil end |
.arguments ⇒ Hash<String => GraphQL::Argument>
Returns Arguments for this function class, including inherited arguments
75 76 77 78 79 80 81 |
# File 'lib/graphql/function.rb', line 75 def arguments if parent_function? own_arguments.merge(superclass.arguments) else own_arguments.dup end end |
.build_field(function) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/graphql/function.rb', line 102 def build_field(function) GraphQL::Field.define( arguments: function.arguments, complexity: function.complexity, type: function.type, resolve: function, description: function.description, function: function, deprecation_reason: function.deprecation_reason, ) end |
.complexity(new_value = nil) ⇒ Object
Get or set this class’s complexity
138 |
# File 'lib/graphql/function.rb', line 138 inherited_value(:complexity) |
.deprecation_reason(new_value = nil) ⇒ Object
Get or set this class’s deprecation_reason
135 |
# File 'lib/graphql/function.rb', line 135 inherited_value(:deprecation_reason) |
.description(new_value = nil) ⇒ Object
Get or set this class’s description
132 |
# File 'lib/graphql/function.rb', line 132 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
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/graphql/function.rb', line 116 def self.inherited_value(name) self.class_eval <<-RUBY def #{name}(new_value = nil) if new_value @#{name} = new_value elsif parent_function? @#{name} || superclass.#{name} else @#{name} end end RUBY end |
.type(premade_type = nil, &block) ⇒ GraphQL::BaseType
Get or set the return type for this function class & descendants
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/graphql/function.rb', line 90 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 |
.types ⇒ Object
Provides shorthand access to GraphQL’s built-in types
84 85 86 |
# File 'lib/graphql/function.rb', line 84 def types GraphQL::Define::TypeDefiner.instance end |
Instance Method Details
#arguments ⇒ Hash<String => GraphQL::Argument>
Returns Arguments, keyed by name
35 36 37 |
# File 'lib/graphql/function.rb', line 35 def arguments self.class.arguments end |
#call(obj, args, ctx) ⇒ Object
Returns This function’s resolver
45 46 47 |
# File 'lib/graphql/function.rb', line 45 def call(obj, args, ctx) raise GraphQL::RequiredImplementationMissingError end |
#complexity ⇒ Integer, Proc
60 61 62 |
# File 'lib/graphql/function.rb', line 60 def complexity self.class.complexity || 1 end |
#deprecation_reason ⇒ String?
55 56 57 |
# File 'lib/graphql/function.rb', line 55 def deprecation_reason self.class.deprecation_reason end |
#description ⇒ String?
50 51 52 |
# File 'lib/graphql/function.rb', line 50 def description self.class.description end |
#type ⇒ GraphQL::BaseType
Return type
40 41 42 |
# File 'lib/graphql/function.rb', line 40 def type self.class.type end |