Sha256: 46138d539154bc615066bb893df65658b54ec3ce0ab5d3b44316fb35313b40a0

Contents?: true

Size: 1.89 KB

Versions: 39

Compression:

Stored size: 1.89 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,
          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)
            response = context.namespace(:interpreter)[: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
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
graphql-1.13.23 lib/graphql/schema/directive/transform.rb
graphql-1.13.22 lib/graphql/schema/directive/transform.rb
graphql-1.13.21 lib/graphql/schema/directive/transform.rb
graphql-1.13.20 lib/graphql/schema/directive/transform.rb
graphql-1.13.19 lib/graphql/schema/directive/transform.rb
graphql-1.13.18 lib/graphql/schema/directive/transform.rb
graphql-1.13.17 lib/graphql/schema/directive/transform.rb
graphql-2.0.14 lib/graphql/schema/directive/transform.rb
graphql-1.13.16 lib/graphql/schema/directive/transform.rb
graphql-2.0.13 lib/graphql/schema/directive/transform.rb
graphql-2.0.12 lib/graphql/schema/directive/transform.rb
graphql-1.13.15 lib/graphql/schema/directive/transform.rb
graphql-2.0.11 lib/graphql/schema/directive/transform.rb
graphql-1.13.14 lib/graphql/schema/directive/transform.rb
graphql-1.13.13 lib/graphql/schema/directive/transform.rb
graphql-2.0.9 lib/graphql/schema/directive/transform.rb
graphql-2.0.8 lib/graphql/schema/directive/transform.rb
graphql-2.0.7 lib/graphql/schema/directive/transform.rb
graphql_cody-1.13.0 lib/graphql/schema/directive/transform.rb
graphql-1.13.12 lib/graphql/schema/directive/transform.rb