-
-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathSchemaGenerator.php
More file actions
168 lines (144 loc) · 5.03 KB
/
SchemaGenerator.php
File metadata and controls
168 lines (144 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types=1);
namespace GraphQL\Benchmarks\Utils;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Type\SchemaConfig;
class SchemaGenerator
{
/** @var array<string, int> */
private array $config = [
'totalTypes' => 100,
'nestingLevel' => 10,
'fieldsPerType' => 10,
'listFieldsPerType' => 2,
];
private int $typeIndex = 0;
/** @var array<string, ObjectType> */
private array $objectTypes = [];
/** @param array<string, int> $config */
public function __construct(array $config)
{
$this->config = \array_merge($this->config, $config);
}
public function buildSchema(): Schema
{
return new Schema(
(new SchemaConfig())
->setQuery($this->buildQueryType())
);
}
public function buildQueryType(): ObjectType
{
$this->typeIndex = 0;
$this->objectTypes = [];
return $this->createType(0);
}
public function loadType(string $name): ObjectType
{
$tokens = \explode('_', $name);
$nestingLevel = (int) $tokens[1];
return $this->createType($nestingLevel, $name);
}
protected function createType(int $nestingLevel, ?string $typeName = null): ObjectType
{
if ($this->typeIndex > $this->config['totalTypes']) {
throw new \Exception("Cannot create new type: there are already {$this->typeIndex} types which exceeds allowed number of {$this->config['totalTypes']} types total");
}
++$this->typeIndex;
if ($typeName === null) {
$typeName = 'Level_' . $nestingLevel . '_Type' . $this->typeIndex;
}
$type = new ObjectType([
'name' => $typeName,
'fields' => fn (): array => $this->createTypeFields($typeName, $nestingLevel + 1),
]);
$this->objectTypes[$typeName] = $type;
return $type;
}
/** @return array{0: Type, 1: string} */
protected function getFieldTypeAndName(int $nestingLevel, int $fieldIndex): array
{
if ($nestingLevel >= $this->config['nestingLevel']) {
$fieldType = Type::string();
$fieldName = 'leafField' . $fieldIndex;
} elseif ($this->typeIndex >= $this->config['totalTypes']) {
$fieldType = $this->objectTypes[\array_rand($this->objectTypes)];
$fieldName = 'randomTypeField' . $fieldIndex;
} else {
$fieldType = $this->createType($nestingLevel);
$fieldName = 'field' . $fieldIndex;
}
return [$fieldType, $fieldName];
}
/** @return array<int, array<string, mixed>> */
protected function createTypeFields(string $typeName, int $nestingLevel): array
{
$fields = [];
for ($index = 0; $index < $this->config['fieldsPerType']; ++$index) {
[$type, $name] = $this->getFieldTypeAndName($nestingLevel, $index);
$fields[] = [
'name' => $name,
'type' => $type,
'resolve' => [$this, 'resolveField'],
];
}
for ($index = 0; $index < $this->config['listFieldsPerType']; ++$index) {
[$type, $name] = $this->getFieldTypeAndName($nestingLevel, $index);
$name = 'listOf' . \ucfirst($name);
$fields[] = [
'name' => $name,
'type' => Type::listOf($type),
'args' => $this->createFieldArgs($name, $typeName),
'resolve' => static fn (): array => [
'string1',
'string2',
'string3',
'string4',
'string5',
],
];
}
return $fields;
}
/** @return array<string, mixed> */
protected function createFieldArgs(string $fieldName, string $typeName): array
{
return [
'argString' => [
'type' => Type::string(),
],
'argEnum' => [
'type' => new EnumType([
'name' => $typeName . $fieldName . 'Enum',
'values' => [
'ONE',
'TWO',
'THREE',
],
]),
],
'argInputObject' => [
'type' => new InputObjectType([
'name' => $typeName . $fieldName . 'Input',
'fields' => [
'field1' => Type::string(),
'field2' => Type::int(),
],
]),
],
];
}
/**
* @param mixed $root
* @param array<string, mixed> $args
* @param mixed $context
*/
public function resolveField($root, array $args, $context, ResolveInfo $resolveInfo): string
{
return $resolveInfo->fieldName . '-value';
}
}