Sha256: 1c54771a4fd8c4309ab8363618446aa0791145d41e3179a0fb0ebcc3d3940884

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Client
    class GraphObject
      include Deserialization

      attr_reader :data, :parent, :query

      def initialize(query:, data:, parent: nil)
        @query = query
        @data = Hash(data)
        @parent = parent

        @data.each do |field_name, value|
          field = query.selection_set.lookup(field_name)

          graph_object = case value
          when Hash
            klass = if field.connection?
              GraphConnection
            elsif field.node?
              GraphNode
            else
              GraphObject
            end

            klass.new(query: field, data: value, parent: self)
          when Array
            value.map do |v|
              if v.is_a? Hash
                self.class.new(query: field, data: v, parent: self)
              else
                v
              end
            end
          else
            value
          end

          create_accessor_methods(field_name, graph_object)
        end
      end

      def build_minimal_query
        if parent
          parent.build_minimal_query do |context|
            yield context.add_field(query.field_defn.name, as: query.name, **query.arguments)
          end
        else
          Query::QueryDocument.new(query.schema) { |root| yield root }
        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/graph_object.rb
graphql_client-0.3.3 lib/graphql_client/graph_object.rb