Class: GraphQL::Language::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/language/printer.rb

Defined Under Namespace

Classes: TruncatableBuffer

Constant Summary collapse

OMISSION =
"... (truncated)"

Instance Method Summary collapse

Instance Method Details

Turn an arbitrary AST node back into a string.

Examples:

Turning a document into a query string

document = GraphQL.parse(query_string)
GraphQL::Language::Printer.new.print(document)
# => "{ ... }"

Building a custom printer


class MyPrinter < GraphQL::Language::Printer
  def print_argument(arg)
    print_string("#{arg.name}: <HIDDEN>")
  end
end

MyPrinter.new.print(document)
# => "mutation { pay(creditCard: <HIDDEN>) { success } }"

Parameters:

  • node (Nodes::AbstractNode)
  • indent (String) (defaults to: "")

    Whitespace to add to the printed node

  • truncate_size (Integer, nil) (defaults to: nil)

    The size to truncate to.

Returns:

  • (String)

    Valid GraphQL for node



54
55
56
57
58
59
60
61
# File 'lib/graphql/language/printer.rb', line 54

def print(node, indent: "", truncate_size: nil)
  truncate_size = truncate_size ? [truncate_size - OMISSION.size, 0].max : nil
  @out = TruncatableBuffer.new(truncate_size: truncate_size)
  print_node(node, indent: indent)
  @out.to_string
rescue TruncatableBuffer::TruncateSizeReached
  @out.to_string << OMISSION
end