Skip to content

Add schema name as Executor argument & fix default schema #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/annotations/annotations-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ The class exposing the mutation(s) must be declared as a [service](https://symfo

Optional attributes:

- **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).
- **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)).

Example:

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

Optional attributes:

- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type of the default schema).
- **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)).

Example:

Expand Down
4 changes: 4 additions & 0 deletions docs/definitions/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ overblog_graphql:
| batch request | `/graphql/bar/batch` |
| GraphiQL* | `/graphiql/bar` |

### Default schema

The schema considered as the default is the one with the name `default` if it exists, otherwise, it will be the first one defined.

\* `/graphiql` depends on [OverblogGraphiQLBundle](https://github.com/overblog/GraphiQLBundle)
9 changes: 5 additions & 4 deletions src/Config/Parser/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,20 @@ private static function typeAnnotationToGQLConfiguration(
): array {
$isMutation = $isDefault = $isRoot = false;
if (isset($configs['definitions']['schema'])) {
$defaultSchemaName = isset($configs['definitions']['schema']['default']) ? 'default' : array_key_first($configs['definitions']['schema']);
foreach ($configs['definitions']['schema'] as $schemaName => $schema) {
$schemaQuery = $schema['query'] ?? null;
$schemaMutation = $schema['mutation'] ?? null;

if ($schemaQuery && $gqlName === $schemaQuery) {
if ($gqlName === $schemaQuery) {
$isRoot = true;
if ('default' == $schemaName) {
if ($defaultSchemaName === $schemaName) {
$isDefault = true;
}
} elseif ($schemaMutation && $gqlName === $schemaMutation) {
} elseif ($gqlName === $schemaMutation) {
$isMutation = true;
$isRoot = true;
if ('default' == $schemaName) {
if ($defaultSchemaName === $schemaName) {
$isDefault = true;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/DataCollector/GraphQLCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ public function collect(Request $request, Response $response, Throwable $excepti
{
$error = false;
$count = 0;
$schema = null;
foreach ($this->batches as $batch) {
if (!$schema) {
$schema = $batch['schema'];
}
if (isset($batch['error'])) {
$error = true;
}
$count += $batch['count'];
}

$this->data = [
'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default',
'schema' => $schema,
'batches' => $this->batches,
'count' => $count,
'error' => $error,
Expand All @@ -62,9 +66,9 @@ public function getCount(): int
/**
* Return the targeted schema.
*/
public function getSchema(): string
public function getSchema(): ?string
{
return $this->data['schema'] ?? 'default';
return $this->data['schema'];
}

/**
Expand Down Expand Up @@ -105,6 +109,7 @@ public function onPostExecutor(ExecutorResultEvent $event): void
$result = $event->getResult()->toArray();

$batch = [
'schema' => $executorArgument->getSchemaName(),
'queryString' => $queryString,
'queryTime' => $queryTime,
'variables' => $this->cloneVar($variables),
Expand Down
13 changes: 13 additions & 0 deletions src/Event/ExecutorArgumentsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

final class ExecutorArgumentsEvent extends Event
{
private string $schemaName;
private ExtensibleSchema $schema;
private string $requestString;
private ArrayObject $contextValue;
Expand All @@ -27,6 +28,7 @@ final class ExecutorArgumentsEvent extends Event
* @return static
*/
public static function create(
string $schemaName,
ExtensibleSchema $schema,
string $requestString,
ArrayObject $contextValue,
Expand All @@ -35,6 +37,7 @@ public static function create(
string $operationName = null
): self {
$instance = new static();
$instance->setSchemaName($schemaName);
$instance->setSchema($schema);
$instance->setRequestString($requestString);
$instance->setContextValue($contextValue);
Expand All @@ -46,6 +49,11 @@ public static function create(
return $instance;
}

public function setSchemaName(string $schemaName): void
{
$this->schemaName = $schemaName;
}

public function setOperationName(?string $operationName): void
{
$this->operationName = $operationName;
Expand Down Expand Up @@ -84,6 +92,11 @@ public function setStartTime(float $startTime): void
$this->startTime = $startTime;
}

public function getSchemaName(): string
{
return $this->schemaName;
}

public function getSchema(): ExtensibleSchema
{
return $this->schema;
Expand Down
20 changes: 12 additions & 8 deletions src/Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Overblog\GraphQLBundle\Definition\Type\ExtensibleSchema;
use Overblog\GraphQLBundle\Event\Events;
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
Expand Down Expand Up @@ -84,14 +83,13 @@ public function getSchema(string $name = null): Schema
}

if (null === $name) {
// TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
foreach ($this->schemas as $name => $schema) {
break;
}
$name = isset($this->schemas['default']) ? 'default' : array_key_first($this->schemas);
}

if (!isset($this->schemas[$name])) {
throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name));
}

$schema = $this->schemas[$name];
if (is_callable($schema)) {
$schema = $schema();
Expand Down Expand Up @@ -137,8 +135,13 @@ public function execute(?string $schemaName, array $request, $rootValue = null):
{
$this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();

$schema = $this->getSchema($schemaName);
/** @var string $schemaName */
$schemaName = array_search($schema, $this->schemas);

$executorArgumentsEvent = $this->preExecute(
$this->getSchema($schemaName),
$schemaName,
$schema,
$request[ParserInterface::PARAM_QUERY] ?? null,
new ArrayObject(),
$rootValue,
Expand Down Expand Up @@ -168,6 +171,7 @@ public function execute(?string $schemaName, array $request, $rootValue = null):
* @param mixed $rootValue
*/
private function preExecute(
string $schemaName,
Schema $schema,
string $requestString,
ArrayObject $contextValue,
Expand All @@ -181,8 +185,8 @@ private function preExecute(
/** @var ExecutorArgumentsEvent $object */
// @phpstan-ignore-next-line (only for Symfony 4.4)
$object = $this->dispatcher->dispatch(
/** @var ExtensibleSchema $schema */
ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
// @phpstan-ignore-next-line
ExecutorArgumentsEvent::create($schemaName, $schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
Events::PRE_EXECUTOR
);

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/profiler/graphql.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
{{ result.status_code|default('n/a') }}
</span>
<span class="nowrap newline">{{ result.time|date }}</span>
{% if schemas|length > 0 %}
{% if schemas|length > 0 and graphql.schema %}
<span class="label schema-name">schema: {{ graphql.schema }}</span>
{% endif %}
<a href="{{ path('_profiler', { token: result.token, panel: 'graphql' }) }}#{{result.token}}">{{ result.token }}</a>
Expand Down
7 changes: 3 additions & 4 deletions tests/DataCollector/GraphQLCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@ public function testCollect(): void
$collector = new GraphQLCollector();

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

$collector->onPostExecutor(new ExecutorResultEvent(
new ExecutionResult(['res' => 'ok', 'error' => 'my error']),
ExecutorArgumentsEvent::create(new ExtensibleSchema([]), 'invalid', new ArrayObject())
ExecutorArgumentsEvent::create('test_schema', new ExtensibleSchema([]), 'invalid', new ArrayObject())
));

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

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

$this->assertEquals($collector->getSchema(), 'myschema');
$this->assertEquals($collector->getSchema(), 'test_schema');
$this->assertEquals($collector->getName(), 'graphql');
$this->assertEquals($collector->getCount(), 1);
$this->assertTrue($collector->getError());
Expand Down