public static function doPrint($ast) { return Visitor::visit($ast, array('leave' => array(Node::NAME => function ($node) { return $node->value . ''; }, Node::VARIABLE => function ($node) { return '$' . $node->name; }, Node::DOCUMENT => function (Document $node) { return self::join($node->definitions, "\n\n") . "\n"; }, Node::OPERATION_DEFINITION => function (OperationDefinition $node) { $op = $node->operation; $name = $node->name; $defs = Printer::manyList('(', $node->variableDefinitions, ', ', ')'); $directives = self::join($node->directives, ' '); $selectionSet = $node->selectionSet; return !$name ? $selectionSet : self::join([$op, self::join([$name, $defs]), $directives, $selectionSet], ' '); }, Node::VARIABLE_DEFINITION => function (VariableDefinition $node) { return self::join([$node->variable . ': ' . $node->type, $node->defaultValue], ' = '); }, Node::SELECTION_SET => function (SelectionSet $node) { return self::blockList($node->selections, ",\n"); }, Node::FIELD => function (Field $node) { $r11 = self::join([$node->alias, $node->name], ': '); $r1 = self::join([$r11, self::manyList('(', $node->arguments, ', ', ')')]); $r2 = self::join($node->directives, ' '); return self::join([$r1, $r2, $node->selectionSet], ' '); }, Node::ARGUMENT => function (Argument $node) { return $node->name . ': ' . $node->value; }, Node::FRAGMENT_SPREAD => function (FragmentSpread $node) { return self::join(['...' . $node->name, self::join($node->directives, '')], ' '); }, Node::INLINE_FRAGMENT => function (InlineFragment $node) { return self::join(['... on', $node->typeCondition, self::join($node->directives, ' '), $node->selectionSet], ' '); }, Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) { return self::join(['fragment', $node->name, 'on', $node->typeCondition, self::join($node->directives, ' '), $node->selectionSet], ' '); }, Node::INT => function (IntValue $node) { return $node->value; }, Node::FLOAT => function (FloatValue $node) { return $node->value; }, Node::STRING => function (StringValue $node) { return json_encode($node->value); }, Node::BOOLEAN => function (BooleanValue $node) { return $node->value ? 'true' : 'false'; }, Node::ENUM => function (EnumValue $node) { return $node->value; }, Node::ARR => function (ArrayValue $node) { return '[' . self::join($node->values, ', ') . ']'; }, Node::OBJECT => function (ObjectValue $node) { return '{' . self::join($node->fields, ', ') . '}'; }, Node::OBJECT_FIELD => function (ObjectField $node) { return $node->name . ': ' . $node->value; }, Node::DIRECTIVE => function (Directive $node) { return self::join(['@' . $node->name, $node->value], ': '); }, Node::LIST_TYPE => function (ListType $node) { return '[' . $node->type . ']'; }, Node::NON_NULL_TYPE => function (NonNullType $node) { return $node->type . '!'; }))); }