kugacz / nj-request-scope Goto Github PK
View Code? Open in Web Editor NEWPerformant request scope dependency injection for NestJS framework using express server
License: MIT License
Performant request scope dependency injection for NestJS framework using express server
License: MIT License
As in the title, I love what you did here. I'd have to take some more time to dig in, but from what I saw your message on SO, this should be a proper way of doing this DI, I can't believe they haven't yet introduced it in the core framework, do you observe any caveats?
That being said, it could be a nice initiative to build around NestJS proper Injectables() so we could benefit from such a basic stuff like Cache, Validation etc...
Let me know what you think :)
Greetings
@kugacz Request scope is empty for graphql only works for the RESTful is there any way to update the request scope context. I am currently using the interceptor approach which doesn't seem to be clean
---- Interceptor -------
@Injectable()
export class InterceptorUsingReqScopedProvider implements NestInterceptor {
constructor(
private readonly modRef: ModuleRef,
private readonly contextProvider: ContextProvider
) {}
async intercept(
context: ExecutionContext,
next: CallHandler
): Promise<Observable<unknown>> {
const req = this.getRequest(context);
if (req.baseUrl === '/graphql') {
this.contextProvider.setRequest(req);
}
return next.handle();
}
---- CONTEXT PROVIDER ------
import { Inject, Injectable, Scope } from '@nestjs/common';
import { Request } from 'express';
import { IncomingHttpHeaders } from 'http';
import { ClientSession } from 'mongodb';
import mongoose from 'mongoose';
import { NJRS_REQUEST, RequestScope } from 'nj-request-scope';
type RESTOrGraphQLRequest = Request;
@Injectable()
@RequestScope()
export class ContextProvider {
private _request: any;
private session: ClientSession;
public headers: IncomingHttpHeaders;
constructor(
@Inject(NJRS_REQUEST) private readonly request: RESTOrGraphQLRequest
) {
this._request = this.request; // NOTHING ON REQUEST
this.headers = request.headers;
}
setRequest(request: Request) {
this._request = request;
}
getUser() {
return this._request.user;
}
getWorkspace() {
return this._request.headers.workspace;
}
}
--APP GRAPH SETTINGS ON APP MODULE ---
GraphQLModule.forRootAsync({
imports: [RequestScopeModule],
inject: [NJRS_REQUEST],
driver: ApolloFederationDriver,
useFactory: (args) => {
console.log(args);
return {
autoSchemaFile: {
path: path.join(__dirname, 'schema.graphql'),
federation: 2,
},
cors: true,
context: async ({ req }) => {
return { req };
},
} as GqlModuleOptions;
},
})
I'm getting compilation error when using @requestScope() decorator.
Is there a missing part I should implement to use this ?
node_modules/nj-request-scope/lib/util/dynamic.object.handler.factory.js:27
throw new NotImplementedException();
^
Error: Not implemented. Feel free to implement.
at Object.getPrototypeOf (//node_modules/nj-request-scope/lib/util/dynamic.object.handler.factory.js:27:19)
at Function.[Symbol.hasInstance] (<anonymous>)
at mapToProxy (node_modules/@nestjs/core/nest-factory.js:167:35)
at Proxy.<anonymous> (//node_modules/@nestjs/core/nest-factory.js:182:32)
at bootstrap (/src/main.ts:39:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Hi Marek,
I noticed from 1.0.5 to 1.0.6 the dependencies to nesjts packages have been bumped from v8 to v9.
It's usually good pratice to issue a new major release when that happens, especially with nestjs. As npm deps resolution might install both v9 and v8 version without any modification in package.json.
No real work needed, but it might be useful for next major nestjs migration.
Apart maybe documenting how to install your extension for Nestjs v8 :
npm install --save --save-exact [email protected]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.