Module: GraphQL::InternalRepresentation::Print

Defined in:
lib/graphql/internal_representation/print.rb

Class Method Summary collapse

Class Method Details



7
8
9
10
# File 'lib/graphql/internal_representation/print.rb', line 7

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


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/graphql/internal_representation/print.rb', line 12

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