Sha256: d0eaabd62f646f9b1f39c0f2c494c7d64e312f306c67826d4219592d80739f11

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Client
    module Query
      class Operation
        include HasSelectionSet

        attr_reader :document, :name, :selection_set, :variables

        def initialize(document, name: nil, variables: {})
          @document = document
          @name = name
          @selection_set = SelectionSet.new
          @variables = variables

          yield self if block_given?
        end

        def schema
          document.schema
        end

        def to_query
          operation_type.dup.tap do |query_string|
            query_string << " #{name}" if name
            query_string << "(#{variables_string.join(', ')})" if variables.any?
            query_string << " {\n"
            query_string << selection_set.to_query
            query_string << "\n}\n"
          end
        end

        alias_method :to_s, :to_query

        private

        def variables_string
          variables.map do |name, type|
            "$#{name}: #{type}"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql_client-0.4.1 lib/graphql_client/query/operation.rb
graphql_client-0.3.3 lib/graphql_client/query/operation.rb