Sha256: 80cc0420c37efbba094750221cc4b0dcfc6f887051ebdb84e40cc4fa91f97ba9

Contents?: true

Size: 1.61 KB

Versions: 66

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    class Directive < GraphQL::Schema::Member
      # 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.
      #
      # @example Installing the directive
      #   class MySchema < GraphQL::Schema
      #     directive(GraphQL::Schema::Directive::Transform)
      #   end
      #
      # @example Transforming strings
      #   viewer {
      #     username @transform(by: "upcase")
      #   }
      class Transform < Schema::Directive
        description "Directs the executor to run named transform on the return value."

        locations(
          GraphQL::Schema::Directive::FIELD,
        )

        argument :by, String, required: true,
          description: "The name of the transform to run if applicable"

        TRANSFORMS = [
          "upcase",
          "downcase",
          # ??
        ]
        # Implement the Directive API
        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
      end
    end
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
graphql-1.10.14 lib/graphql/schema/directive/transform.rb
graphql-1.11.1 lib/graphql/schema/directive/transform.rb
graphql-1.10.13 lib/graphql/schema/directive/transform.rb
graphql-1.11.0 lib/graphql/schema/directive/transform.rb
graphql-1.10.12 lib/graphql/schema/directive/transform.rb
graphql-1.9.21 lib/graphql/schema/directive/transform.rb
graphql-1.10.11 lib/graphql/schema/directive/transform.rb
graphql-1.9.20 lib/graphql/schema/directive/transform.rb
graphql-1.10.10 lib/graphql/schema/directive/transform.rb
graphql-1.10.9 lib/graphql/schema/directive/transform.rb
graphql-1.10.8 lib/graphql/schema/directive/transform.rb
graphql-1.10.7 lib/graphql/schema/directive/transform.rb
graphql-1.10.6 lib/graphql/schema/directive/transform.rb
graphql-1.10.5 lib/graphql/schema/directive/transform.rb
graphql-1.10.4 lib/graphql/schema/directive/transform.rb
graphql-1.10.3 lib/graphql/schema/directive/transform.rb
graphql-1.10.2 lib/graphql/schema/directive/transform.rb
graphql-1.9.19 lib/graphql/schema/directive/transform.rb
graphql-1.10.1 lib/graphql/schema/directive/transform.rb
graphql-1.10.0 lib/graphql/schema/directive/transform.rb