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
6 changes: 6 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function __construct(
public bool $dumpEnumIsMethods = false,
public ?object $introspectionClient = null,
public array $inlineProcessingDirectories = [],
public bool $formatOperationFiles = false,
) {}

public static function create(
Expand Down Expand Up @@ -123,6 +124,11 @@ public function enableDumpEnumIsMethods() : self
return $this->with('dumpEnumIsMethods', true);
}

public function enableFormatOperationFiles() : self
{
return $this->with('formatOperationFiles', true);
}

public function withScalar(string $name, Type $type, ?Type $payloadType = null) : self
{
$scalars = $this->scalars;
Expand Down
24 changes: 22 additions & 2 deletions src/Executor/PlanExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ruudk\GraphQLCodeGenerator\Executor;

use GraphQL\Error\SyntaxError;
use GraphQL\Language\Parser as GraphQLParser;
use JsonException;
use LogicException;
use PhpParser\NodeTraverser;
Expand All @@ -12,6 +14,7 @@
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use RuntimeException;
use Ruudk\GraphQLCodeGenerator\Config\Config;
use Ruudk\GraphQLCodeGenerator\Generator\DataClassGenerator;
use Ruudk\GraphQLCodeGenerator\Generator\EnumTypeGenerator;
Expand All @@ -20,6 +23,7 @@
use Ruudk\GraphQLCodeGenerator\Generator\InputTypeGenerator;
use Ruudk\GraphQLCodeGenerator\Generator\NodeNotFoundExceptionGenerator;
use Ruudk\GraphQLCodeGenerator\Generator\OperationClassGenerator;
use Ruudk\GraphQLCodeGenerator\GraphQL\AST\Printer;
use Ruudk\GraphQLCodeGenerator\PHP\Visitor\OperationInjector;
use Ruudk\GraphQLCodeGenerator\PHP\Visitor\UseStatementInserter;
use Ruudk\GraphQLCodeGenerator\Planner\OperationPlan;
Expand All @@ -36,6 +40,7 @@
use Ruudk\GraphQLCodeGenerator\TypeInitializer\ObjectTypeInitializer;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Webmozart\Assert\Assert;

final class PlanExecutor
Expand All @@ -51,7 +56,7 @@ final class PlanExecutor
private Filesystem $filesystem;

public function __construct(
Config $config,
private Config $config,
) {
// Initialize type initializer
$typeInitializer = new DelegatingTypeInitializer(
Expand All @@ -76,9 +81,11 @@ public function __construct(
}

/**
* @throws LogicException
* @throws JsonException
* @throws IOException
* @throws SyntaxError
* @throws RuntimeException
* @throws LogicException
* @return array<string, string>
*/
public function execute(PlannerResult $plan) : array
Expand All @@ -95,6 +102,19 @@ public function execute(PlannerResult $plan) : array
}
}

if ($this->config->formatOperationFiles) {
$finder = Finder::create()->files()
->in($this->config->queriesDir)
->name('*.graphql')
->sortByName();

foreach ($finder as $file) {
$document = GraphQLParser::parse($file->getContents());

$this->filesystem->dumpFile($file->getPathname(), Printer::doPrint($document));
}
}

$printer = new Standard();
foreach ($plan->operationsToInject as $path => $operations) {
$oldStmts = $this->phpParser->parse($this->filesystem->readFile($path));
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/DataClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Ruudk\GraphQLCodeGenerator\Generator;

use GraphQL\Language\Printer;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\UnionType;
use Ruudk\CodeGenerator\CodeGenerator;
use Ruudk\GraphQLCodeGenerator\Attribute\Generated;
use Ruudk\GraphQLCodeGenerator\Config\Config;
use Ruudk\GraphQLCodeGenerator\GraphQL\AST\Printer;
use Ruudk\GraphQLCodeGenerator\Planner\Plan\DataClassPlan;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Type\FragmentObjectType;
Expand Down
28 changes: 28 additions & 0 deletions src/GraphQL/AST/Printer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\GraphQL\AST;

use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\OperationDefinitionNode;
use Override;

final class Printer extends \GraphQL\Language\Printer
{
#[Override]
protected static function p(?Node $node) : string
{
if ($node instanceof OperationDefinitionNode) {
$value = parent::p($node);

if (str_starts_with($value, '{')) {
return 'query ' . $value;
}

return $value;
}

return parent::p($node);
}
}
11 changes: 4 additions & 7 deletions src/PHP/Visitor/OperationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace Ruudk\GraphQLCodeGenerator\PHP\Visitor;

use InvalidArgumentException;
use Override;
use PhpParser\NameContext;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitorAbstract;
use Ruudk\GraphQLCodeGenerator\Attribute\GeneratedGraphQLClient;
use Webmozart\Assert\InvalidArgumentException;

final class OperationFinder extends NodeVisitorAbstract
{
Expand All @@ -26,17 +24,16 @@ final class OperationFinder extends NodeVisitorAbstract
*/
public function __construct(
private array $constants,
private NameContext $context,
) {}

/**
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
#[Override]
public function enterNode(Node $node) : null
{
if ($node instanceof Node\Stmt\Class_ && $node->name !== null) {
$this->className = Name::concat($this->context->getNamespace(), (string) $node->name)?->toString();
$this->className = $node->namespacedName?->toString();

return null;
}
Expand All @@ -56,7 +53,7 @@ public function enterNode(Node $node) : null

foreach ($param->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($this->context->getResolvedClassName($attr->name)->toString() !== GeneratedGraphQLClient::class) {
if ($attr->name->toString() !== GeneratedGraphQLClient::class) {
continue;
}

Expand Down
14 changes: 4 additions & 10 deletions src/Planner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\NamedType;
Expand Down Expand Up @@ -59,6 +58,7 @@
use PhpParser\ParserFactory;
use Ruudk\GraphQLCodeGenerator\Attribute\GeneratedGraphQLClient;
use Ruudk\GraphQLCodeGenerator\Config\Config;
use Ruudk\GraphQLCodeGenerator\GraphQL\AST\Printer;
use Ruudk\GraphQLCodeGenerator\GraphQL\DocumentNodeWithSource;
use Ruudk\GraphQLCodeGenerator\GraphQL\FragmentDefinitionNodeWithSource;
use Ruudk\GraphQLCodeGenerator\GraphQL\PossibleTypesFinder;
Expand Down Expand Up @@ -264,15 +264,9 @@ public function plan() : PlannerResult
Assert::notNull($stmts, 'Failed to parse PHP file');

$classConstants = new ClassConstantFinder();
$nameResolver = new NameResolver(options: [
'replaceNodes' => false,
]);
new NodeTraverser($nameResolver, $classConstants)->traverse($stmts);

$operationFinder = new OperationFinder(
$classConstants->constants,
$nameResolver->getNameContext(),
);
$stmts = new NodeTraverser(new NameResolver(), $classConstants)->traverse($stmts);

$operationFinder = new OperationFinder($classConstants->constants);
new NodeTraverser($operationFinder)->traverse($stmts);

foreach ($operationFinder->operations as $className => $methods) {
Expand Down
12 changes: 6 additions & 6 deletions tests/Simple/Schema.graphql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type Query {
viewer: Viewer!
viewer: Viewer!
}

type Viewer {
login: String!
projects: [Project!]!
login: String!
projects: [Project!]!
}

type Project {
id: ID!
name: String!
description: String
id: ID!
name: String!
description: String
}
9 changes: 9 additions & 0 deletions tests/Simple/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

namespace Ruudk\GraphQLCodeGenerator\Simple;

use Override;
use Ruudk\GraphQLCodeGenerator\Config\Config;
use Ruudk\GraphQLCodeGenerator\GraphQLTestCase;
use Ruudk\GraphQLCodeGenerator\Simple\Generated\Query\Test\TestQuery;

final class SimpleTest extends GraphQLTestCase
{
#[Override]
public function getConfig() : Config
{
return parent::getConfig()
->enableFormatOperationFiles();
}

public function testGenerate() : void
{
$this->assertActualMatchesExpected();
Expand Down
12 changes: 6 additions & 6 deletions tests/Simple/Test.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
query Test {
viewer {
login
projects {
name
description
}
viewer {
login
projects {
name
description
}
}
}