|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBS\Nova\Translation\GraphQL\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
|
6
|
|
|
use GraphQL\Language\AST\NamedTypeNode; |
|
7
|
|
|
use GraphQL\Language\AST\NameNode; |
|
8
|
|
|
use GraphQL\Language\AST\NodeList; |
|
9
|
|
|
use GraphQL\Language\AST\NonNullTypeNode; |
|
10
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
|
11
|
|
|
use GraphQL\Language\AST\StringValueNode; |
|
12
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
13
|
|
|
use Nuwave\Lighthouse\Schema\AST\ASTHelper; |
|
14
|
|
|
use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
|
15
|
|
|
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
|
16
|
|
|
use Nuwave\Lighthouse\Schema\Values\FieldValue; |
|
17
|
|
|
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective; |
|
18
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; |
|
19
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldResolver; |
|
20
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
|
21
|
|
|
|
|
22
|
|
|
class TranslationDirective extends BaseDirective implements FieldResolver, FieldManipulator, DefinedDirective |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function name(): string |
|
28
|
|
|
{ |
|
29
|
|
|
return 'translation'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function definition(): string |
|
36
|
|
|
{ |
|
37
|
|
|
return /* @lang GraphQL */ <<<'SDL' |
|
38
|
|
|
directive @translation( |
|
39
|
|
|
"Specify the class name of the model to use." |
|
40
|
|
|
model: String |
|
41
|
|
|
"Specify the GraphQL type to add the 'locale' field." |
|
42
|
|
|
type: String |
|
43
|
|
|
) on ARGUMENT_DEFINITION |
|
44
|
|
|
SDL; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void |
|
51
|
|
|
{ |
|
52
|
|
|
$typeToAddLocaleField = $this->directiveArgValue('type', class_basename($this->getModelClass())); |
|
53
|
|
|
|
|
54
|
|
|
foreach ($documentAST->types as &$type) { |
|
55
|
|
|
if ($type->name->value === $typeToAddLocaleField) { |
|
56
|
|
|
/* @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $type */ |
|
57
|
|
|
$type->fields = ASTHelper::mergeNodeList($type->fields, [$this->defineLocaleField(), $this->defineTranslationIdField()]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function resolveField(FieldValue $fieldValue): FieldValue |
|
66
|
|
|
{ |
|
67
|
|
|
return $fieldValue->setResolver( |
|
68
|
|
|
function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { |
|
|
|
|
|
|
69
|
|
|
$modelClass = $this->getModelClass(); |
|
70
|
|
|
|
|
71
|
|
|
$isos = []; |
|
72
|
|
|
if (isset($args['localeFilters'])) { |
|
73
|
|
|
if (isset($args['localeFilters']['locale'])) { |
|
74
|
|
|
$isos[] = $args['localeFilters']['locale']; |
|
75
|
|
|
} |
|
76
|
|
|
if (isset($args['localeFilters']['locales'])) { |
|
77
|
|
|
$isos = array_merge($isos, $args['localeFilters']['locales']); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$table = (new $modelClass)->getTable(); |
|
82
|
|
|
$query = $modelClass::query() |
|
83
|
|
|
->select($table.'.translation_id', $table.'.type', $table.'.key', $table.'.value', 'locales.iso AS locale', $table.'.created_at', $table.'.updated_at') |
|
84
|
|
|
->join('translations', $table.'.id', '=', 'translations.translatable_id') |
|
85
|
|
|
->join('locales', 'translations.locale_id', '=', 'locales.id') |
|
86
|
|
|
->where('translations.translatable_type', '=', $modelClass) |
|
87
|
|
|
->where('locales.available_in_api', '=', true); |
|
88
|
|
|
|
|
89
|
|
|
if (! empty($isos)) { |
|
90
|
|
|
$query = $query->whereIn('locales.iso', $isos); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $query->get(); |
|
94
|
|
|
} |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Setup "locale" field definition. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \GraphQL\Language\AST\FieldDefinitionNode |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
protected function defineLocaleField() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
return new FieldDefinitionNode([ |
|
106
|
|
|
'name' => new NameNode(['value' => 'locale']), |
|
107
|
|
|
'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'String'])])]), |
|
108
|
|
|
'arguments' => new NodeList([]), |
|
109
|
|
|
'directives' => new NodeList([]), |
|
110
|
|
|
'description' => new StringValueNode(['value' => 'Locale ISO', 'block' => false]), |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Setup "translationId" field definition. |
|
116
|
|
|
* |
|
117
|
|
|
* @return \GraphQL\Language\AST\FieldDefinitionNode |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
protected function defineTranslationIdField() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
// @TODO... Add @rename(attribute: "translation_id") directive |
|
122
|
|
|
return new FieldDefinitionNode([ |
|
123
|
|
|
'name' => new NameNode(['value' => 'translationId']), |
|
124
|
|
|
'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'Int!'])])]), |
|
125
|
|
|
'arguments' => new NodeList([]), |
|
126
|
|
|
'directives' => new NodeList([]), |
|
127
|
|
|
'description' => new StringValueNode(['value' => 'Item translation ID', 'block' => false]), |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.