@2muchcoffee/nestjs-gql-context 中文文档教程

发布于 4年前 浏览 27 项目主页 更新于 3年前

@2muchcoffee/nestjs-context

允许您获取 NestJs 请求范围之外的请求上下文

Installation

npm install @2muchcoffee/nestjs-context --save

Usage

Import ContextModuleAppModuleCoreModule 一次

import { ContextModule } from '@2muchcoffee/nestjs-context';
@Module({
  imports: [
    ...
    ContextModule,
    ...
  ]
})
export class AppModule {
}

After ContextModule import 您可以在代码中使用 Context 及其静态函数

import { Context } from '@2muchcoffee/nestjs-context';
const {user} = Context.getHttpReq<Request>();

注意:在大多数情况下,作者使用 @2muchcoffee/nestjs-context 在自定义验证器中获取上下文。 有关详细信息,请参阅示例

GraphQl

你应该安装 @2muchcoffee/nestjs-gql-context 以获得框架使用的正确上下文

npm install @2muchcoffee/nestjs-gql-context --save

如果你想要 graphql 上下文,你可以使用 GqlContext 来获取它Context

import { GqlContext } from '@2muchcoffee/nestjs-gql-context';
GqlContext.getGqlContext<{req: Request}>()

Static Methods

MethodDescription
Contextimport { Context } from '@2muchcoffee/nestjs-context'
getContextReturns the current request pipeline
getHttpArgsReturns an array of arguments being passed to the handler
getHttpReqReturns a request object
getHttpResReturns a response object
getTypeReturns the current execution context type (string)
isHttpReturns true if the current execution context type is http
isGraphqlReturns true if the current execution context type is graphql
GqlContextimport { GqlContext } from '@2muchcoffee/nestjs-gql-context';
Context extends from GqlContext and has the same methods
getExecutionContextReturns the current gql execution context
getGqlRootThe previous object sent from the previous Query type
getGqlArgsArguments provided in the GraphQL query
getGqlContextA value which is provided to every resolver and holds important contextual information
getGqlInfoA value which holds field-specific information relevant to the current query

Samples

查看./sample中的示例使用例子。

@2muchcoffee/nestjs-context

Allows you to get the request context outside of the NestJs request scope

Installation

npm install @2muchcoffee/nestjs-context --save

Usage

Import ContextModule to AppModule or CoreModule for once

import { ContextModule } from '@2muchcoffee/nestjs-context';
@Module({
  imports: [
    ...
    ContextModule,
    ...
  ]
})
export class AppModule {
}

After ContextModule import you can use Context and its static functions in your code

import { Context } from '@2muchcoffee/nestjs-context';
const {user} = Context.getHttpReq<Request>();

Note: In most cases author uses @2muchcoffee/nestjs-context to get context in custom validators. See samples for more details.

GraphQl

You should install @2muchcoffee/nestjs-gql-context in order to get the correct context on the framework usage

npm install @2muchcoffee/nestjs-gql-context --save

If you want the graphql context, you can get it using GqlContext instead of Context

import { GqlContext } from '@2muchcoffee/nestjs-gql-context';
GqlContext.getGqlContext<{req: Request}>()

Static Methods

MethodDescription
Contextimport { Context } from '@2muchcoffee/nestjs-context'
getContextReturns the current request pipeline
getHttpArgsReturns an array of arguments being passed to the handler
getHttpReqReturns a request object
getHttpResReturns a response object
getTypeReturns the current execution context type (string)
isHttpReturns true if the current execution context type is http
isGraphqlReturns true if the current execution context type is graphql
GqlContextimport { GqlContext } from '@2muchcoffee/nestjs-gql-context';
Context extends from GqlContext and has the same methods
getExecutionContextReturns the current gql execution context
getGqlRootThe previous object sent from the previous Query type
getGqlArgsArguments provided in the GraphQL query
getGqlContextA value which is provided to every resolver and holds important contextual information
getGqlInfoA value which holds field-specific information relevant to the current query

Samples

Take a look at samples in ./sample for usage examples.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文