Skip to content

Commit c8a183a

Browse files
committed
Use operation name for generated operation name
This allows writing things like: ```php private const string OPERATION = <<<'GRAPHQL' query ViewerProjects { viewer { login projects { name description } } } GRAPHQL; public function __construct( #[GeneratedGraphQLClient(self::OPERATION)] public ViewerProjectsQuery $query, ) {} ``` As you can see, the class is now called `ViewerProjectsQuery` which looks a lot better than `ViewerProjects1d8480Query`.
1 parent 89bb242 commit c8a183a

File tree

66 files changed

+274
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+274
-229
lines changed

examples/Generated/Query/SearchQuery.php renamed to examples/Generated/Query/Search/SearchQuery.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
declare(strict_types=1);
44

5-
namespace Ruudk\GraphQLCodeGenerator\Examples\Generated\Query;
5+
namespace Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Search;
66

7-
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Search\Data;
87
use Ruudk\GraphQLCodeGenerator\Examples\GitHubClient;
98

109
// This file was automatically generated and should not be edited.

examples/Generated/Query/ViewerQuery.php renamed to examples/Generated/Query/Viewer/ViewerQuery.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
declare(strict_types=1);
44

5-
namespace Ruudk\GraphQLCodeGenerator\Examples\Generated\Query;
5+
namespace Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Viewer;
66

7-
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Viewer\Data;
87
use Ruudk\GraphQLCodeGenerator\Examples\GitHubClient;
98

109
// This file was automatically generated and should not be edited.

examples/run.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
require_once __DIR__ . '/../vendor/autoload.php';
66

77
use Http\Discovery\Psr18ClientDiscovery;
8-
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\SearchQuery;
9-
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\ViewerQuery;
8+
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Search\SearchQuery;
9+
use Ruudk\GraphQLCodeGenerator\Examples\Generated\Query\Viewer\ViewerQuery;
1010
use Ruudk\GraphQLCodeGenerator\Examples\GitHubClient;
1111
use Symfony\Component\Dotenv\Dotenv;
1212
use Webmozart\Assert\Assert;
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Attribute;
88

99
#[Attribute(flags: Attribute::TARGET_CLASS)]
10-
final readonly class GeneratedFrom
10+
final readonly class Generated
1111
{
1212
public function __construct(
1313
public string $source,
14-
public bool $restrict = false,
15-
) {}
14+
public bool $restricted = false,
15+
public bool $restrictInstantiation = false,
16+
) {
17+
}
18+
1619
}

src/Config/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private function __construct(
4242
public bool $useEdgeNameForEdges = false,
4343
public bool $addNodesOnConnections = false,
4444
public bool $addSymfonyExcludeAttribute = false,
45-
public bool $addGeneratedFromAttribute = false,
45+
public bool $addGeneratedAttribute = false,
4646
public bool $indexByDirective = false,
4747
public bool $addUnknownCaseToEnums = false,
4848
public bool $dumpEnumIsMethods = false,
@@ -103,9 +103,9 @@ public function enableSymfonyExcludeAttribute() : self
103103
return $this->with('addSymfonyExcludeAttribute', true);
104104
}
105105

106-
public function enableGeneratedFromAttribute() : self
106+
public function enableGeneratedAttribute() : self
107107
{
108-
return $this->with('addGeneratedFromAttribute', true);
108+
return $this->with('addGeneratedAttribute', true);
109109
}
110110

111111
public function enableIndexByDirective() : self

src/Generator/DataClassGenerator.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
use GraphQL\Type\Definition\ObjectType;
1010
use GraphQL\Type\Definition\UnionType;
1111
use Ruudk\CodeGenerator\CodeGenerator;
12-
use Ruudk\GraphQLCodeGenerator\Attribute\GeneratedFrom;
12+
use Ruudk\GraphQLCodeGenerator\Attribute\Generated;
1313
use Ruudk\GraphQLCodeGenerator\Config\Config;
1414
use Ruudk\GraphQLCodeGenerator\Planner\Plan\DataClassPlan;
15+
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
1516
use Ruudk\GraphQLCodeGenerator\Type\FragmentObjectType;
1617
use Ruudk\GraphQLCodeGenerator\Type\IndexByCollectionType;
1718
use Ruudk\GraphQLCodeGenerator\Type\StringLiteralType;
@@ -83,16 +84,17 @@ public function generate(DataClassPlan $plan) : string
8384
yield from $generator->dumpAttribute('Symfony\Component\DependencyInjection\Attribute\Exclude');
8485
}
8586

86-
if ($this->config->addGeneratedFromAttribute) {
87-
yield from $generator->dumpAttribute(GeneratedFrom::class, function () use ($generator, $plan) {
88-
if (str_ends_with($plan->source, '.graphql')) {
89-
yield sprintf('source: %s', var_export($plan->source, true));
87+
if ($this->config->addGeneratedAttribute) {
88+
yield from $generator->dumpAttribute(Generated::class, function () use ($generator, $plan) {
89+
if ($plan->source instanceof FileSource) {
90+
yield sprintf('source: %s', var_export($plan->source->relativeFilePath, true));
9091

9192
return;
9293
}
9394

94-
yield sprintf('source: %s', $generator->dumpClassReference($plan->source));
95-
yield 'restrict: true';
95+
yield sprintf('source: %s', $generator->dumpClassReference($plan->source->class));
96+
yield 'restricted: true';
97+
yield 'restrictInstantiation: true';
9698
});
9799
}
98100

src/Generator/ErrorClassGenerator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ final class ErrorClassGenerator extends AbstractGenerator
1313
{
1414
public function generate(ErrorClassPlan $plan) : string
1515
{
16-
$operationType = $plan->operationType;
17-
$operationName = $plan->operationName;
18-
$generator = new CodeGenerator($this->fullyQualified($operationType, $operationName));
16+
$generator = new CodeGenerator($plan->namespace);
1917

2018
return $generator->dumpFile(function () use ($generator) {
2119
yield $this->dumpHeader();

src/Generator/ExceptionClassGenerator.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,22 @@ final class ExceptionClassGenerator extends AbstractGenerator
1212
{
1313
public function generate(ExceptionClassPlan $plan) : string
1414
{
15-
$operationType = $plan->operationType;
16-
$operationName = $plan->operationName;
17-
$className = $plan->exceptionClassName;
18-
$generator = new CodeGenerator($this->fullyQualified($operationType, $operationName));
15+
$generator = new CodeGenerator($plan->namespace);
1916

20-
return $generator->dumpFile(function () use ($className, $generator) {
17+
return $generator->dumpFile(function () use ($plan, $generator) {
2118
yield $this->dumpHeader();
2219

2320
yield '';
24-
yield sprintf('final class %s extends %s', $className, $generator->import(Exception::class));
21+
yield sprintf('final class %s extends %s', $plan->className, $generator->import(Exception::class));
2522
yield '{';
26-
yield $generator->indent(function () use ($generator, $className) {
23+
yield $generator->indent(function () use ($generator, $plan) {
2724
yield 'public function __construct(';
2825
yield $generator->indent('public readonly Data $data,');
2926
yield ') {';
30-
yield $generator->indent(function () use ($generator, $className) {
27+
yield $generator->indent(function () use ($generator, $plan) {
3128
yield 'parent::__construct(sprintf(';
3229
yield $generator->indent([
33-
sprintf("'%s failed%%s',", $className),
30+
sprintf("'%s failed%%s',", $plan->className),
3431
"\$data->errors !== [] ? sprintf(': %s', \$data->errors[0]->message) : '',",
3532
]);
3633
yield '));';

src/Generator/OperationClassGenerator.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
use JsonException;
88
use Ruudk\CodeGenerator\CodeGenerator;
9-
use Ruudk\GraphQLCodeGenerator\Attribute\GeneratedFrom;
9+
use Ruudk\GraphQLCodeGenerator\Attribute\Generated;
1010
use Ruudk\GraphQLCodeGenerator\Planner\Plan\OperationClassPlan;
11+
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
1112
use Ruudk\GraphQLCodeGenerator\Type\TypeDumper;
1213
use Symfony\Component\TypeInfo\Type as SymfonyType;
1314

@@ -24,12 +25,12 @@ public function generate(OperationClassPlan $plan) : string
2425
$variables[$name] = $variable['type'];
2526
}
2627

27-
$namespace = $this->fullyQualified($plan->operationType);
28-
$className = $plan->queryClassName . $plan->operationType;
28+
$namespace = $this->fullyQualified($plan->operationType, $plan->operationNamepaceName);
29+
$className = $plan->className;
2930
$failedException = $this->fullyQualified(
3031
$plan->operationType,
31-
$plan->queryClassName,
32-
$plan->queryClassName . $plan->operationType . 'FailedException',
32+
$plan->operationNamepaceName,
33+
$plan->operationNamepaceName . $plan->operationType . 'FailedException',
3334
);
3435

3536
$generator = new CodeGenerator($namespace);
@@ -39,16 +40,16 @@ public function generate(OperationClassPlan $plan) : string
3940

4041
yield '';
4142

42-
if ($this->config->addGeneratedFromAttribute) {
43-
yield from $generator->dumpAttribute(GeneratedFrom::class, function () use ($generator, $plan) {
44-
if (str_ends_with($plan->source, '.graphql')) {
45-
yield sprintf('source: %s', var_export($plan->source, true));
43+
if ($this->config->addGeneratedAttribute) {
44+
yield from $generator->dumpAttribute(Generated::class, function () use ($generator, $plan) {
45+
if ($plan->source instanceof FileSource) {
46+
yield sprintf('source: %s', var_export($plan->source->relativeFilePath, true));
4647

4748
return;
4849
}
4950

50-
yield sprintf('source: %s', $generator->dumpClassReference($plan->source));
51-
yield 'restrict: true';
51+
yield sprintf('source: %s', $generator->dumpClassReference($plan->source->class));
52+
yield 'restricted: true';
5253
});
5354
}
5455

@@ -99,12 +100,12 @@ function () use ($plan, $failedException, $namespace, $variables, $generator) {
99100
yield $parameters;
100101
yield sprintf(
101102
') : %s {',
102-
$generator->import(sprintf($namespace . '\\%s\Data', $plan->queryClassName)),
103+
$generator->import($namespace . '\\Data'),
103104
);
104105
} else {
105106
yield sprintf(
106107
'public function execute() : %s',
107-
$generator->import(sprintf($namespace . '\\%s\Data', $plan->queryClassName)),
108+
$generator->import($namespace . '\\Data'),
108109
);
109110
yield '{';
110111
}
@@ -156,12 +157,12 @@ function () use ($plan, $failedException, $namespace, $variables, $generator) {
156157
yield $parameters;
157158
yield sprintf(
158159
') : %s {',
159-
$generator->import(sprintf($namespace . '\\%s\Data', $plan->queryClassName)),
160+
$generator->import($namespace . '\\Data'),
160161
);
161162
} else {
162163
yield sprintf(
163164
'public function executeOrThrow() : %s',
164-
$generator->import(sprintf($namespace . '\\%s\Data', $plan->queryClassName)),
165+
$generator->import($namespace . '\\Data'),
165166
);
166167
yield '{';
167168
}

src/GraphQL/DocumentNodeWithSource.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use GraphQL\Language\AST\DocumentNode;
88
use GraphQL\Language\AST\FragmentDefinitionNode;
99
use GraphQL\Language\AST\NodeList;
10+
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
11+
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
1012

1113
final class DocumentNodeWithSource extends DocumentNode
1214
{
13-
public string $source;
15+
public FileSource | InlineSource $source;
1416

15-
public static function create(DocumentNode $documentNode, string $source) : self
17+
public static function create(DocumentNode $documentNode, FileSource | InlineSource $source) : self
1618
{
1719
$definitions = [];
1820
foreach ($documentNode->definitions as $definition) {

0 commit comments

Comments
 (0)