Skip to content
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
1 change: 1 addition & 0 deletions src/DataCollector/GraphqlOrmDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function addQuery(
): void {
$this->data['queries'][] = [
'graphql' => $trace->graphql,
'ast' => $trace->ast,
'variables' => $trace->variables,
'endpoint' => $trace->endpoint,
'caller' => $trace->caller,
Expand Down
27 changes: 5 additions & 22 deletions src/Dialect/DataApiBuilderDialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace GraphqlOrm\Dialect;

use GraphqlOrm\Query\Walker\DABGraphqlWalker;
use GraphqlOrm\Query\Walker\GraphqlWalkerInterface;

final class DataApiBuilderDialect implements GraphqlQueryDialect
{
public function wrapCollection(string $selection, int $indentLevel): string
{
$indent = str_repeat(' ', $indentLevel);
$innerIndent = str_repeat(' ', $indentLevel - 1);

$selection = $this->indent($selection, $innerIndent);

return <<<GQL
{$indent}items {
$selection
{$indent}}
GQL;
}

public function extractCollection(array $data): array
{
/** @var array<string|int, mixed> $items */
Expand All @@ -28,14 +17,8 @@ public function extractCollection(array $data): array
return $items;
}

private function indent(string $text, string $indent): string
public function createWalker(): GraphqlWalkerInterface
{
return implode(
"\n",
array_map(
fn ($line) => $line !== '' ? $indent . $line : $line,
explode("\n", $text)
)
);
return new DABGraphqlWalker();
}
}
11 changes: 7 additions & 4 deletions src/Dialect/DefaultDialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

namespace GraphqlOrm\Dialect;

use GraphqlOrm\Query\Walker\DefaultGraphqlWalker;
use GraphqlOrm\Query\Walker\GraphqlWalkerInterface;

final class DefaultDialect implements GraphqlQueryDialect
{
public function wrapCollection(string $selection, int $indentLevel): string
public function extractCollection(array $data): array
{
return $selection;
return $data;
}

public function extractCollection(array $data): array
public function createWalker(): GraphqlWalkerInterface
{
return $data;
return new DefaultGraphqlWalker();
}
}
6 changes: 4 additions & 2 deletions src/Dialect/GraphqlQueryDialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace GraphqlOrm\Dialect;

use GraphqlOrm\Query\Walker\GraphqlWalkerInterface;

interface GraphqlQueryDialect
{
public function wrapCollection(string $selection, int $indentLevel): string;

/**
* @param array<string, mixed> $data
*
* @return array<string|int, mixed>
*/
public function extractCollection(array $data): array;

public function createWalker(): GraphqlWalkerInterface;
}
9 changes: 9 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace GraphqlOrm\Exception;

final class LogicException extends \LogicException implements GraphqlOrmException
{
}
36 changes: 29 additions & 7 deletions src/GraphqlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use GraphqlOrm\Execution\GraphqlExecutionContext;
use GraphqlOrm\Hydrator\EntityHydrator;
use GraphqlOrm\Metadata\GraphqlEntityMetadataFactory;
use GraphqlOrm\Query\Ast\QueryNode;
use GraphqlOrm\Query\GraphqlQueryCompiler;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

/**
Expand All @@ -30,6 +32,7 @@ public function __construct(
public GraphqlOrmDataCollector $collector,
public int $maxDepth,
public GraphqlQueryDialect $dialect = new DefaultDialect(),
private ?GraphqlQueryCompiler $compiler = null,
) {
}

Expand All @@ -38,19 +41,25 @@ public function __construct(
*
* @return T[]
*/
public function execute(
string $graphql,
callable $hydration,
array $variables = [],
): array {
public function execute(QueryNode|string $graphql, callable $hydration, array $variables = [], ): array
{
$context = new GraphqlExecutionContext();

$context->trace->graphql = $graphql;
if ($graphql instanceof QueryNode) {
$compiled = $this->getQueryCompiler()->compile($graphql);
/** @var array<string|int, mixed> $ast */
$ast = json_decode(json_encode($graphql, JSON_THROW_ON_ERROR), true);
$context->trace->ast = $ast;
} else {
$compiled = $graphql;
}

$context->trace->graphql = $compiled;

try {
$result = $this->client
->query(
$graphql,
$compiled,
$context,
$variables
);
Expand All @@ -74,4 +83,17 @@ public function getDialect(): GraphqlQueryDialect
{
return $this->dialect;
}

public function getQueryCompiler(): GraphqlQueryCompiler
{
if ($this->compiler !== null) {
return $this->compiler;
}

$walker = $this->dialect->createWalker();

$this->compiler = new GraphqlQueryCompiler($walker);

return $this->compiler;
}
}
18 changes: 18 additions & 0 deletions src/Query/Ast/FieldNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace GraphqlOrm\Query\Ast;

final class FieldNode
{
/**
* @param array<string, mixed> $arguments
*/
public function __construct(
public string $name,
public array $arguments = [],
public ?SelectionSetNode $selectionSet = null,
) {
}
}
13 changes: 13 additions & 0 deletions src/Query/Ast/QueryNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace GraphqlOrm\Query\Ast;

final class QueryNode
{
public string $operation = 'query';

/** @var FieldNode[] */
public array $fields = [];
}
16 changes: 16 additions & 0 deletions src/Query/Ast/SelectionSetNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace GraphqlOrm\Query\Ast;

final class SelectionSetNode
{
/** @var FieldNode[] */
public array $fields = [];

public function add(FieldNode $field): void
{
$this->fields[] = $field;
}
}
7 changes: 6 additions & 1 deletion src/Query/GraphqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GraphqlOrm\Exception\InvalidGraphqlResponseException;
use GraphqlOrm\GraphqlManager;
use GraphqlOrm\Query\Ast\QueryNode;

/**
* @template T of object
Expand All @@ -17,14 +18,18 @@
* @param GraphqlManager<T> $manager
*/
public function __construct(
private string $graphql,
private QueryNode|string $graphql,
private string $entityClass,
private GraphqlManager $manager,
) {
}

public function getGraphQL(): string
{
if ($this->graphql instanceof QueryNode) {
return $this->manager->getQueryCompiler()->compile($this->graphql);
}

return $this->graphql;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Query/GraphqlQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public function getQuery(): GraphqlQuery

$manualSelect = $this->selectedFields !== null;

$graphql = (new GraphqlQueryStringBuilder($this->manager))
$ast = (new GraphqlQueryStringBuilder($this->manager))
->entity($this->entityClass)
->root($metadata->name)
->arguments($this->criteria)
->fields($fields, $manualSelect)
->build();

return new GraphqlQuery(
$graphql,
$ast,
$this->entityClass,
$this->manager
);
Expand Down
21 changes: 21 additions & 0 deletions src/Query/GraphqlQueryCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace GraphqlOrm\Query;

use GraphqlOrm\Query\Ast\QueryNode;
use GraphqlOrm\Query\Walker\GraphqlWalkerInterface;

readonly class GraphqlQueryCompiler
{
public function __construct(
private GraphqlWalkerInterface $walker,
) {
}

public function compile(QueryNode $node): string
{
return $this->walker->walk($node);
}
}
Loading