Sha256: af74436e084d69a7ea4d9fbb28c6b020a8a3c8ac0cdd054134a554bd9d5fe473

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Define
    # Turn field configs into a {GraphQL::Field} and attach it to a {GraphQL::ObjectType} or {GraphQL::InterfaceType}
    module AssignObjectField
      def self.call(owner_type, name, type_or_field = nil, desc = nil, field: nil, **kwargs, &block)
        name_s = name.to_s

        # Move some possitional definitions into keyword defns:
        kwargs[:description] ||= desc
        kwargs[:name] ||= name_s

        if !type_or_field.nil? && !type_or_field.is_a?(GraphQL::Field)
          kwargs[:type] = type_or_field
        end

        # Figure out how to find or initialize the field instance:
        if type_or_field.is_a?(GraphQL::Field)
          field = type_or_field
          field = field.redefine(name: name_s)
        elsif block_given?
          field = GraphQL::Field.define(kwargs, &block)
        elsif field.nil?
          field = GraphQL::Field.define(kwargs)
        elsif field.is_a?(GraphQL::Field)
          field = field.redefine(name: name_s)
        else
          raise("Couldn't find a field argument, received: #{field || type_or_field}")
        end

        # Attach the field to the type
        owner_type.fields[name_s] = field
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
graphql-1.4.5 lib/graphql/define/assign_object_field.rb
graphql-1.4.4 lib/graphql/define/assign_object_field.rb
graphql-1.4.3 lib/graphql/define/assign_object_field.rb
graphql-1.4.2 lib/graphql/define/assign_object_field.rb
graphql-1.4.1 lib/graphql/define/assign_object_field.rb
graphql-1.4.0 lib/graphql/define/assign_object_field.rb
graphql-1.3.0 lib/graphql/define/assign_object_field.rb