|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\GraphQL\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
|
6
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
|
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
8
|
|
|
use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
|
9
|
|
|
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
|
10
|
|
|
use Nuwave\Lighthouse\Schema\Values\FieldValue; |
|
11
|
|
|
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective; |
|
12
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; |
|
13
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldResolver; |
|
14
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
|
15
|
|
|
|
|
16
|
|
View Code Duplication |
class AllTranslationsDirective extends BaseDirective implements FieldResolver, FieldManipulator, DefinedDirective |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
use Traits\ExtendSchemaWithLocaleFields, Traits\LocaleFilters; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function name(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return 'allTranslations'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function definition(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return /* @lang GraphQL */ <<<'SDL' |
|
34
|
|
|
directive @allTranslations( |
|
35
|
|
|
"Specify the class name of the model to use." |
|
36
|
|
|
model: String |
|
37
|
|
|
"Specify the GraphQL type to add the 'locale' field (if GraphQL type is different from model class basename)." |
|
38
|
|
|
type: String |
|
39
|
|
|
) on ARGUMENT_DEFINITION |
|
40
|
|
|
SDL; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->extendSchemaWithLocaleFields( |
|
49
|
|
|
$documentAST, |
|
50
|
|
|
$this->directiveArgValue('type', class_basename($this->getModelClass())) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function resolveField(FieldValue $fieldValue): FieldValue |
|
58
|
|
|
{ |
|
59
|
|
|
return $fieldValue->setResolver( |
|
60
|
|
|
function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { |
|
|
|
|
|
|
61
|
|
|
return $this->localeFilters($this->getModelClass(), $args)->get(); |
|
62
|
|
|
} |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.