Sha256: abf3b5c2140e56de3e33a150695b1cf8019a71dac5cceb795c12d5121ba40b64

Contents?: true

Size: 1.49 KB

Versions: 168

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
module GraphQL
  module InternalRepresentation
    module Print
      module_function

      def print(schema, query_string)
        query = GraphQL::Query.new(schema, query_string)
        print_node(query.irep_selection)
      end

      def print_node(node, indent: 0)
        padding = " " * indent
        typed_children_padding = " " * (indent + 2)
        query_str = "".dup

        if !node.definition
          op_node = node.ast_node
          name = op_node.name ? " " + op_node.name : ""
          op_type = op_node.operation_type
          query_str << "#{op_type}#{name}"
        else
          if node.name == node.definition_name
            query_str << "#{padding}#{node.name}"
          else
            query_str << "#{padding}#{node.name}: #{node.definition_name}"
          end

          args = node.ast_nodes.map { |n| n.arguments.map(&:to_query_string).join(",") }.uniq
          query_str << args.map { |a| "(#{a})"}.join("|")
        end

        if node.typed_children.any?
          query_str << " {\n"
          node.typed_children.each do |type, children|
            query_str << "#{typed_children_padding}... on #{type.name} {\n"
            children.each do |name, child|
              query_str << print_node(child, indent: indent + 4)
            end
            query_str << "#{typed_children_padding}}\n"
          end
          query_str << "#{padding}}\n"
        else
          query_str << "\n"
        end

        query_str
      end
    end
  end
end

Version data entries

168 entries across 168 versions & 2 rubygems

Version Path
graphql-1.12.11 lib/graphql/internal_representation/print.rb
graphql-1.12.10 lib/graphql/internal_representation/print.rb
graphql-1.12.9 lib/graphql/internal_representation/print.rb
graphql-1.12.8 lib/graphql/internal_representation/print.rb
graphql-1.12.7 lib/graphql/internal_representation/print.rb
graphql-1.12.6 lib/graphql/internal_representation/print.rb
graphql-1.12.5 lib/graphql/internal_representation/print.rb
graphql-1.11.8 lib/graphql/internal_representation/print.rb
graphql-1.12.4 lib/graphql/internal_representation/print.rb
graphql-1.12.3 lib/graphql/internal_representation/print.rb
graphql-1.12.2 lib/graphql/internal_representation/print.rb
graphql-1.12.1 lib/graphql/internal_representation/print.rb
graphql-1.12.0 lib/graphql/internal_representation/print.rb
graphql-1.11.7 lib/graphql/internal_representation/print.rb
graphql-1.11.6 lib/graphql/internal_representation/print.rb
graphql-1.8.18 lib/graphql/internal_representation/print.rb
graphql-1.11.5 lib/graphql/internal_representation/print.rb
graphql-1.11.4 lib/graphql/internal_representation/print.rb
graphql-1.11.3 lib/graphql/internal_representation/print.rb
graphql-1.11.2 lib/graphql/internal_representation/print.rb