Sha256: d47c47095804d1b68b0dfa3e12e5af8bf636329b27f612d4817277b6e2cabd9d

Contents?: true

Size: 1019 Bytes

Versions: 2

Compression:

Stored size: 1019 Bytes

Contents

require "graphql"
require "dry-initializer"
require "kanji/types"
require "kanji/type/attribute"
require "kanji/graph/coerce_type"

module Kanji
  class Graph
    class RegisterObject
      extend Dry::Initializer

      option :attributes, Kanji::Types::Strict::Array.member(Kanji::Type::Attribute)
      option :name, Kanji::Types::Strict::String
      option :description, Kanji::Types::Strict::String, optional: true

      def call
        name = "#{self.name}Type"
        attributes = self.attributes
        description = self.description
        coercer = Graph::CoerceType

        GraphQL::ObjectType.define do
          name name
          description description

          attributes.each do |attribute|
            field attribute.name do
              type -> { coercer.(attribute.type) }
              description attribute.description

              if attribute.resolve
                resolve attribute.resolve
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kanji-web-0.3.1 lib/kanji/graph/register_object.rb
kanji-web-0.3.0 lib/kanji/graph/register_object.rb