I am getting an error when using nestjs-tenancy with mongoose and graphql

20 views Asked by At

With basic setup as outlined in the docs https://github.com/needle-innovision/nestjs-tenancy module doesn't work with nestjs and graphql. After little debugging I found that "getTenantFromRequest" in the TenancyCore module called twice. In first call req was available and I was able to capture the header. But in the second call req was null. Which case tenantId becomes empty.

Has anyone able to get it working?

Update: I figured this only happens for resolvers I have AuthGuard configured, I used both below JwtAuthGuard(S)

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {

    canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean>{
        const type: string = context.getType();
        if(type == 'graphql'){
            const ctx = GqlExecutionContext.create(context);
            const { req } = ctx.getContext();
            return super.canActivate(new ExecutionContextHost([req]));
        }else{
            return super.canActivate(context);
        }
    }
}

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {

    getRequest(context: ExecutionContext){
        console.log('getRequest', context);
        const ctx = GqlExecutionContext.create(context);
        return ctx.getContext().req;
    }
}

0

There are 0 answers