@nestjs/graphql
Version:
GraphQL for NestJS: code-first and schema-first APIs, resolvers as providers, subscriptions, federation and generated SDL, on Apollo or Mercurius.
80 lines (79 loc) • 2.63 kB
JavaScript
var ResolverDecoratorHost_1;
import { __decorate } from "tslib";
import { Injectable, Logger } from '@nestjs/common';
/**
* @publicApi
*/
let ResolverDecoratorHost = ResolverDecoratorHost_1 = class ResolverDecoratorHost {
constructor() {
this.logger = new Logger(ResolverDecoratorHost_1.name);
}
setDecorator(decorator) {
this.decorator = decorator;
}
getDecorator() {
return this.decorator;
}
decorate(target) {
if (!this.decorator) {
return target;
}
return this.decorator(target);
}
/**
* Registers a hook that is called right before a GraphQL operation
* starts being processed. Whatever it returns is passed back to the
* "request end" hook through the `state` property.
*/
setOnRequestStartHook(hook) {
this.onRequestStartHook = hook;
}
getOnRequestStartHook() {
return this.onRequestStartHook;
}
/**
* Registers a hook that is called once a GraphQL operation has been
* fully processed (right before the response is sent back to the client).
*/
setOnRequestEndHook(hook) {
this.onRequestEndHook = hook;
}
getOnRequestEndHook() {
return this.onRequestEndHook;
}
/**
* Indicates whether any of the request lifecycle hooks has been registered.
*/
hasRequestHooks() {
return !!(this.onRequestStartHook || this.onRequestEndHook);
}
/**
* Calls the "request start" hook and returns what it returns: the promise
* of an async hook is returned as is, for the caller to await.
*/
onRequestStart(context) {
return this.handleRejection(this.onRequestStartHook?.(context), 'start');
}
/**
* Calls the "request end" hook. The promise of an async hook is returned,
* for the caller to await.
*/
onRequestEnd(context) {
return this.handleRejection(this.onRequestEndHook?.(context), 'end');
}
/**
* A rejected promise that nothing handles terminates the Node.js process,
* so the rejection of an async hook is logged even if the caller does not
* await the promise (the caller awaiting it still sees the rejection).
*/
handleRejection(result, hook) {
if (result instanceof Promise) {
result.catch((err) => this.logger.error(`The "request ${hook}" hook failed.`, err instanceof Error ? err.stack : String(err)));
}
return result;
}
};
ResolverDecoratorHost = ResolverDecoratorHost_1 = __decorate([
Injectable()
], ResolverDecoratorHost);
export { ResolverDecoratorHost };