You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to implement some custom logic to default to a language saved in the DB for the user when the lang header is not provided. For this to work I need to inject the db-service into the resolver.
Nest, just keeps complaining that the service is not properly provided. Alternatively I could also set the language for the I18N context in a middleware (that is applied after the I18N middleware so I cant just set the header there) but the context does not seem to have an option to set the language. Thats why I think a custom resolver is the only proper solution.
This is my current implementation:
app.module.ts
@Module({
imports: [
I18nModule.forRoot({
fallbackLanguage: 'en-GB',
loaderOptions: {
path: join(__dirname, '../../../libs/i18n/backend/src/locales'),
},
typesOutputPath: join(__dirname, '../../../../src/generated/i18n.generated.ts'),
resolvers: [
GraphQLWebsocketResolver,
{ use: QueryResolver, options: ['lang'] },
AcceptLanguageResolver,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{
// with use I get an error that the service is not provided, without use I get an type error and "resolver.resolve is not a function" when the resolver is called.
// use: I18NCustomHeaderResolver,
useFactory: (databaseService) => new I18NCustomHeaderResolver(['x-lang'], databaseService),
inject: [DatabaseService],
},
],
}),
The whole configuration was valid and works. Its just the databaseService that I can not provide to the custom resolver.
languageResolver.ts: really just the custom db logic with the injected service
@Injectable()
export class I18NCustomHeaderResolver implements I18nResolver {
private logger = new Logger('I18nService');
constructor(
@I18nResolverOptions()
private keys: string[] = [],
private readonly dbService: DatabaseService,
) {
console.log('dbService: ', dbService);
}
resolve(context: ExecutionContext) {
let req: any;
switch (context.getType() as string) {
case 'http':
req = context.switchToHttp().getRequest();
break;
case 'graphql':
[, , { req }] = context.getArgs();
break;
}
let lang: string;
if (req) {
for (const key of this.keys) {
if (key === 'accept-language') {
this.logger.warn(
'HeaderResolver does not support RFC4647 Accept-Language header. Please use AcceptLanguageResolver instead.',
);
}
if (req.headers !== undefined && req.headers[key] !== undefined) {
lang = req.headers[key];
break;
}
}
}
... HERE IS MY DB LOGIC, nothing special
return lang;
}
}
Can you please give me hint how I can inject my service to the custom provider and what is an recommended pattern. Unfortunately documentation is missing for these kind of use cases.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to implement some custom logic to default to a language saved in the DB for the user when the lang header is not provided. For this to work I need to inject the db-service into the resolver.
Nest, just keeps complaining that the service is not properly provided. Alternatively I could also set the language for the I18N context in a middleware (that is applied after the I18N middleware so I cant just set the header there) but the context does not seem to have an option to set the language. Thats why I think a custom resolver is the only proper solution.
This is my current implementation:
app.module.ts
The whole configuration was valid and works. Its just the databaseService that I can not provide to the custom resolver.
languageResolver.ts: really just the custom db logic with the injected service
Can you please give me hint how I can inject my service to the custom provider and what is an recommended pattern. Unfortunately documentation is missing for these kind of use cases.
Beta Was this translation helpful? Give feedback.
All reactions