Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Disabling of language fallback #907

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/10_GraphQL/04_Query/08_Localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
```
8 changes: 7 additions & 1 deletion src/GraphQL/DataObjectQueryFieldConfigGenerator/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
]);
}

Expand Down
9 changes: 8 additions & 1 deletion src/GraphQL/FieldHelper/DataObjectFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading