Add predicates to for built-in types

ref: graphql/graphql-js#924
This commit is contained in:
Daniel Tschinder 2018-02-09 18:03:31 +01:00
parent c4f11a577e
commit 1da3801614
5 changed files with 69 additions and 57 deletions

View File

@ -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
*/

View File

@ -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

View File

@ -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'])) {

View File

@ -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();
}
/**

View File

@ -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)],