Sha256: c94ca9adc32845ab29052722be7ebf9de0f36153f0ac16447caf6d54ad9fd6c1

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

require "kanji/instance_define"
require "kanji/errors"
require "kanji/type/mutation"
require "kanji/type/argument"

module Kanji
  class Type
    class MutationDefiner
      extend Kanji::InstanceDefine

      instance_define :name, :return_type, :resolve

      def initialize(&block)
        @_arguments = []
        self.instance_eval &block

        raise(AttributeError, "You must supply a name") unless @_name
        raise(AttributeError, "You must supply a return type") unless @_return_type
        raise(AttributeError, "You must supply a resolve proc") unless @_resolve
        raise(AttributeError, "You must supply at least one argument") if @_arguments.empty?
      end

      def argument(name, type, **kwargs)
        raise ArgumentError if @_arguments.map(&:name).include?(name.to_s)

        @_arguments << Argument.new({
          name: name.to_s,
          type: type,
          options: kwargs
        })
      end

      def call
        Mutation.new({
          name: @_name,
          return_type: @_return_type,
          arguments: @_arguments,
          resolve: @_resolve
        })
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kanji-web-0.3.1 lib/kanji/type/mutation_definer.rb
kanji-web-0.3.0 lib/kanji/type/mutation_definer.rb
kanji-web-0.2.2 lib/kanji/type/mutation_definer.rb
kanji-web-0.2.1 lib/kanji/type/mutation_definer.rb
kanji-web-0.2.0 lib/kanji/type/mutation_definer.rb
kanji-web-0.1.0 lib/kanji/type/mutation_definer.rb