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

Custom resolver is not working #203

Closed
windcloudit opened this issue Jun 24, 2021 · 2 comments
Closed

Custom resolver is not working #203

windcloudit opened this issue Jun 24, 2021 · 2 comments

Comments

@windcloudit
Copy link

windcloudit commented Jun 24, 2021

I created a custom resolver but maybe it is not running now, This is my source code, you can see console.log('Run here') is not print out.
I try call by postman with header key is lang but it can not translate now, the default language alway be returned, could you help me check it. Thank you so much.

import { QueryResolver } from './resolvers/query.resolver';

I18nModule.forRootAsync({
      useFactory: () => {
        return {
          fallbackLanguage: 'en',
          parserOptions: {
            path: path.join(__dirname, '/i18n'),
          },
        };
      },
      parser: I18nJsonParser,
      resolvers: [{ use: QueryResolver, options: ['lang', 'locale', 'l'] }],
    })
import { ExecutionContext, Injectable } from '@nestjs/common';
import { I18nResolver, I18nResolverOptions } from 'nestjs-i18n';

@Injectable()
export class QueryResolver implements I18nResolver {
  constructor(@I18nResolverOptions() private keys: string[]) {}

  resolve(context: ExecutionContext) {
    let req: any;
    console.log('Run here'); // it is not run
    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 (req.query != undefined && req.query[key] !== undefined) {
          lang = req.query[key];
          break;
        }
      }
    }

    return lang;
  }
}
@toonvanstrijp
Copy link
Owner

Heey @windcloudit I'm not sure what the issue is in your case. Maybe you misconfigured something else. I tried replicating your issue but it works just fine. See the attached zip with my project. (Run npm install and than npm run start:dev)

test.zip

@windcloudit
Copy link
Author

Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants