diff --git a/doc/10_GraphQL/04_Query/08_Localization.md b/doc/10_GraphQL/04_Query/08_Localization.md index b74db6ac..d020beb5 100644 --- a/doc/10_GraphQL/04_Query/08_Localization.md +++ b/doc/10_GraphQL/04_Query/08_Localization.md @@ -87,3 +87,18 @@ However, you can always provide an alternative language for a specific field. } } ``` + +### Fallback Language + +You can disable getting the value of the fallback language by passing the `getFallbackLanguageValue` argument. +Set it to `false` to disable the fallback language. + +##### Sample Request +``` +query { + getCar(id: 1229) + { + name(language:"de", getFallbackLanguageValue:false) + } +} +``` diff --git a/src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php b/src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php index df408c39..2e1d53d4 100644 --- a/src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php +++ b/src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php @@ -61,7 +61,13 @@ public function enrichConfig($fieldDefinition, $class, $attribute, $graphQLConfi if ($container instanceof Data\Localizedfields) { $graphQLConfig['args'] = $graphQLConfig['args'] ?? []; $graphQLConfig['args'] = array_merge($graphQLConfig['args'], - ['language' => ['type' => Type::string()], + [ + 'language' => [ + 'type' => Type::string(), + ], + 'getFallbackLanguageValue' => [ + 'type' => Type::boolean(), + ], ]); } diff --git a/src/GraphQL/FieldHelper/DataObjectFieldHelper.php b/src/GraphQL/FieldHelper/DataObjectFieldHelper.php index f5c033f2..46c29b6e 100644 --- a/src/GraphQL/FieldHelper/DataObjectFieldHelper.php +++ b/src/GraphQL/FieldHelper/DataObjectFieldHelper.php @@ -387,7 +387,14 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex $container, $getter ) { - return $container->$getter($args['language'] ?? null); + $orgUseFallbackValues = Localizedfield::getGetFallbackValues(); + Localizedfield::setGetFallbackValues( + $args['getFallbackLanguageValue'] ?? $orgUseFallbackValues + ); + $localizedValue = $container->$getter($args['language'] ?? null); + Localizedfield::setGetFallbackValues($orgUseFallbackValues); + + return $localizedValue; }; } else { $data[$astName] = $container->$getter();