Skip to content

Commit

Permalink
Add typehints to SchemaPrinter (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Mar 27, 2021
1 parent 1600f46 commit cb3211e
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/Utils/SchemaPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use GraphQL\Language\Printer;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\EnumValueDefinition;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
Expand Down Expand Up @@ -51,17 +55,19 @@ public static function doPrint(Schema $schema, array $options = []): string
{
return static::printFilteredSchema(
$schema,
static function ($type): bool {
return ! Directive::isSpecifiedDirective($type);
static function (Directive $directive): bool {
return ! Directive::isSpecifiedDirective($directive);
},
static function ($type): bool {
static function (Type $type): bool {
return ! Type::isBuiltInType($type);
},
$options
);
}

/**
* @param callable(Directive $directive): bool $directiveFilter
* @param callable(Type $type): bool $typeFilter
* @param array<string, bool> $options
*/
protected static function printFilteredSchema(Schema $schema, callable $directiveFilter, callable $typeFilter, array $options): string
Expand Down Expand Up @@ -165,11 +171,12 @@ protected static function printDirective(Directive $directive, array $options):
}

/**
* @param array<string, bool> $options
* @param array<string, bool> $options
* @param Type|Directive|EnumValueDefinition|FieldArgument|FieldDefinition|InputObjectField $def
*/
protected static function printDescription(array $options, $def, $indentation = '', $firstInBlock = true): string
protected static function printDescription(array $options, $def, string $indentation = '', bool $firstInBlock = true): string
{
if (! $def->description) {
if ($def->description === null || $def->description === '') {
return '';
}

Expand All @@ -178,7 +185,7 @@ protected static function printDescription(array $options, $def, $indentation =
return static::printDescriptionWithComments($lines, $indentation, $firstInBlock);
}

$description = $indentation && ! $firstInBlock
$description = $indentation !== '' && ! $firstInBlock
? "\n" . $indentation . '"""'
: $indentation . '"""';

Expand Down Expand Up @@ -216,7 +223,7 @@ protected static function printDescription(array $options, $def, $indentation =
}

/**
* @return string[]
* @return array<int, string>
*/
protected static function descriptionLines(string $description, int $maxLen): array
{
Expand All @@ -239,7 +246,7 @@ protected static function descriptionLines(string $description, int $maxLen): ar
}

/**
* @return string[]
* @return array<int, string>
*/
protected static function breakLine(string $line, int $maxLen): array
{
Expand All @@ -253,9 +260,12 @@ protected static function breakLine(string $line, int $maxLen): array
return array_map('trim', $parts);
}

protected static function printDescriptionWithComments($lines, $indentation, $firstInBlock): string
/**
* @param array<int, string> $lines
*/
protected static function printDescriptionWithComments(array $lines, string $indentation, bool $firstInBlock): string
{
$description = $indentation && ! $firstInBlock ? "\n" : '';
$description = $indentation !== '' && ! $firstInBlock ? "\n" : '';
foreach ($lines as $line) {
if ($line === '') {
$description .= $indentation . "#\n";
Expand All @@ -267,17 +277,18 @@ protected static function printDescriptionWithComments($lines, $indentation, $fi
return $description;
}

protected static function escapeQuote($line): string
protected static function escapeQuote(string $line): string
{
return str_replace('"""', '\\"""', $line);
}

/**
* @param array<string, bool> $options
* @param array<string, bool> $options
* @param array<int, FieldArgument> $args
*/
protected static function printArgs(array $options, $args, $indentation = ''): string
protected static function printArgs(array $options, array $args, string $indentation = ''): string
{
if (! $args) {
if (count($args) === 0) {
return '';
}

Expand All @@ -298,8 +309,8 @@ static function ($arg): bool {
implode(
"\n",
array_map(
static function ($arg, $i) use ($indentation, $options): string {
return static::printDescription($options, $arg, ' ' . $indentation, ! $i) . ' ' . $indentation .
static function (FieldArgument $arg, int $i) use ($indentation, $options): string {
return static::printDescription($options, $arg, ' ' . $indentation, $i === 0) . ' ' . $indentation .
static::printInputValue($arg);
},
$args,
Expand All @@ -310,6 +321,9 @@ static function ($arg, $i) use ($indentation, $options): string {
);
}

/**
* @param InputObjectField|FieldArgument $arg
*/
protected static function printInputValue($arg): string
{
$argDecl = $arg->name . ': ' . (string) $arg->getType();
Expand Down Expand Up @@ -383,7 +397,8 @@ static function (InterfaceType $interface): string {
}

/**
* @param array<string, bool> $options
* @param array<string, bool> $options
* @param ObjectType|InterfaceType $type
*/
protected static function printFields(array $options, $type): string
{
Expand All @@ -392,8 +407,8 @@ protected static function printFields(array $options, $type): string
return implode(
"\n",
array_map(
static function ($f, $i) use ($options): string {
return static::printDescription($options, $f, ' ', ! $i) . ' ' .
static function (FieldDefinition $f, int $i) use ($options): string {
return static::printDescription($options, $f, ' ', $i === 0) . ' ' .
$f->name . static::printArgs($options, $f->args, ' ') . ': ' .
(string) $f->getType() . static::printDeprecated($f);
},
Expand All @@ -403,6 +418,9 @@ static function ($f, $i) use ($options): string {
);
}

/**
* @param FieldArgument|EnumValueDefinition $fieldOrEnumVal
*/
protected static function printDeprecated($fieldOrEnumVal): string
{
$reason = $fieldOrEnumVal->deprecationReason;
Expand Down Expand Up @@ -459,15 +477,16 @@ protected static function printEnum(EnumType $type, array $options): string
}

/**
* @param array<string, bool> $options
* @param array<int, EnumValueDefinition> $values
* @param array<string, bool> $options
*/
protected static function printEnumValues($values, array $options): string
protected static function printEnumValues(array $values, array $options): string
{
return implode(
"\n",
array_map(
static function ($value, $i) use ($options): string {
return static::printDescription($options, $value, ' ', ! $i) . ' ' .
static function (EnumValueDefinition $value, int $i) use ($options): string {
return static::printDescription($options, $value, ' ', $i === 0) . ' ' .
$value->name . static::printDeprecated($value);
},
$values,
Expand Down

0 comments on commit cb3211e

Please sign in to comment.