graphql-php/src/Utils/SchemaPrinter.php

342 lines
12 KiB
PHP
Raw Normal View History

2017-02-19 22:26:56 +03:00
<?php
namespace GraphQL\Utils;
use GraphQL\Error\Error;
2017-02-19 22:26:56 +03:00
use GraphQL\Language\Printer;
use GraphQL\Type\Introspection;
use GraphQL\Type\Schema;
2017-02-19 22:26:56 +03:00
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\Directive;
/**
2017-08-20 18:10:13 +03:00
* Given an instance of Schema, prints it in GraphQL type language.
2017-02-19 22:26:56 +03:00
*/
class SchemaPrinter
{
2017-08-20 18:10:13 +03:00
/**
* Accepts options as a second argument:
*
* - commentDescriptions:
* Provide true to use preceding comments as the description.
2017-08-20 18:10:13 +03:00
* @api
* @param Schema $schema
* @return string
*/
public static function doPrint(Schema $schema, array $options = [])
2017-02-19 22:26:56 +03:00
{
return self::printFilteredSchema(
$schema,
function($type) {
return !Directive::isSpecifiedDirective($type);
},
function ($type) {
return !Type::isBuiltInType($type);
},
$options
);
2017-02-19 22:26:56 +03:00
}
2017-08-20 18:10:13 +03:00
/**
* @api
* @param Schema $schema
* @return string
*/
public static function printIntrosepctionSchema(Schema $schema, array $options = [])
2017-02-19 22:26:56 +03:00
{
return self::printFilteredSchema(
$schema,
[Directive::class, 'isSpecifiedDirective'],
[Introspection::class, 'isIntrospectionType'],
$options
);
2017-02-19 22:26:56 +03:00
}
private static function printFilteredSchema(Schema $schema, $directiveFilter, $typeFilter, $options)
2017-02-19 22:26:56 +03:00
{
$directives = array_filter($schema->getDirectives(), function($directive) use ($directiveFilter) {
return $directiveFilter($directive);
2017-02-19 22:26:56 +03:00
});
$types = $schema->getTypeMap();
ksort($types);
$types = array_filter($types, $typeFilter);
2017-02-19 22:26:56 +03:00
return implode("\n\n", array_filter(array_merge(
[self::printSchemaDefinition($schema)],
array_map(function($directive) use ($options) { return self::printDirective($directive, $options); }, $directives),
array_map(function($type) use ($options) { return self::printType($type, $options); }, $types)
2017-02-19 22:26:56 +03:00
))) . "\n";
}
private static function printSchemaDefinition(Schema $schema)
{
if (self::isSchemaOfCommonNames($schema)) {
return;
}
$operationTypes = [];
$queryType = $schema->getQueryType();
if ($queryType) {
$operationTypes[] = " query: {$queryType->name}";
}
$mutationType = $schema->getMutationType();
if ($mutationType) {
$operationTypes[] = " mutation: {$mutationType->name}";
}
$subscriptionType = $schema->getSubscriptionType();
if ($subscriptionType) {
$operationTypes[] = " subscription: {$subscriptionType->name}";
}
return "schema {\n" . implode("\n", $operationTypes) . "\n}";
}
2017-02-19 22:26:56 +03:00
/**
* GraphQL schema define root types for each type of operation. These types are
* the same as any other type and can be named in any manner, however there is
* a common naming convention:
*
* schema {
* query: Query
* mutation: Mutation
* }
*
* When using this naming convention, the schema description can be omitted.
*/
private static function isSchemaOfCommonNames(Schema $schema)
{
$queryType = $schema->getQueryType();
if ($queryType && $queryType->name !== 'Query') {
return false;
}
$mutationType = $schema->getMutationType();
if ($mutationType && $mutationType->name !== 'Mutation') {
return false;
}
$subscriptionType = $schema->getSubscriptionType();
if ($subscriptionType && $subscriptionType->name !== 'Subscription') {
return false;
}
return true;
}
public static function printType(Type $type, array $options = [])
2017-02-19 22:26:56 +03:00
{
if ($type instanceof ScalarType) {
return self::printScalar($type, $options);
2017-02-19 22:26:56 +03:00
} else if ($type instanceof ObjectType) {
return self::printObject($type, $options);
2017-02-19 22:26:56 +03:00
} else if ($type instanceof InterfaceType) {
return self::printInterface($type, $options);
2017-02-19 22:26:56 +03:00
} else if ($type instanceof UnionType) {
return self::printUnion($type, $options);
2017-02-19 22:26:56 +03:00
} else if ($type instanceof EnumType) {
return self::printEnum($type, $options);
} else if ($type instanceof InputObjectType) {
return self::printInputObject($type, $options);
2017-02-19 22:26:56 +03:00
}
throw new Error('Unknown type: ' . Utils::printSafe($type) . '.');
2017-02-19 22:26:56 +03:00
}
private static function printScalar(ScalarType $type, array $options)
2017-02-19 22:26:56 +03:00
{
return self::printDescription($options, $type) . "scalar {$type->name}";
2017-02-19 22:26:56 +03:00
}
private static function printObject(ObjectType $type, array $options)
2017-02-19 22:26:56 +03:00
{
$interfaces = $type->getInterfaces();
$implementedInterfaces = !empty($interfaces) ?
' implements ' . implode(', ', array_map(function($i) {
return $i->name;
}, $interfaces)) : '';
return self::printDescription($options, $type) .
2017-02-19 22:26:56 +03:00
"type {$type->name}$implementedInterfaces {\n" .
self::printFields($options, $type) . "\n" .
2017-02-19 22:26:56 +03:00
"}";
}
private static function printInterface(InterfaceType $type, array $options)
2017-02-19 22:26:56 +03:00
{
return self::printDescription($options, $type) .
2017-02-19 22:26:56 +03:00
"interface {$type->name} {\n" .
self::printFields($options, $type) . "\n" .
2017-02-19 22:26:56 +03:00
"}";
}
private static function printUnion(UnionType $type, array $options)
2017-02-19 22:26:56 +03:00
{
return self::printDescription($options, $type) .
2017-02-19 22:26:56 +03:00
"union {$type->name} = " . implode(" | ", $type->getTypes());
}
private static function printEnum(EnumType $type, array $options)
2017-02-19 22:26:56 +03:00
{
return self::printDescription($options, $type) .
2017-02-19 22:26:56 +03:00
"enum {$type->name} {\n" .
self::printEnumValues($type->getValues(), $options) . "\n" .
2017-02-19 22:26:56 +03:00
"}";
}
private static function printEnumValues($values, $options)
2017-02-19 22:26:56 +03:00
{
return implode("\n", array_map(function($value, $i) use ($options) {
return self::printDescription($options, $value, ' ', !$i) . ' ' .
2017-02-19 22:26:56 +03:00
$value->name . self::printDeprecated($value);
}, $values, array_keys($values)));
}
private static function printInputObject(InputObjectType $type, array $options)
2017-02-19 22:26:56 +03:00
{
$fields = array_values($type->getFields());
return self::printDescription($options, $type) .
2017-02-19 22:26:56 +03:00
"input {$type->name} {\n" .
implode("\n", array_map(function($f, $i) use ($options) {
return self::printDescription($options, $f, ' ', !$i) . ' ' . self::printInputValue($f);
2017-02-19 22:26:56 +03:00
}, $fields, array_keys($fields))) . "\n" .
"}";
}
private static function printFields($options, $type)
2017-02-19 22:26:56 +03:00
{
$fields = array_values($type->getFields());
return implode("\n", array_map(function($f, $i) use ($options) {
return self::printDescription($options, $f, ' ', !$i) . ' ' .
$f->name . self::printArgs($options, $f->args, ' ') . ': ' .
2017-02-19 22:26:56 +03:00
(string) $f->getType() . self::printDeprecated($f);
}, $fields, array_keys($fields)));
}
private static function printArgs($options, $args, $indentation = '')
2017-02-19 22:26:56 +03:00
{
if (!$args) {
2017-02-19 22:26:56 +03:00
return '';
}
// If every arg does not have a description, print them on one line.
if (Utils::every($args, function($arg) { return empty($arg->description); })) {
return '(' . implode(', ', array_map('self::printInputValue', $args)) . ')';
}
return "(\n" . implode("\n", array_map(function($arg, $i) use ($indentation, $options) {
return self::printDescription($options, $arg, ' ' . $indentation, !$i) . ' ' . $indentation .
2017-02-19 22:26:56 +03:00
self::printInputValue($arg);
}, $args, array_keys($args))) . "\n" . $indentation . ')';
}
private static function printInputValue($arg)
{
$argDecl = $arg->name . ': ' . (string) $arg->getType();
if ($arg->defaultValueExists()) {
$argDecl .= ' = ' . Printer::doPrint(AST::astFromValue($arg->defaultValue, $arg->getType()));
}
return $argDecl;
}
private static function printDirective($directive, $options)
2017-02-19 22:26:56 +03:00
{
return self::printDescription($options, $directive) .
'directive @' . $directive->name . self::printArgs($options, $directive->args) .
2017-02-19 22:26:56 +03:00
' on ' . implode(' | ', $directive->locations);
}
private static function printDeprecated($fieldOrEnumVal)
{
$reason = $fieldOrEnumVal->deprecationReason;
if (empty($reason)) {
return '';
}
if ($reason === '' || $reason === Directive::DEFAULT_DEPRECATION_REASON) {
return ' @deprecated';
}
return ' @deprecated(reason: ' .
Printer::doPrint(AST::astFromValue($reason, Type::string())) . ')';
}
private static function printDescription($options, $def, $indentation = '', $firstInBlock = true)
2017-02-19 22:26:56 +03:00
{
if (!$def->description) {
return '';
}
$lines = self::descriptionLines($def->description, 120 - strlen($indentation));
if (isset($options['commentDescriptions'])) {
return self::printDescriptionWithComments($lines, $indentation, $firstInBlock);
}
$description = ($indentation && !$firstInBlock) ? "\n" : '';
if (count($lines) === 1 && mb_strlen($lines[0]) < 70) {
$description .= $indentation . '"""' . self::escapeQuote($lines[0]) . "\"\"\"\n";
return $description;
}
$description .= $indentation . "\"\"\"\n";
foreach ($lines as $line) {
$description .= $indentation . self::escapeQuote($line) . "\n";
}
$description .= $indentation . "\"\"\"\n";
return $description;
}
private static function escapeQuote($line)
{
return str_replace('"""', '\\"""', $line);
}
private static function printDescriptionWithComments($lines, $indentation, $firstInBlock)
{
2017-02-19 22:26:56 +03:00
$description = $indentation && !$firstInBlock ? "\n" : '';
foreach ($lines as $line) {
if ($line === '') {
$description .= $indentation . "#\n";
} else {
$description .= $indentation . '# ' . $line . "\n";
}
}
return $description;
}
private static function descriptionLines($description, $maxLen) {
$lines = [];
$rawLines = explode("\n", $description);
foreach($rawLines as $line) {
if ($line === '') {
$lines[] = $line;
2017-02-19 22:26:56 +03:00
} else {
// For > 120 character long lines, cut at space boundaries into sublines
// of ~80 chars.
$sublines = self::breakLine($line, $maxLen);
2017-02-19 22:26:56 +03:00
foreach ($sublines as $subline) {
$lines[] = $subline;
2017-02-19 22:26:56 +03:00
}
}
}
return $lines;
2017-02-19 22:26:56 +03:00
}
private static function breakLine($line, $maxLen)
2017-02-19 22:26:56 +03:00
{
if (strlen($line) < $maxLen + 5) {
2017-02-19 22:26:56 +03:00
return [$line];
}
preg_match_all("/((?: |^).{15," . ($maxLen - 40) . "}(?= |$))/", $line, $parts);
2017-02-19 22:26:56 +03:00
$parts = $parts[0];
return array_map(function($part) {
return trim($part);
}, $parts);
}
}