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:

Installing the directive

class MySchema < GraphQL::Schema
  directive(GraphQL::Schema::Directive::Transform)
end

Transforming strings

viewer {
  username @transform(by: "upcase")
}

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 Member::HasDirectives

Member::HasDirectives::NO_DIRECTIVES

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

Class Method Summary collapse

Methods inherited from GraphQL::Schema::Directive

default_directive, default_directive?, default_graphql_name, include?, #initialize, locations, on_field?, on_fragment?, on_operation?, path, static_include?, to_graphql

Methods included from Member::HasArguments

#add_argument, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, #get_argument, #own_arguments, #validate_directive_argument

Methods included from Member::CachedGraphQLDefinition

#graphql_definition, #initialize_copy, #type_class

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

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

Methods included from Member::Scoped

#scope_items

Methods included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

#ast_node

Methods included from Member::HasDirectives

#directive, #directives, #remove_directive

Constructor Details

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

Class Method Details

.resolve(object, arguments, context) ⇒ Object

Implement the Directive API



36
37
38
39
40
41
42
43
44
# File 'lib/graphql/schema/directive/transform.rb', line 36

def self.resolve(object, arguments, context)
  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)
    context.namespace(:interpreter)[:runtime].write_in_response(path, return_value)
  end
end