Skip to content

Commit 90aabfc

Browse files
authored
[Feature]: Disabling of language fallback (#907)
* Added getFallbackLanguageValue argument * Apply php-cs-fixer changes * Added sample request --------- Co-authored-by: mcop1 <[email protected]>
1 parent 18a4902 commit 90aabfc

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/10_GraphQL/04_Query/08_Localization.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,18 @@ However, you can always provide an alternative language for a specific field.
8787
}
8888
}
8989
```
90+
91+
### Fallback Language
92+
93+
You can disable getting the value of the fallback language by passing the `getFallbackLanguageValue` argument.
94+
Set it to `false` to disable the fallback language.
95+
96+
##### Sample Request
97+
```
98+
query {
99+
getCar(id: 1229)
100+
{
101+
name(language:"de", getFallbackLanguageValue:false)
102+
}
103+
}
104+
```

src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ public function enrichConfig($fieldDefinition, $class, $attribute, $graphQLConfi
6161
if ($container instanceof Data\Localizedfields) {
6262
$graphQLConfig['args'] = $graphQLConfig['args'] ?? [];
6363
$graphQLConfig['args'] = array_merge($graphQLConfig['args'],
64-
['language' => ['type' => Type::string()],
64+
[
65+
'language' => [
66+
'type' => Type::string(),
67+
],
68+
'getFallbackLanguageValue' => [
69+
'type' => Type::boolean(),
70+
],
6571
]);
6672
}
6773

src/GraphQL/FieldHelper/DataObjectFieldHelper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,14 @@ public function doExtractData(FieldNode $ast, &$data, $container, $args, $contex
387387
$container,
388388
$getter
389389
) {
390-
return $container->$getter($args['language'] ?? null);
390+
$orgUseFallbackValues = Localizedfield::getGetFallbackValues();
391+
Localizedfield::setGetFallbackValues(
392+
$args['getFallbackLanguageValue'] ?? $orgUseFallbackValues
393+
);
394+
$localizedValue = $container->$getter($args['language'] ?? null);
395+
Localizedfield::setGetFallbackValues($orgUseFallbackValues);
396+
397+
return $localizedValue;
391398
};
392399
} else {
393400
$data[$astName] = $container->$getter();

0 commit comments

Comments
 (0)