class GraphQL::Schema::Directive::Transform
An example directive to show how you might interact with the runtime.
This directive takes the return value of the tagged part of the query, and if the named transform is whitelisted and applies to the return value, itβs applied by calling a method with that name.
Examples
Example: Installing the directive
class MySchema < GraphQL::Schema directive(GraphQL::Schema::Directive::Transform) end
Example: Transforming strings
viewer { username @transform(by: "upcase") }
Constants
- TRANSFORMS
Public Class Methods
(untyped object, Hash[Symbol, untyped] arguments, GraphQL::Query::Context context) { () → untyped } → nil
Source
# File lib/graphql/schema/directive/transform.rb, line 47 def self.resolve(object, arguments, context, &block) path = context.namespace(:interpreter)[:current_path] return_value = yield transform_name = arguments[:by] if TRANSFORMS.include?(transform_name) && return_value.respond_to?(transform_name) return_value = return_value.public_send(transform_name) response = context.namespace(:interpreter_runtime)[:runtime].final_result *keys, last = path keys.each do |key| if response && (response = response[key]) next else break end end if response response[last] = return_value end nil end end
Implement the Directive API
(Array[GraphQL::Language::Nodes::Field] ast_nodes, untyped parent_type, GraphQL::Schema::Field field_defn, Array[untyped] objects, Hash[Symbol, untyped] arguments, GraphQL::Query::Context context) → Transformer?
Source
# File lib/graphql/schema/directive/transform.rb, line 70 def self.resolve_field(ast_nodes, parent_type, field_defn, objects, arguments, context) transform_name = arguments[:by] if TRANSFORMS.include?(transform_name) Transformer.new(transform_name) else nil end end