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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ enum SearchType: string
// Returns results matching issues in repositories.
case IssueAdvanced = 'ISSUE_ADVANCED';

// Returns results matching issues using hybrid (lexical + semantic) search.
case IssueHybrid = 'ISSUE_HYBRID';

// Returns results matching issues using semantic search.
case IssueSemantic = 'ISSUE_SEMANTIC';

// Returns results matching repositories.
case Repository = 'REPOSITORY';

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": "^8.4",
"composer-runtime-api": "^2.0",
"nikic/php-parser": "^5.6",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan": "^2.1.18",
"ruudk/code-generator": "^0.4.4",
"symfony/console": "^7.4 || ^8.0",
"symfony/filesystem": "^7.4 || ^8.0",
Expand Down
15 changes: 5 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/Generated/Enum/SearchType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ enum SearchType: string
// Returns results matching issues in repositories.
case IssueAdvanced = 'ISSUE_ADVANCED';

// Returns results matching issues using hybrid (lexical + semantic) search.
case IssueHybrid = 'ISSUE_HYBRID';

// Returns results matching issues using semantic search.
case IssueSemantic = 'ISSUE_SEMANTIC';

// Returns results matching repositories.
case Repository = 'REPOSITORY';

Expand Down
2 changes: 2 additions & 0 deletions lefthook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pre-commit:

php-cs-fixer:
glob: "*.php"
exclude:
- "**/Generated/**"
run: vendor/bin/php-cs-fixer fix --config .php-cs-fixer.php -- {staged_files}
stage_fixed: true

Expand Down
8 changes: 8 additions & 0 deletions phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
__DIR__ . '/tests/*/Generated/*',
],
],
[
'identifiers' => [
'shipmonk.deadMethod',
],
'paths' => [
__DIR__ . '/tests/PHPStan/Fixtures/*',
],
],
[
'identifiers' => [
'shipmonk.deadConstant',
Expand Down
13 changes: 11 additions & 2 deletions src/Generator/DataClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
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\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\TwigFileSource;
use Ruudk\GraphQLCodeGenerator\Type\FragmentObjectType;
use Ruudk\GraphQLCodeGenerator\Type\IndexByCollectionType;
use Ruudk\GraphQLCodeGenerator\Type\StringLiteralType;
Expand Down Expand Up @@ -86,12 +87,20 @@ public function generate(DataClassPlan $plan) : string

if ($this->config->addGeneratedAttribute) {
yield from $generator->dumpAttribute(Generated::class, function () use ($generator, $plan) {
if ($plan->source instanceof FileSource) {
if ($plan->source instanceof GraphQLFileSource) {
yield sprintf('source: %s', var_export($plan->source->relativeFilePath, true));

return;
}

if ($plan->source instanceof TwigFileSource) {
yield sprintf('source: %s', var_export($plan->source->relativeFilePath, true));
yield 'restricted: true';
yield 'restrictInstantiation: true';

return;
}

yield sprintf('source: %s', $generator->dumpClassReference($plan->source->class));
yield 'restricted: true';
yield 'restrictInstantiation: true';
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/OperationClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Ruudk\CodeGenerator\CodeGenerator;
use Ruudk\GraphQLCodeGenerator\Attribute\Generated;
use Ruudk\GraphQLCodeGenerator\Planner\Plan\OperationClassPlan;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Type\TypeDumper;
use Symfony\Component\TypeInfo\Type as SymfonyType;
use Symfony\Component\TypeInfo\TypeIdentifier;
Expand Down Expand Up @@ -36,7 +36,7 @@ public function generate(OperationClassPlan $plan) : string

if ($this->config->addGeneratedAttribute) {
yield from $generator->dumpAttribute(Generated::class, function () use ($generator, $plan) {
if ($plan->source instanceof FileSource) {
if ($plan->source instanceof GraphQLFileSource) {
yield sprintf('source: %s', var_export($plan->source->relativeFilePath, true));

return;
Expand Down
7 changes: 4 additions & 3 deletions src/GraphQL/DocumentNodeWithSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\NodeList;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\TwigFileSource;

final class DocumentNodeWithSource extends DocumentNode
{
public FileSource | InlineSource $source;
public GraphQLFileSource | InlineSource | TwigFileSource $source;

public static function create(DocumentNode $documentNode, FileSource | InlineSource $source) : self
public static function create(DocumentNode $documentNode, GraphQLFileSource | InlineSource | TwigFileSource $source) : self
{
$definitions = [];
foreach ($documentNode->definitions as $definition) {
Expand Down
7 changes: 4 additions & 3 deletions src/GraphQL/FragmentDefinitionNodeWithSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Ruudk\GraphQLCodeGenerator\GraphQL;

use GraphQL\Language\AST\FragmentDefinitionNode;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\TwigFileSource;

final class FragmentDefinitionNodeWithSource extends FragmentDefinitionNode
{
public FileSource | InlineSource $source;
public GraphQLFileSource | InlineSource | TwigFileSource $source;

public static function create(FragmentDefinitionNode $fragmentNode, FileSource | InlineSource $source) : self
public static function create(FragmentDefinitionNode $fragmentNode, GraphQLFileSource | InlineSource | TwigFileSource $source) : self
{
return new self([
'name' => $fragmentNode->name,
Expand Down
4 changes: 4 additions & 0 deletions src/PHP/Visitor/OperationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function enterNode(Node $node) : null
return null;
}

if ($this->className === null) {
return null;
}

foreach ($node->params as $param) {
if ( ! $param->var instanceof Node\Expr\Variable) {
continue;
Expand Down
23 changes: 15 additions & 8 deletions src/Planner.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@
use Ruudk\GraphQLCodeGenerator\Planner\Plan\OperationClassPlan;
use Ruudk\GraphQLCodeGenerator\Planner\PlannerResult;
use Ruudk\GraphQLCodeGenerator\Planner\SelectionSetPlanner;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\TwigFileSource;
use Ruudk\GraphQLCodeGenerator\Twig\GraphQLExtension;
use Ruudk\GraphQLCodeGenerator\Twig\GraphQLNodeFinder;
use Ruudk\GraphQLCodeGenerator\Type\TypeHelper;
Expand Down Expand Up @@ -252,7 +253,7 @@ public function plan() : PlannerResult

$operations[$file->getPathname()][] = DocumentNodeWithSource::create(
$document,
new FileSource(Path::makeRelative($file->getPathname(), $this->config->projectDir)),
new GraphQLFileSource(Path::makeRelative($file->getPathname(), $this->config->projectDir)),
);
}

Expand Down Expand Up @@ -344,7 +345,7 @@ public function plan() : PlannerResult

$operations[$file->getPathname()][] = DocumentNodeWithSource::create(
$document,
new FileSource(Path::makeRelative($file->getPathname(), $this->config->projectDir)),
new TwigFileSource(Path::makeRelative($file->getPathname(), $this->config->projectDir)),
);
}
}
Expand Down Expand Up @@ -615,14 +616,20 @@ private function planOperation(DocumentNodeWithSource $document, string $path, P

Assert::notNull($operation->name, 'Expected operation to have a name');

if ($document->source instanceof TwigFileSource) {
throw new Exception('Twig templates may only contain fragment definitions');
}

$source = $document->source;

$operationName = $operation->name->value;
$operationNamespaceName = $operationName;

if ($document->source instanceof InlineSource) {
if ($source instanceof InlineSource) {
$operationNamespaceName = sprintf(
'%s%s',
$operationName,
$document->source->hash,
$source->hash,
);
}

Expand Down Expand Up @@ -650,7 +657,7 @@ private function planOperation(DocumentNodeWithSource $document, string $path, P

// Plan the data class and its nested classes
$planResult = $planner->plan(
$document->source,
$source,
$operation->selectionSet,
$rootType,
$operationDir . '/Data',
Expand All @@ -660,7 +667,7 @@ private function planOperation(DocumentNodeWithSource $document, string $path, P

// Create the data class plan
$dataClassPlan = new DataClassPlan(
$document->source,
$source,
$operationDir . '/Data.php',
$fqcn,
$rootType,
Expand All @@ -684,7 +691,7 @@ private function planOperation(DocumentNodeWithSource $document, string $path, P
$operationType,
$operationDefinition,
$variables,
$document->source,
$source,
);

// Create the error class plan
Expand Down
5 changes: 3 additions & 2 deletions src/Planner/Plan/DataClassPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\Type;
use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\TwigFileSource;
use Symfony\Component\TypeInfo\Type as SymfonyType;

final readonly class DataClassPlan
Expand All @@ -20,7 +21,7 @@
* @param array<string, list<string>> $inlineFragmentRequiredFields
*/
public function __construct(
public FileSource | InlineSource $source,
public GraphQLFileSource | InlineSource | TwigFileSource $source,
public string $path,
public string $fqcn,
public NamedType & Type $parentType,
Expand Down
4 changes: 2 additions & 2 deletions src/Planner/Plan/OperationClassPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ruudk\GraphQLCodeGenerator\Planner\Plan;

use Ruudk\GraphQLCodeGenerator\Planner\Source\FileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\GraphQLFileSource;
use Ruudk\GraphQLCodeGenerator\Planner\Source\InlineSource;
use Symfony\Component\TypeInfo\Type as SymfonyType;

Expand All @@ -21,6 +21,6 @@ public function __construct(
public string $operationType,
public string $operationDefinition,
public array $variables,
public FileSource | InlineSource $source,
public GraphQLFileSource | InlineSource $source,
) {}
}
Loading
Loading