Skip to main content

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 handler, 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.

The RequestContext also provides mechanisms for managing the database replication mode via the setReplicationMode method and the replicationMode getter. This allows for finer control over whether database queries within the context should be executed against the master or a replica database, which can be particularly useful in distributed database environments.

Example

Ts

Example

Ts
Signature

empty

method() => RequestContext

Creates an "empty" RequestContext object. This is only intended to be used when a service method must be called outside the normal request-response cycle, e.g. when programmatically populating data. Usually a better alternative is to use the RequestContextService create() method, which allows more control over the resulting RequestContext object.

deserialize

method(ctxObject: SerializedRequestContext) => RequestContext

Creates a new RequestContext object from a serialized object created by the serialize() method.

userHasPermissions

method(permissions: Permission[]) => boolean

Returns true if there is an active Session & User associated with this request, and that User has at least one of the specified permissions on the active Channel.

This method uses OR logic - it checks if the user has ANY of the given permissions, not ALL of them. For AND logic, use userHasAllPermissions.

Example

Ts

userHasAllPermissions

method(permissions: Permission[]) => booleanv3.6.0

Returns true if there is an active Session & User associated with this request, and that User has all of the specified permissions on the active Channel.

This method uses AND logic - it checks if the user has EVERY one of the given permissions. For OR logic (any permission), use userHasPermissions.

Example

Ts

serialize

method() => SerializedRequestContext

Serializes the RequestContext object into a JSON-compatible simple object. This is useful when you need to send a RequestContext object to another process, e.g. to pass it to the Job Queue via the JobQueueService.

copy

method() => RequestContext

Creates a shallow copy of the RequestContext instance. This means that mutations to the copy itself will not affect the original, but deep mutations (e.g. copy.channel.code = 'new') will also affect the original.

req

propertyRequest | undefined

The raw Express request object.

apiType

propertyApiType

Signals which API this request was received by, e.g. admin or shop.

channel

propertyChannel

The active Channel of this request.

channelId

propertyID

languageCode

propertyLanguageCode

currencyCode

propertyCurrencyCode

session

propertyCachedSession | undefined

activeUserId

propertyID | undefined

isAuthorized

propertyboolean

True if the current session is authorized to access the current resolver method.

authorizedAsOwnerOnly

propertyboolean

True if the current anonymous session is only authorized to operate on entities that are owned by the current session.

translate

method(key: string, variables?: { [k: string]: any }) => string

Translate the given i18n key

setReplicationMode

method(mode: ReplicationMode) => void

Sets the replication mode for the current RequestContext. This mode determines whether the operations within this context should interact with the master database or a replica. Use this method to explicitly define the replication mode for the context.

replicationMode

propertyReplicationMode | undefined

Gets the current replication mode of the RequestContext. If no replication mode has been set, it returns undefined. This property indicates whether the context is configured to interact with the master database or a replica.

Was this chapter helpful?
Report Issue
Edited Feb 10, 2026ยทEdit this page