Skip to content

Commit a21bfcc

Browse files
committed
[ISSUE-36] Allow to configure how deep the schema inspector should go for type/ofType sub-queries
1 parent 2b451e3 commit a21bfcc

File tree

6 files changed

+263
-46
lines changed

6 files changed

+263
-46
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ schema_object/*
55
!schema_object/.gitkeep
66
.phpunit.result.cache
77
/build/
8-
composer.lock
8+
composer.lock
9+
composer.phar

bin/generate_schema_objects

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@
44
use GraphQL\Client;
55
use GraphQL\SchemaGenerator\CodeGenerator\ObjectBuilderInterface;
66
use GraphQL\SchemaGenerator\SchemaClassGenerator;
7+
use GraphQL\SchemaGenerator\SchemaInspector\TypeSubQueryGenerator;
78

89
$autoLoadFiles = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php'];
910

10-
function readConfig()
11+
/**
12+
* @return array{
13+
* 0: string,
14+
* 1: string,
15+
* 2: array<string, string>,
16+
* 3: string,
17+
* 4: int,
18+
* }
19+
*/
20+
function readConfig(): array
1121
{
1222
$shortOptions = implode("", [
1323
'u:',
1424
'h:',
1525
'v:',
1626
'd:',
17-
'n:'
27+
'n:',
28+
'D:',
1829
]);
1930

2031
$longOptions = [
@@ -23,6 +34,7 @@ function readConfig()
2334
'authorization-header-value:',
2435
'directory:',
2536
'namespace:',
37+
'type-of-type-depth:',
2638
];
2739

2840
$options = getopt($shortOptions, $longOptions);
@@ -32,14 +44,21 @@ function readConfig()
3244
$authHeaderName = $options['authorization-header-name'] ?? $options['h'] ?? readline('Authorization header name: ');
3345
$authHeaderValue = $options['authorization-header-value'] ?? $options['v'] ?? readline('Authorization header value: ');
3446
$namespace = $options['n'] ?? $options['namespace'] ?? trim(readline('Custom namespace (optional): '));
47+
$typeOfTypeDepth = $options['type-of-type-depth'] ?? $options['D'] ?? 4;
3548

3649
$authHeaders = [];
3750

3851
if (!empty($authHeaderName)) {
3952
$authHeaders = [$authHeaderName => $authHeaderValue];
4053
}
4154

42-
return [$url, empty($customWriteDir) ? "" : $customWriteDir, $authHeaders, empty($namespace) ? ObjectBuilderInterface::DEFAULT_NAMESPACE : $namespace];
55+
return [
56+
$url,
57+
empty($customWriteDir) ? "" : $customWriteDir,
58+
$authHeaders,
59+
empty($namespace) ? ObjectBuilderInterface::DEFAULT_NAMESPACE : $namespace,
60+
$typeOfTypeDepth,
61+
];
4362
}
4463

4564
// Require autoload.php depending on environment
@@ -55,13 +74,14 @@ if (!$autoLoadFound) {
5574
throw new RuntimeException('Could not find vendor/autoload.php');
5675
}
5776

58-
[$endpointUrl, $customWriteDir, $authHeaders, $namespace] = readConfig();
77+
[$endpointUrl, $customWriteDir, $authHeaders, $namespace, $typeOfTypeDepth] = readConfig();
5978

6079
$client = new Client($endpointUrl, $authHeaders);
61-
$scanner = new SchemaClassGenerator($client, $customWriteDir, $namespace);
80+
$scanner = new SchemaClassGenerator($client, $customWriteDir, $namespace, new TypeSubQueryGenerator($typeOfTypeDepth));
6281

6382
print "-------------------------------------------\n";
6483
print "Generating schema objects from schema types\n";
84+
print "Using \"type / ofType\" depth: $typeOfTypeDepth\n";
6585
print "-------------------------------------------\n";
6686

6787
$scanner->generateRootQueryObject();

src/SchemaGenerator/SchemaClassGenerator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GraphQL\SchemaGenerator\CodeGenerator\ObjectBuilderInterface;
1111
use GraphQL\SchemaGenerator\CodeGenerator\QueryObjectClassBuilder;
1212
use GraphQL\SchemaGenerator\CodeGenerator\UnionObjectBuilder;
13+
use GraphQL\SchemaGenerator\SchemaInspector\TypeSubQueryGenerator;
1314
use GraphQL\SchemaObject\QueryObject;
1415
use GraphQL\Util\StringLiteralFormatter;
1516
use RuntimeException;
@@ -49,13 +50,14 @@ class SchemaClassGenerator
4950
/**
5051
* SchemaClassGenerator constructor.
5152
*
52-
* @param Client $client
53-
* @param string $writeDir
54-
* @param string $namespace
53+
* @param Client $client
54+
* @param string $writeDir
55+
* @param string $namespace
56+
* @param TypeSubQueryGenerator|null $typeSubQueryGenerate
5557
*/
56-
public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE)
58+
public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE, ?TypeSubQueryGenerator $typeSubQueryGenerate = null)
5759
{
58-
$this->schemaInspector = new SchemaInspector($client);
60+
$this->schemaInspector = new SchemaInspector($client, $typeSubQueryGenerate ?? new TypeSubQueryGenerator(4));
5961
$this->generatedObjects = [];
6062
$this->writeDir = $writeDir;
6163
$this->generationNamespace = $namespace;

src/SchemaGenerator/SchemaInspector.php

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GraphQL\SchemaGenerator;
44

55
use GraphQL\Client;
6+
use GraphQL\SchemaGenerator\SchemaInspector\TypeSubQueryGenerator;
67

78
/**
89
* Class SchemaInspector
@@ -13,45 +14,44 @@
1314
*/
1415
class SchemaInspector
1516
{
16-
private const TYPE_SUB_QUERY = <<<QUERY
17-
type{
18-
name
19-
kind
20-
description
21-
ofType{
22-
name
23-
kind
24-
ofType{
25-
name
26-
kind
27-
ofType{
28-
name
29-
kind
30-
ofType{
31-
name
32-
kind
33-
}
34-
}
35-
}
36-
}
37-
}
38-
QUERY;
39-
4017

4118
/**
4219
* @var Client
4320
*/
4421
protected $client;
4522

23+
/**
24+
* @var TypeSubQueryGenerator
25+
*/
26+
private $typeSubQueryGenerate;
27+
28+
4629
/**
4730
* SchemaInspector constructor.
4831
*
49-
* @param Client $client
32+
* @param Client $client
33+
* @param TypeSubQueryGenerator|null $typeSubQueryGenerate Generator of sub queries for types
5034
*/
51-
public function __construct(Client $client)
35+
public function __construct(Client $client, ?TypeSubQueryGenerator $typeSubQueryGenerate=null)
5236
{
5337
$this->client = $client;
38+
$this->typeSubQueryGenerate = ($typeSubQueryGenerate ?? new TypeSubQueryGenerator(4));
39+
40+
}
41+
42+
// End __construct().
43+
44+
45+
/**
46+
* @return string
47+
*/
48+
private function getTypeSubQuery(): string
49+
{
50+
return $this->typeSubQueryGenerate->getSubTypeQuery();
51+
5452
}
53+
// End getTypeSubQuery().
54+
5555

5656
/**
5757
* @return array
@@ -69,12 +69,12 @@ public function getQueryTypeSchema(): array
6969
description
7070
isDeprecated
7171
deprecationReason
72-
" . static::TYPE_SUB_QUERY . "
72+
".$this->getTypeSubQuery()."
7373
args{
7474
name
7575
description
7676
defaultValue
77-
" . static::TYPE_SUB_QUERY . "
77+
".$this->getTypeSubQuery()."
7878
}
7979
}
8080
}
@@ -83,10 +83,14 @@ public function getQueryTypeSchema(): array
8383
$response = $this->client->runRawQuery($schemaQuery, true);
8484

8585
return $response->getData()['__schema']['queryType'];
86+
8687
}
8788

89+
// End getQueryTypeSchema().
90+
91+
8892
/**
89-
* @param string $objectName
93+
* @param string $objectName The name of the object
9094
*
9195
* @return array
9296
*/
@@ -101,23 +105,27 @@ public function getObjectSchema(string $objectName): array
101105
description
102106
isDeprecated
103107
deprecationReason
104-
" . static::TYPE_SUB_QUERY . "
108+
".$this->getTypeSubQuery()."
105109
args{
106110
name
107111
description
108112
defaultValue
109-
" . static::TYPE_SUB_QUERY . "
113+
".$this->getTypeSubQuery()."
110114
}
111115
}
112116
}
113117
}";
114118
$response = $this->client->runRawQuery($schemaQuery, true);
115119

116120
return $response->getData()['__type'];
121+
117122
}
118123

124+
// End getObjectSchema().
125+
126+
119127
/**
120-
* @param string $objectName
128+
* @param string $objectName The name of the object
121129
*
122130
* @return array
123131
*/
@@ -131,17 +139,21 @@ public function getInputObjectSchema(string $objectName): array
131139
name
132140
description
133141
defaultValue
134-
" . static::TYPE_SUB_QUERY . "
142+
".$this->getTypeSubQuery()."
135143
}
136144
}
137145
}";
138146
$response = $this->client->runRawQuery($schemaQuery, true);
139147

140148
return $response->getData()['__type'];
149+
141150
}
142151

152+
// End getInputObjectSchema().
153+
154+
143155
/**
144-
* @param string $objectName
156+
* @param string $objectName The name of the enum object
145157
*
146158
* @return array
147159
*/
@@ -160,10 +172,14 @@ enumValues {
160172
$response = $this->client->runRawQuery($schemaQuery, true);
161173

162174
return $response->getData()['__type'];
175+
163176
}
164177

178+
// End getEnumObjectSchema().
179+
180+
165181
/**
166-
* @param string $objectName
182+
* @param string $objectName The name of the union object
167183
*
168184
* @return array
169185
*/
@@ -182,5 +198,10 @@ public function getUnionObjectSchema(string $objectName): array
182198
$response = $this->client->runRawQuery($schemaQuery, true);
183199

184200
return $response->getData()['__type'];
201+
185202
}
203+
204+
// End getUnionObjectSchema().
205+
206+
186207
}

0 commit comments

Comments
 (0)