webonyx /
graphql-php
| 1 | <?php |
||
| 2 | namespace GraphQL\Benchmarks\Utils; |
||
| 3 | |||
| 4 | use GraphQL\Language\AST\DocumentNode; |
||
| 5 | use GraphQL\Language\AST\FieldNode; |
||
| 6 | use GraphQL\Language\AST\NameNode; |
||
| 7 | use GraphQL\Language\AST\OperationDefinitionNode; |
||
| 8 | use GraphQL\Language\AST\SelectionSetNode; |
||
| 9 | use GraphQL\Language\Printer; |
||
| 10 | use GraphQL\Type\Definition\FieldDefinition; |
||
| 11 | use GraphQL\Type\Definition\InterfaceType; |
||
| 12 | use GraphQL\Type\Definition\ObjectType; |
||
| 13 | use GraphQL\Type\Definition\WrappingType; |
||
| 14 | use GraphQL\Type\Schema; |
||
| 15 | use GraphQL\Utils\Utils; |
||
| 16 | use function count; |
||
| 17 | use function max; |
||
| 18 | use function round; |
||
| 19 | |||
| 20 | class QueryGenerator |
||
| 21 | { |
||
| 22 | private $schema; |
||
| 23 | |||
| 24 | private $maxLeafFields; |
||
| 25 | |||
| 26 | private $currentLeafFields; |
||
| 27 | |||
| 28 | public function __construct(Schema $schema, $percentOfLeafFields) |
||
| 29 | { |
||
| 30 | $this->schema = $schema; |
||
| 31 | |||
| 32 | Utils::invariant(0 < $percentOfLeafFields && $percentOfLeafFields <= 1); |
||
| 33 | |||
| 34 | $totalFields = 0; |
||
| 35 | foreach ($schema->getTypeMap() as $type) { |
||
| 36 | if (! ($type instanceof ObjectType)) { |
||
| 37 | continue; |
||
| 38 | } |
||
| 39 | |||
| 40 | $totalFields += count($type->getFields()); |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->maxLeafFields = max(1, round($totalFields * $percentOfLeafFields)); |
||
| 44 | $this->currentLeafFields = 0; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function buildQuery() |
||
| 48 | { |
||
| 49 | $qtype = $this->schema->getQueryType(); |
||
| 50 | |||
| 51 | $ast = new DocumentNode([ |
||
| 52 | 'definitions' => [new OperationDefinitionNode([ |
||
| 53 | 'name' => new NameNode(['value' => 'TestQuery']), |
||
| 54 | 'operation' => 'query', |
||
| 55 | 'selectionSet' => $this->buildSelectionSet($qtype->getFields()), |
||
| 56 | ]), |
||
| 57 | ], |
||
| 58 | ]); |
||
| 59 | |||
| 60 | return Printer::doPrint($ast); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param FieldDefinition[] $fields |
||
| 65 | * |
||
| 66 | * @return SelectionSetNode |
||
| 67 | */ |
||
| 68 | public function buildSelectionSet($fields) |
||
| 69 | { |
||
| 70 | $selections[] = new FieldNode([ |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 71 | 'name' => new NameNode(['value' => '__typename']), |
||
| 72 | ]); |
||
| 73 | $this->currentLeafFields++; |
||
| 74 | |||
| 75 | foreach ($fields as $field) { |
||
| 76 | if ($this->currentLeafFields >= $this->maxLeafFields) { |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | |||
| 80 | $type = $field->getType(); |
||
| 81 | |||
| 82 | if ($type instanceof WrappingType) { |
||
| 83 | $type = $type->getWrappedType(true); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($type instanceof ObjectType || $type instanceof InterfaceType) { |
||
| 87 | $selectionSet = $this->buildSelectionSet($type->getFields()); |
||
| 88 | } else { |
||
| 89 | $selectionSet = null; |
||
| 90 | $this->currentLeafFields++; |
||
| 91 | } |
||
| 92 | |||
| 93 | $selections[] = new FieldNode([ |
||
| 94 | 'name' => new NameNode(['value' => $field->name]), |
||
| 95 | 'selectionSet' => $selectionSet, |
||
| 96 | ]); |
||
| 97 | } |
||
| 98 | |||
| 99 | $selectionSet = new SelectionSetNode([ |
||
| 100 | 'selections' => $selections, |
||
| 101 | ]); |
||
| 102 | |||
| 103 | return $selectionSet; |
||
| 104 | } |
||
| 105 | } |
||
| 106 |