Sha256: acd02e3ca7e6a6243f4b93d2945ad95a91f70a15243b40bafea84253a4b72711

Contents?: true

Size: 1.5 KB

Versions: 174

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true
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)
        elsif !field.hash_key.nil?
          HashKeyResolve.new(field.hash_key)
        else
          NameResolve.new(field)
        end
      end

      # These only require `obj` as input
      class BuiltInResolve
      end

      # Resolve the field by `public_send`ing `@method_name`
      class MethodResolve < BuiltInResolve
        def initialize(field)
          @method_name = field.property.to_sym
        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

174 entries across 174 versions & 2 rubygems

Version Path
graphql-1.13.23 lib/graphql/field/resolve.rb
graphql-1.13.22 lib/graphql/field/resolve.rb
graphql-1.13.21 lib/graphql/field/resolve.rb
graphql-1.13.20 lib/graphql/field/resolve.rb
graphql-1.13.19 lib/graphql/field/resolve.rb
graphql-1.13.18 lib/graphql/field/resolve.rb
graphql-1.13.17 lib/graphql/field/resolve.rb
graphql-1.13.16 lib/graphql/field/resolve.rb
graphql-1.13.15 lib/graphql/field/resolve.rb
graphql-1.13.14 lib/graphql/field/resolve.rb
graphql-1.13.13 lib/graphql/field/resolve.rb
graphql_cody-1.13.0 lib/graphql/field/resolve.rb
graphql-1.13.12 lib/graphql/field/resolve.rb
graphql-1.13.11 lib/graphql/field/resolve.rb
graphql-1.13.10 lib/graphql/field/resolve.rb
graphql-1.13.9 lib/graphql/field/resolve.rb
graphql-1.12.24 lib/graphql/field/resolve.rb
graphql-1.13.8 lib/graphql/field/resolve.rb
graphql-1.13.7 lib/graphql/field/resolve.rb
graphql-1.13.6 lib/graphql/field/resolve.rb