graphql-php/tests/Validator/QuerySecuritySchema.php

114 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2018-09-02 14:08:49 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Validator;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-02 14:08:49 +03:00
use GraphQL\Type\Schema;
class QuerySecuritySchema
{
2018-09-02 14:08:49 +03:00
/** @var Schema */
private static $schema;
2018-09-02 14:08:49 +03:00
/** @var ObjectType */
private static $dogType;
2018-09-02 14:08:49 +03:00
/** @var ObjectType */
private static $humanType;
2018-09-02 14:08:49 +03:00
/** @var ObjectType */
private static $queryRootType;
/**
* @return Schema
*/
public static function buildSchema()
{
2018-09-02 14:08:49 +03:00
if (self::$schema !== null) {
return self::$schema;
}
self::$schema = new Schema([
2018-09-02 14:08:49 +03:00
'query' => static::buildQueryRootType(),
]);
return self::$schema;
}
public static function buildQueryRootType()
{
2018-09-02 14:08:49 +03:00
if (self::$queryRootType !== null) {
return self::$queryRootType;
}
self::$queryRootType = new ObjectType([
2018-09-02 14:08:49 +03:00
'name' => 'QueryRoot',
'fields' => [
'human' => [
'type' => self::buildHumanType(),
'args' => ['name' => ['type' => Type::string()]],
],
],
]);
return self::$queryRootType;
}
public static function buildHumanType()
{
2018-09-02 14:08:49 +03:00
if (self::$humanType !== null) {
return self::$humanType;
}
self::$humanType = new ObjectType(
[
2018-09-02 14:08:49 +03:00
'name' => 'Human',
2018-09-26 12:07:23 +03:00
'fields' => static function () {
return [
'firstName' => ['type' => Type::nonNull(Type::string())],
2018-09-02 14:08:49 +03:00
'dogs' => [
'type' => Type::nonNull(
Type::listOf(
Type::nonNull(self::buildDogType())
)
),
2018-09-26 12:07:23 +03:00
'complexity' => static function ($childrenComplexity, $args) {
$complexity = isset($args['name']) ? 1 : 10;
return $childrenComplexity + $complexity;
},
2018-09-02 14:08:49 +03:00
'args' => ['name' => ['type' => Type::string()]],
],
];
},
]
);
return self::$humanType;
}
public static function buildDogType()
{
2018-09-02 14:08:49 +03:00
if (self::$dogType !== null) {
return self::$dogType;
}
self::$dogType = new ObjectType(
[
2018-09-02 14:08:49 +03:00
'name' => 'Dog',
'fields' => [
2018-09-02 14:08:49 +03:00
'name' => ['type' => Type::nonNull(Type::string())],
'master' => [
'type' => self::buildHumanType(),
],
],
]
);
return self::$dogType;
}
}