Sha256: 3911adcda14ef76ed76bd57ee51aa53b34a458c138b3b64dfba3a0db9c67c963

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require_relative './base'

module GQLi
  # Node wrapper
  class Node < Base
    attr_reader :__params

    def initialize(name, params = {}, depth = 1, &block)
      super(name, depth, &block)
      @__params = params
    end

    # Serializes to a GraphQL string
    def to_gql
      result = '  ' * __depth + __name
      result += __params_to_s(__params, true) unless __params.empty?
      unless __nodes.empty?
        result += " {\n"
        result += __nodes.map(&:to_gql).join("\n")
        result += "\n#{'  ' * __depth}}"
      end

      result
    end

    private

    def __directive?(params)
      params.size == 1 && params.keys.first.to_s.start_with?('@')
    end

    def __directive(params)
      params.first.tap do |directive, directive_params|
        return " #{directive}#{__params_to_s(directive_params, true)}"
      end
    end

    def __params_to_s(params, initial = false)
      case params
      when ::Hash
        return __directive(params) if __directive?(params)

        result = params.map do |k, v|
          "#{k}: #{__params_to_s(v)}"
        end.join(', ')

        return "(#{result})" if initial

        "{#{result}}"
      when ::Array
        "[#{params.map { |p| __params_to_s(p) }.join(', ')}]"
      when ::String
        "\"#{params}\""
      else
        params.to_s
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gqli-1.2.0 lib/gqli/node.rb
gqli-1.1.0 lib/gqli/node.rb
gqli-1.0.0 lib/gqli/node.rb
gqli-0.6.1 lib/gqli/node.rb
gqli-0.6.0 lib/gqli/node.rb
gqli-0.5.0 lib/gqli/node.rb
gqli-0.4.0 lib/gqli/node.rb