1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\GraphQL\Directives; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
7
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
9
|
|
|
use Nuwave\Lighthouse\Schema\AST\DocumentAST; |
10
|
|
|
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; |
11
|
|
|
use Nuwave\Lighthouse\Schema\Values\FieldValue; |
12
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; |
13
|
|
|
use Nuwave\Lighthouse\Support\Contracts\FieldResolver; |
14
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
15
|
|
|
|
16
|
|
|
class FirstTranslationDirective extends BaseDirective implements FieldResolver, FieldManipulator |
17
|
|
|
{ |
18
|
|
|
use Traits\ExtendSchemaWithLocaleFields, Traits\LocaleFilters; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public static function definition(): string |
24
|
|
|
{ |
25
|
|
|
return /* @lang GraphQL */ <<<'SDL' |
26
|
|
|
directive @firstTranslation( |
27
|
|
|
"Specify the class name of the model to use." |
28
|
|
|
model: String |
29
|
|
|
"Specify the GraphQL type to add the 'locale' field (if GraphQL type is different from model class basename)." |
30
|
|
|
type: String |
31
|
|
|
) on FIELD_DEFINITION |
32
|
|
|
SDL; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void |
39
|
|
|
{ |
40
|
|
|
$this->extendSchemaWithLocaleFields( |
41
|
|
|
$documentAST, |
42
|
|
|
$this->directiveArgValue('type', class_basename($this->getModelClass())) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function resolveField(FieldValue $fieldValue): FieldValue |
50
|
|
|
{ |
51
|
|
|
return $fieldValue->setResolver( |
52
|
|
|
function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { |
|
|
|
|
53
|
|
|
if (isset($args['localeFilters']) && ! empty($args['localeFilters']['locales'])) { |
54
|
|
|
throw new Exception('Multiple locales cannot be queried on a single returned instance! You have to only use "locale" filter on your "localeFilters" parameter.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->localeFilters($this->getModelClass(), $args)->first(); |
58
|
|
|
} |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.