|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\GraphQL\Directives; |
| 6 | + |
| 7 | +use GraphQL\Error\SyntaxError; |
| 8 | +use GraphQL\Language\AST\FieldDefinitionNode; |
| 9 | +use GraphQL\Language\AST\InterfaceTypeDefinitionNode; |
| 10 | +use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
| 11 | +use GraphQL\Language\Parser; |
| 12 | +use JsonException; |
| 13 | +use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
| 14 | +use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
| 15 | +use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; |
| 16 | + |
| 17 | +final class FilterableDirective extends BaseDirective implements FieldManipulator |
| 18 | +{ |
| 19 | + public static function definition(): string |
| 20 | + { |
| 21 | + return /* @lang GraphQL */ <<<'GRAPHQL' |
| 22 | + directive @filterable on FIELD_DEFINITION |
| 23 | + GRAPHQL; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @throws JsonException |
| 28 | + * @throws SyntaxError |
| 29 | + */ |
| 30 | + public function manipulateFieldDefinition( |
| 31 | + DocumentAST &$documentAST, |
| 32 | + FieldDefinitionNode &$fieldDefinition, |
| 33 | + ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType, |
| 34 | + ): void { |
| 35 | + $typeName = $parentType->name->value . 'FilterInput'; |
| 36 | + |
| 37 | + // We only have to do this once per type, even though this is called once per filterable directive |
| 38 | + if (!array_key_exists($typeName, $documentAST->types)) { |
| 39 | + $inputTypeString = "input {$typeName} {" . PHP_EOL; |
| 40 | + |
| 41 | + $allFieldNames = []; |
| 42 | + foreach ($parentType->fields as $field) { |
| 43 | + $allFieldNames[] = $field->name->value; |
| 44 | + } |
| 45 | + |
| 46 | + $filterableFields = []; |
| 47 | + foreach ($parentType->fields as $field) { |
| 48 | + foreach ($field->directives as $directive) { |
| 49 | + if ($directive->name->value === 'filterable') { |
| 50 | + $filterableFields[] = $field; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + foreach ($filterableFields as $field) { |
| 57 | + $name = $field->name->value; |
| 58 | + $type = $field->type->name->value ?? $field->type->type->name->value; |
| 59 | + $description = $field->description?->value; |
| 60 | + |
| 61 | + // Only allow one field at a time. |
| 62 | + $fieldsToExclude = array_filter($allFieldNames, fn ($value): bool => $value !== $name); |
| 63 | + $validationDirective = '@rules(apply: ["prohibits:' . implode(',', $fieldsToExclude) . '"])'; |
| 64 | + |
| 65 | + // The @rename directive is commonly used, so we handle it explicitly. In the future, we may |
| 66 | + // want a more generalized approach which applies any directives which are also valid input directives. |
| 67 | + $renameDirective = ''; |
| 68 | + foreach ($field->directives as $directive) { |
| 69 | + if ($directive->name->value === 'rename') { |
| 70 | + $renameDirective = '@rename(attribute: "' . $directive->arguments[0]->value->value . '")'; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if ($description !== null) { |
| 76 | + $inputTypeString .= '"""' . PHP_EOL . $description . PHP_EOL . '"""' . PHP_EOL; |
| 77 | + } |
| 78 | + $inputTypeString .= "{$name}: $type $validationDirective $renameDirective" . PHP_EOL; |
| 79 | + } |
| 80 | + |
| 81 | + $inputTypeString .= '}'; |
| 82 | + |
| 83 | + $documentAST->setTypeDefinition(Parser::inputObjectTypeDefinition($inputTypeString)); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments