@nestjs/graphql#GqlContextType TypeScript Examples
The following examples show how to use
@nestjs/graphql#GqlContextType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: userData.decorator.ts From amplication with Apache License 2.0 | 6 votes |
/**
* Access the user data from the request object i.e `req.user`.
*/
function userFactory(ctx: ExecutionContext): User {
const contextType = ctx.getType();
if (contextType === "http") {
// do something that is only important in the context of regular HTTP requests (REST)
const { user } = ctx.switchToHttp().getRequest();
return user;
} else if (contextType === "rpc") {
// do something that is only important in the context of Microservice requests
throw new Error("Rpc context is not implemented yet");
} else if (contextType === "ws") {
// do something that is only important in the context of Websockets requests
throw new Error("Websockets context is not implemented yet");
} else if (ctx.getType<GqlContextType>() === "graphql") {
// do something that is only important in the context of GraphQL requests
const gqlExecutionContext = GqlExecutionContext.create(ctx);
return gqlExecutionContext.getContext().req.user;
}
throw new Error("Invalid context");
}
Example #2
Source File: tracing-context.decorator.ts From nestjs-jaeger-tracing with MIT License | 6 votes |
Tracing = createParamDecorator(
(key: keyof TracingData, context: ExecutionContext) => {
const contextType = context.getType<GqlContextType>();
let tracingData: TracingData;
if (contextType === 'graphql') {
const ctx = GqlExecutionContext.create(context);
tracingData = ctx.getContext<TracingObject>().tracing;
}
if (contextType === 'http') {
const ctx = context.switchToHttp();
tracingData = ctx.getRequest<TracingObject>().tracing;
}
if (contextType === 'rpc') {
const ctx = context.switchToRpc();
tracingData = ctx.getContext<TracingObject>().tracing;
}
return key ? tracingData && tracingData[key] : tracingData;
},
)
Example #3
Source File: extract-request.ts From nestjs-keycloak-admin with MIT License | 5 votes |
export function extractRequest(context: ExecutionContext): any {
if (context.getType<GqlContextType>() === 'graphql') {
const gqlContext = GqlExecutionContext.create(context)
return gqlContext.getContext().req
}
return context.switchToHttp().getRequest()
}
Example #4
Source File: tracing.interceptor.ts From nestjs-jaeger-tracing with MIT License | 4 votes |
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const contextType = context.getType<GqlContextType>();
const except = this.reflector.get<boolean>(
EXCEPT_TRACING,
context.getHandler(),
);
if (contextType === 'ws') {
return next.handle();
}
if (except) {
if (contextType === 'rpc') {
const ctx = context.switchToRpc();
const data = ctx.getData<TracingContext>();
if (isTracingContext(data)) {
context.getArgs()[0] = data.payload;
}
}
return next.handle();
}
let parentSpanContext: SpanContext;
const spanTags = new Map<string, unknown>();
const spanLogs: Array<Record<string, unknown>> = [];
const constructorRef = context.getClass().name;
const handler = context.getHandler().name;
const operation = [contextType, constructorRef, handler].join(':');
const tracingData: TracingData = { operation };
spanTags.set('operation', operation);
spanTags.set(Tags.COMPONENT, contextType);
if (contextType === 'graphql') {
const ctx = GqlExecutionContext.create(context);
ctx.getContext<TracingObject>().tracing = tracingData;
const { path } = ctx.getInfo<GraphQLResolveInfo>();
spanTags.set('graphql.name', `${path?.key}`);
spanTags.set('graphql.type', `${path?.typename}`.toLowerCase());
}
if (contextType === 'http') {
const ctx = context.switchToHttp();
ctx.getRequest<TracingObject>().tracing = tracingData;
const { statusCode } = ctx.getResponse<Response>();
const { headers, method, path, ip, ips } = ctx.getRequest<Request>();
spanTags.set(Tags.HTTP_URL, path);
spanTags.set(Tags.HTTP_METHOD, `${method}`.toUpperCase());
spanTags.set(Tags.PEER_HOST_IPV4, [ip, ...ips].join(', '));
spanTags.set(Tags.HTTP_STATUS_CODE, statusCode);
parentSpanContext = this.tracerProvider.extractHeaders(headers);
}
if (contextType === 'rpc') {
const ctx = context.switchToRpc();
const data = ctx.getData<TracingContext>();
if (isTracingContext(data)) {
const { payload, tracing } = data;
context.getArgs()[0] = payload;
tracingData.parent = tracing;
parentSpanContext = this.tracerProvider.extractTracing(tracing);
}
ctx.getContext<TracingObject>().tracing = tracingData;
if (typeof ctx.getContext().getPattern === 'function') {
const pattern = ctx.getContext().getPattern();
spanTags.set('rpc.pattern', pattern);
}
if (typeof ctx.getContext().getChannel === 'function') {
const channel = ctx.getContext().getChannel();
spanTags.set('rpc.channel', channel);
}
if (typeof ctx.getContext().getSubject === 'function') {
const subject = ctx.getContext().getSubject();
spanTags.set('rpc.subject', subject);
}
if (typeof ctx.getContext().getTopic === 'function') {
const topic = ctx.getContext().getTopic();
spanTags.set('rpc.topic', topic);
}
spanLogs.push({ payload: JSON.stringify(ctx.getData(), null, 2) });
}
return this.asyncContext.run(() => {
const span = this.tracer.startSpan(operation, {
childOf: parentSpanContext,
tags: { kind: 'server' },
});
spanTags.forEach((value: string, key: string) => {
span.setTag(key, value);
});
spanLogs.forEach((log) => {
span.log(log);
});
tracingData.carrier = this.tracerProvider.getCarrier(span);
this.asyncContext.set(TRACING_CARRIER_INFO, tracingData);
return next.handle().pipe(
tap(() => {
span.finish();
}),
catchError((error: Error) => {
span.setTag(Tags.ERROR, true);
if (error instanceof HttpException) {
const tag =
contextType === 'http'
? Tags.HTTP_STATUS_CODE
: 'error.status_code';
span.setTag(tag, error.getStatus());
}
span.log({
event: Tags.ERROR,
'error.object': error,
message: error.message,
stack: error.stack,
});
span.finish();
return throwError(error);
}),
);
}) as Observable<unknown>;
}