Sha256: b659fc4eb39d4ca71a1cdc7c2d10ca9fa7e94752ca743fccf7b6eaa1a99b4275

Contents?: true

Size: 1.44 KB

Versions: 18

Compression:

Stored size: 1.44 KB

Contents

module GraphQL
  class Field
    # Create resolve procs ahead of time based on a {GraphQL::Field}'s `name`, `property`, and `hash_key` configuration.
    module Resolve
      module_function

      # @param field [GraphQL::Field] A field that needs a resolve proc
      # @return [Proc] A resolver for this field, based on its config
      def create_proc(field)
        if field.property
          MethodResolve.new(field.property.to_sym)
        elsif !field.hash_key.nil?
          HashKeyResolve.new(field.hash_key)
        else
          NameResolve.new(field)
        end
      end

      class BuiltInResolve
      end

      # Resolve the field by `public_send`ing `@method_name`
      class MethodResolve < BuiltInResolve
        def initialize(method_name)
          @method_name = method_name
        end

        def call(obj, args, ctx)
          obj.public_send(@method_name)
        end
      end

      # Resolve the field by looking up `@hash_key` with `#[]`
      class HashKeyResolve < BuiltInResolve
        def initialize(hash_key)
          @hash_key = hash_key
        end

        def call(obj, args, ctx)
          obj[@hash_key]
        end
      end

      # Call the field's name at query-time since
      # it might have changed
      class NameResolve < BuiltInResolve
        def initialize(field)
          @field = field
        end

        def call(obj, args, ctx)
          obj.public_send(@field.name)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
graphql-1.1.0 lib/graphql/field/resolve.rb
graphql-1.0.0 lib/graphql/field/resolve.rb
graphql-0.19.4 lib/graphql/field/resolve.rb
graphql-0.19.3 lib/graphql/field/resolve.rb
graphql-0.19.2 lib/graphql/field/resolve.rb
graphql-0.19.1 lib/graphql/field/resolve.rb
graphql-0.19.0 lib/graphql/field/resolve.rb
graphql-0.18.15 lib/graphql/field/resolve.rb
graphql-0.18.14 lib/graphql/field/resolve.rb
graphql-0.18.13 lib/graphql/field/resolve.rb
graphql-0.18.12 lib/graphql/field/resolve.rb
graphql-0.18.11 lib/graphql/field/resolve.rb
graphql-0.18.10 lib/graphql/field/resolve.rb
graphql-0.18.9 lib/graphql/field/resolve.rb
graphql-0.18.8 lib/graphql/field/resolve.rb
graphql-0.18.7 lib/graphql/field/resolve.rb
graphql-0.18.6 lib/graphql/field/resolve.rb
graphql-0.18.5 lib/graphql/field/resolve.rb