Class: GraphQL::Schema::Directive::Transform

Inherits:
GraphQL::Schema::Directive show all
Defined in:
lib/graphql/schema/directive/transform.rb

Overview

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")
}

Defined Under Namespace

Classes: Transformer

Constant Summary collapse

TRANSFORMS =
[
  "upcase",
  "downcase",
  # ??
]

Constants inherited from GraphQL::Schema::Directive

DEFAULT_DEPRECATION_REASON, LOCATIONS, LOCATION_DESCRIPTIONS

Constants included from Member::HasArguments

Member::HasArguments::NO_ARGUMENTS

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Member::GraphQLTypeNames

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

Instance Attribute Summary

Attributes inherited from GraphQL::Schema::Directive

#arguments, #owner

Attributes included from Member::BaseDSLMethods

#default_graphql_name, #graphql_name

Attributes included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Member::HasAstNode

#ast_node

Class Method Summary collapse

Methods inherited from GraphQL::Schema::Directive

default_directive, default_directive?, default_graphql_name, #graphql_name, include?, #initialize, locations, on_field?, on_fragment?, on_operation?, path, repeatable, repeatable?, resolve_each, resolve_fragment_spread, resolve_inline_fragment, resolve_operation, runtime?, static_include?, validate!

Methods included from Member::HasArguments

#add_argument, #all_argument_definitions, #any_arguments?, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, extended, #get_argument, included, #own_arguments, #remove_argument, #validate_directive_argument

Methods included from Member::HasArguments::HasDirectiveArguments

#validate_directive_argument

Methods included from Member::HasValidators

extended, #validates, #validators

Methods inherited from Member

authorizes?

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?

Methods included from FindInheritedValue

extended, included

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

#initialize, #kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

extended, #inherited

Methods included from Member::HasDirectives

add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive

Constructor Details

This class inherits a constructor from GraphQL::Schema::Directive

Class Method Details

.resolve(object, arguments, context, &block) ⇒ Object

Implement the Directive API : (untyped object, Hash[Symbol, untyped] arguments, GraphQL::Query::Context context) { () -> untyped } -> nil



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

.resolve_field(ast_nodes, parent_type, field_defn, objects, arguments, context) ⇒ Object

: (Array ast_nodes, untyped parent_type, GraphQL::Schema::Field field_defn, Array objects, Hash[Symbol, untyped] arguments, GraphQL::Query::Context context) -> Transformer?



70
71
72
73
74
75
76
77
# 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