diff --git a/src/Type/Definition/Directive.php b/src/Type/Definition/Directive.php index 546f02d..63ea075 100644 --- a/src/Type/Definition/Directive.php +++ b/src/Type/Definition/Directive.php @@ -74,6 +74,15 @@ class Directive return $internal['deprecated']; } + /** + * @param Directive $directive + * @return bool + */ + public static function isSpecifiedDirective(Directive $directive) + { + return in_array($directive->name, array_keys(self::getInternalDirectives())); + } + /** * @return array */ diff --git a/src/Type/Definition/Type.php b/src/Type/Definition/Type.php index 2b2c83f..89bac9b 100644 --- a/src/Type/Definition/Type.php +++ b/src/Type/Definition/Type.php @@ -2,7 +2,9 @@ namespace GraphQL\Type\Definition; use GraphQL\Error\InvariantViolation; +use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\TypeDefinitionNode; +use GraphQL\Type\Introspection; /** * Registry of standard GraphQL types @@ -23,6 +25,11 @@ abstract class Type implements \JsonSerializable */ private static $internalTypes; + /** + * @var array + */ + private static $builtInTypes; + /** * @api * @return IDType @@ -107,6 +114,8 @@ abstract class Type implements \JsonSerializable } /** + * Returns all builtin scalar types + * * @return Type[] */ public static function getInternalTypes() @@ -114,6 +123,34 @@ abstract class Type implements \JsonSerializable return self::getInternalType(); } + /** + * Returns all builtin in types including base scalar and + * introspection types + * + * @return Type[] + */ + public static function getAllBuiltInTypes() + { + if (null === self::$builtInTypes) { + self::$builtInTypes = array_merge( + Introspection::getTypes(), + self::getInternalTypes() + ); + } + return self::$builtInTypes; + } + + /** + * Checks if the type is a builtin type + * + * @param Type $type + * @return bool + */ + public static function isBuiltInType(Type $type) + { + return in_array($type->name, array_keys(self::getAllBuiltInTypes())); + } + /** * @api * @param Type $type diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index 7f0734d..3c87a90 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -240,6 +240,15 @@ EOD; ]; } + /** + * @param Type $type + * @return bool + */ + public static function isIntrospectionType(Type $type) + { + return in_array($type->name, array_keys(self::getTypes())); + } + public static function _schema() { if (!isset(self::$map['__Schema'])) { diff --git a/src/Utils/ASTDefinitionBuilder.php b/src/Utils/ASTDefinitionBuilder.php index 7541633..ed73dd5 100644 --- a/src/Utils/ASTDefinitionBuilder.php +++ b/src/Utils/ASTDefinitionBuilder.php @@ -61,21 +61,7 @@ class ASTDefinitionBuilder $this->options = $options; $this->resolveType = $resolveType; - $this->cache = [ - 'String' => Type::string(), - 'Int' => Type::int(), - 'Float' => Type::float(), - 'Boolean' => Type::boolean(), - 'ID' => Type::id(), - '__Schema' => Introspection::_schema(), - '__Directive' => Introspection::_directive(), - '__DirectiveLocation' => Introspection::_directiveLocation(), - '__Type' => Introspection::_type(), - '__Field' => Introspection::_field(), - '__InputValue' => Introspection::_inputValue(), - '__EnumValue' => Introspection::_enumValue(), - '__TypeKind' => Introspection::_typeKind(), - ]; + $this->cache = Type::getAllBuiltInTypes(); } /** diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index 93c667e..d80e104 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -2,8 +2,8 @@ namespace GraphQL\Utils; use GraphQL\Language\Printer; +use GraphQL\Type\Introspection; use GraphQL\Type\Schema; -use GraphQL\Type\Definition\CompositeType; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; @@ -31,10 +31,12 @@ class SchemaPrinter { return self::printFilteredSchema( $schema, - function($n) { - return !self::isSpecDirective($n); + function($type) { + return !Directive::isSpecifiedDirective($type); + }, + function ($type) { + return !Type::isBuiltInType($type); }, - 'self::isDefinedType', $options ); } @@ -48,51 +50,20 @@ class SchemaPrinter { return self::printFilteredSchema( $schema, - [__CLASS__, 'isSpecDirective'], - [__CLASS__, 'isIntrospectionType'], + [Directive::class, 'isSpecifiedDirective'], + [Introspection::class, 'isIntrospectionType'], $options ); } - private static function isSpecDirective($directiveName) - { - return ( - $directiveName === 'skip' || - $directiveName === 'include' || - $directiveName === 'deprecated' - ); - } - - private static function isDefinedType($typename) - { - return !self::isIntrospectionType($typename) && !self::isBuiltInScalar($typename); - } - - private static function isIntrospectionType($typename) - { - return strpos($typename, '__') === 0; - } - - private static function isBuiltInScalar($typename) - { - return ( - $typename === Type::STRING || - $typename === Type::BOOLEAN || - $typename === Type::INT || - $typename === Type::FLOAT || - $typename === Type::ID - ); - } - private static function printFilteredSchema(Schema $schema, $directiveFilter, $typeFilter, $options) { $directives = array_filter($schema->getDirectives(), function($directive) use ($directiveFilter) { - return $directiveFilter($directive->name); + return $directiveFilter($directive); }); - $typeMap = $schema->getTypeMap(); - $types = array_filter(array_keys($typeMap), $typeFilter); - sort($types); - $types = array_map(function($typeName) use ($typeMap) { return $typeMap[$typeName]; }, $types); + $types = $schema->getTypeMap(); + ksort($types); + $types = array_filter($types, $typeFilter); return implode("\n\n", array_filter(array_merge( [self::printSchemaDefinition($schema)], @@ -364,4 +335,4 @@ class SchemaPrinter return trim($part); }, $parts); } -} \ No newline at end of file +}