needle-innovision / nestjs-tenancy

Multi-tenancy approach for nestjs - currently supported only for mongodb with mongoose
MIT License
186 stars 58 forks source link

How to use this with Graphql ? #10

Closed gaurav-bothra closed 2 years ago

gaurav-bothra commented 3 years ago

Hi,

I am trying to integrate nestjs-tenacy with graphql. I am getting the below error.

{
      "message": "req.get is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createCat"
      ],
}
sandeepsuvit commented 3 years ago

Hi @gaurav-bothra thanks for writing to us. Currently this library doesn't have out of the box support for GraphQL. Express and Fastify are supported. Feel free to raise a PR if you have ideas around how to implement the GraphQL support on this. Thanks 👍

gaurav-bothra commented 3 years ago

Hi @sandeepsuvit,

I Just add try/catch in tenancy-core.module.ts file. It is working fine after adding below snippet

 static getTenantFromRequest(isFastifyAdaptor, req, tenantIdentifier) {
            var _a;
            let tenantId = '';
            if (isFastifyAdaptor) {
                tenantId = ((_a = req.headers[`${tenantIdentifier || ''}`.toLowerCase()]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
            }
            else {
                try {
                    tenantId =  req.get(`${tenantIdentifier}`)  || '';
                } catch(err) {
                    tenantId =  req.req.get(`${tenantIdentifier}`) // For graphql
                }
            }
            if (this.isEmpty(tenantId)) {
                throw new common_1.BadRequestException(`${tenantIdentifier} is not supplied`);
            }
            return tenantId;
        }