Skip to content

Commit 9fc5e9c

Browse files
committed
Add schema name as Executor argument & fix default schema
- Add schemaName in the ExecutorArgumentsEvent - Make the GraphQL collector retrieve schema from ExecutorArgument instead of routing - Consider default schema as the one with name 'default' or the first one defined - Apply same logic to Annotation Parser - Update doc
1 parent 7d433d6 commit 9fc5e9c

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

docs/annotations/annotations-reference.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ The class exposing the mutation(s) must be declared as a [service](https://symfo
386386

387387
Optional attributes:
388388

389-
- **targetType** : The GraphQL type to attach the field to. It must be a mutation. (by default, it'll be the root Mutation type of the default schema).
389+
- **targetType** : The GraphQL type to attach the field to. It must be a mutation. (by default, it'll be the root Mutation type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)).
390390

391391
Example:
392392

@@ -436,7 +436,7 @@ The class exposing the query(ies) must be declared as a [service](https://symfon
436436

437437
Optional attributes:
438438

439-
- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type of the default schema).
439+
- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)).
440440

441441
Example:
442442

docs/definitions/schema.md

+4
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ overblog_graphql:
111111
| batch request | `/graphql/bar/batch` |
112112
| GraphiQL* | `/graphiql/bar` |
113113

114+
### Default schema
115+
116+
The schema considered as the default is the one with the name `default` if it exists, otherwise, it will be the first one defined.
117+
114118
\* `/graphiql` depends on [OverblogGraphiQLBundle](https://github.com/overblog/GraphiQLBundle)

src/Config/Parser/AnnotationParser.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,20 @@ private static function typeAnnotationToGQLConfiguration(
253253
): array {
254254
$isMutation = $isDefault = $isRoot = false;
255255
if (isset($configs['definitions']['schema'])) {
256+
$defaultSchemaName = isset($configs['definitions']['schema']['default']) ? 'default' : array_key_first($configs['definitions']['schema']);
256257
foreach ($configs['definitions']['schema'] as $schemaName => $schema) {
257258
$schemaQuery = $schema['query'] ?? null;
258259
$schemaMutation = $schema['mutation'] ?? null;
259260

260261
if ($schemaQuery && $gqlName === $schemaQuery) {
261262
$isRoot = true;
262-
if ('default' == $schemaName) {
263+
if ($defaultSchemaName == $schemaName) {
263264
$isDefault = true;
264265
}
265266
} elseif ($schemaMutation && $gqlName === $schemaMutation) {
266267
$isMutation = true;
267268
$isRoot = true;
268-
if ('default' == $schemaName) {
269+
if ($defaultSchemaName == $schemaName) {
269270
$isDefault = true;
270271
}
271272
}

src/DataCollector/GraphQLCollector.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ public function collect(Request $request, Response $response, Throwable $excepti
2828
{
2929
$error = false;
3030
$count = 0;
31+
$schema = false;
3132
foreach ($this->batches as $batch) {
33+
if (!$schema) {
34+
$schema = $batch['schema'];
35+
}
3236
if (isset($batch['error'])) {
3337
$error = true;
3438
}
3539
$count += $batch['count'];
3640
}
3741

3842
$this->data = [
39-
'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default',
43+
'schema' => $schema,
4044
'batches' => $this->batches,
4145
'count' => $count,
4246
'error' => $error,
@@ -105,6 +109,7 @@ public function onPostExecutor(ExecutorResultEvent $event): void
105109
$result = $event->getResult()->toArray();
106110

107111
$batch = [
112+
'schema' => $executorArgument->getSchemaName(),
108113
'queryString' => $queryString,
109114
'queryTime' => $queryTime,
110115
'variables' => $this->cloneVar($variables),

src/Event/ExecutorArgumentsEvent.php

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
final class ExecutorArgumentsEvent extends Event
1313
{
14+
private string $schemaName;
1415
private ExtensibleSchema $schema;
1516
private string $requestString;
1617
private ArrayObject $contextValue;
@@ -27,6 +28,7 @@ final class ExecutorArgumentsEvent extends Event
2728
* @return static
2829
*/
2930
public static function create(
31+
string $schemaName,
3032
ExtensibleSchema $schema,
3133
string $requestString,
3234
ArrayObject $contextValue,
@@ -35,6 +37,7 @@ public static function create(
3537
string $operationName = null
3638
): self {
3739
$instance = new static();
40+
$instance->setSchemaName($schemaName);
3841
$instance->setSchema($schema);
3942
$instance->setRequestString($requestString);
4043
$instance->setContextValue($contextValue);
@@ -46,6 +49,11 @@ public static function create(
4649
return $instance;
4750
}
4851

52+
public function setSchemaName(string $schemaName): void
53+
{
54+
$this->schemaName = $schemaName;
55+
}
56+
4957
public function setOperationName(?string $operationName): void
5058
{
5159
$this->operationName = $operationName;
@@ -84,6 +92,11 @@ public function setStartTime(float $startTime): void
8492
$this->startTime = $startTime;
8593
}
8694

95+
public function getSchemaName(): string
96+
{
97+
return $this->schemaName;
98+
}
99+
87100
public function getSchema(): ExtensibleSchema
88101
{
89102
return $this->schema;

src/Request/Executor.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ public function getSchema(string $name = null): Schema
8484
}
8585

8686
if (null === $name) {
87-
// TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
88-
foreach ($this->schemas as $name => $schema) {
89-
break;
90-
}
87+
$name = isset($this->schemas['default']) ? 'default' : array_key_first($this->schemas);
9188
}
89+
9290
if (!isset($this->schemas[$name])) {
9391
throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name));
9492
}
93+
9594
$schema = $this->schemas[$name];
9695
if (is_callable($schema)) {
9796
$schema = $schema();
@@ -137,8 +136,12 @@ public function execute(?string $schemaName, array $request, $rootValue = null):
137136
{
138137
$this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
139138

139+
$schema = $this->getSchema($schemaName);
140+
$schemaName = array_search($schema, $this->schemas);
141+
140142
$executorArgumentsEvent = $this->preExecute(
141-
$this->getSchema($schemaName),
143+
$schemaName,
144+
$schema,
142145
$request[ParserInterface::PARAM_QUERY] ?? null,
143146
new ArrayObject(),
144147
$rootValue,
@@ -168,6 +171,7 @@ public function execute(?string $schemaName, array $request, $rootValue = null):
168171
* @param mixed $rootValue
169172
*/
170173
private function preExecute(
174+
string $schemaName,
171175
Schema $schema,
172176
string $requestString,
173177
ArrayObject $contextValue,
@@ -182,7 +186,7 @@ private function preExecute(
182186
// @phpstan-ignore-next-line (only for Symfony 4.4)
183187
$object = $this->dispatcher->dispatch(
184188
/** @var ExtensibleSchema $schema */
185-
ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
189+
ExecutorArgumentsEvent::create($schemaName, $schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
186190
Events::PRE_EXECUTOR
187191
);
188192

src/Resources/views/profiler/graphql.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
{{ result.status_code|default('n/a') }}
115115
</span>
116116
<span class="nowrap newline">{{ result.time|date }}</span>
117-
{% if schemas|length > 0 %}
117+
{% if schemas|length > 0 and graphql.schema %}
118118
<span class="label schema-name">schema: {{ graphql.schema }}</span>
119119
{% endif %}
120120
<a href="{{ path('_profiler', { token: result.token, panel: 'graphql' }) }}#{{result.token}}">{{ result.token }}</a>

tests/DataCollector/GraphQLCollectorTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ public function testCollect(): void
2222
$collector = new GraphQLCollector();
2323

2424
$request = new Request();
25-
$request->attributes->set('_route_params', ['schemaName' => 'myschema']);
2625

2726
$collector->onPostExecutor(new ExecutorResultEvent(
2827
new ExecutionResult(['res' => 'ok', 'error' => 'my error']),
29-
ExecutorArgumentsEvent::create(new ExtensibleSchema([]), 'invalid', new ArrayObject())
28+
ExecutorArgumentsEvent::create('test_schema', new ExtensibleSchema([]), 'invalid', new ArrayObject())
3029
));
3130

3231
$collector->onPostExecutor(new ExecutorResultEvent(
3332
new ExecutionResult(['res' => 'ok', 'error' => 'my error']),
34-
ExecutorArgumentsEvent::create(new ExtensibleSchema([]), 'query{ myalias: test{field1, field2} }', new ArrayObject(), null, ['variable1' => 'v1'])
33+
ExecutorArgumentsEvent::create('test_schema', new ExtensibleSchema([]), 'query{ myalias: test{field1, field2} }', new ArrayObject(), null, ['variable1' => 'v1'])
3534
));
3635

3736
$collector->collect($request, new Response());
3837

39-
$this->assertEquals($collector->getSchema(), 'myschema');
38+
$this->assertEquals($collector->getSchema(), 'test_schema');
4039
$this->assertEquals($collector->getName(), 'graphql');
4140
$this->assertEquals($collector->getCount(), 1);
4241
$this->assertTrue($collector->getError());

0 commit comments

Comments
 (0)