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

Example: Turning a document into a query string

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

Example: 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) — Whitespace to add to the printed node
  • truncate_size (Integer, nil) — The size to truncate to.

Returns

  • String — Valid GraphQL for node

:call-seq:

print(Nodes::AbstractNode node, String indent:, Integer | nil truncate_size:) -> String


70
71
72
73
74
75
76
77
# File 'lib/graphql/language/printer.rb', line 70

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