| @@ 16-65 (lines=50) @@ | ||
| 13 | use Nuwave\Lighthouse\Support\Contracts\FieldResolver; |
|
| 14 | use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
|
| 15 | ||
| 16 | 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 | ||
| @@ 16-65 (lines=50) @@ | ||
| 13 | use Nuwave\Lighthouse\Support\Contracts\FieldResolver; |
|
| 14 | use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
|
| 15 | ||
| 16 | class FirstTranslationDirective 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 'firstTranslation'; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * {@inheritdoc} |
|
| 30 | */ |
|
| 31 | public static function definition(): string |
|
| 32 | { |
|
| 33 | return /* @lang GraphQL */ <<<'SDL' |
|
| 34 | directive @firstTranslation( |
|
| 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)->first(); |
|
| 62 | } |
|
| 63 | ); |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||