RequestContext
RequestContext
The RequestContext holds information relevant to the current request, which may be required at various points of the stack.
It is a good practice to inject the RequestContext (using the Ctx decorator) into all resolvers & REST handlers, and then pass it through to the service layer.
This allows the service layer to access information about the current user, the active language, the active Channel, and so on. In addition, the TransactionalConnection relies on the presence of the RequestContext object in order to correctly handle per-request database transactions.
Example
@Query()
myQuery(@Ctx() ctx: RequestContext) {
return this.myService.getData(ctx);
}
Signature
class RequestContext {
static empty() => RequestContext;
static deserialize(ctxObject: SerializedRequestContext) => RequestContext;
userHasPermissions(permissions: Permission[]) => boolean;
serialize() => SerializedRequestContext;
copy() => RequestContext;
req: Request | undefined
apiType: ApiType
channel: Channel
channelId: ID
languageCode: LanguageCode
session: CachedSession | undefined
activeUserId: ID | undefined
isAuthorized: boolean
authorizedAsOwnerOnly: boolean
translate(key: string, variables?: { [k: string]: any }) => string;
}
Members
empty
() => RequestContext
create()
method, which allows more control
over the resulting RequestContext object.
deserialize
(ctxObject: SerializedRequestContext) => RequestContext
serialize()
method.
userHasPermissions
(permissions: Permission[]) => boolean
true
if there is an active Session & User associated with this request,
and that User has the specified permissions on the active Channel.
serialize
() => SerializedRequestContext
copy
() => RequestContext
req
Request | undefined
apiType
ApiType
admin
or shop
.
channel
Channel
channelId
ID
languageCode
LanguageCode
session
CachedSession | undefined
activeUserId
ID | undefined
isAuthorized
boolean
authorizedAsOwnerOnly
boolean
translate
(key: string, variables?: { [k: string]: any }) => string