Skip to main content

Class: Laika

Laika.Laika

Class responsible for managing interceptions. By default a singleton is installed on globalThis (usually window) under laika.

Read more in the module page or scroll down to see it's functionality.

example

laika.log.startLogging();

Constructors

constructor

new Laika(__namedParameters?)

Parameters

NameType
__namedParametersObject
__namedParameters.referenceName?string

Defined in

src/laika.ts:84

Properties

log

log: LogApi

A set of functions that controls logging and recording of all (or selected) operations.

Read more on the LogApi page.

example

laika.log.startLogging();

Defined in

src/laika.ts:429

Methods

createLink(onRequest?): ApolloLink

Use this function to create an Apollo Link that uses this Laika instance. Useful in unit tests.

Parameters

NameType
onRequest?(operation: Operation, forward: NextLink) => void

Returns

ApolloLink

Defined in

src/laika.ts:488


intercept

intercept(matcher?, connectFutureLinksOrMitmFn?, keepNonSubscriptionConnectionsOpen?): InterceptApi

Provides functionality to intercept, and optionally mock or modify each operation's subscription. The API returned is heavily inspired on jest's mocking functionality (jest.fn()) and is described in length here: InterceptApi.

Every interceptor you create should be as specific as needed in a given session. At the very least, ensure the order of creating interceptors is from most specific, to least specific.

This is because any operations that are executed by your client will end up being intercepted by the first interceptor that matches the constraints of the Matcher.

See Pitfalls for more information.

example

const getActiveUsersInterceptor = laika.intercept({
clientName: 'users',
operationName: 'getActiveUsers',
});

Parameters

NameTypeDefault valueDescription
matcher?MatcherundefinedLeave undefined if you want to intercept every operation, otherwise provide either a matcher function or a matcher object with properties like clientName and/or a partial set of variables that have to match for a given operation to be intercepted
connectFutureLinksOrMitmFnundefined | boolean | ManInTheMiddleFnfalseIf true, future links will still be called (e.g. reach the backend) and return responses. If set to a function, can serve for man-in-the-middle tinkering with the result.
keepNonSubscriptionConnectionsOpenbooleanfalseIf true, queries and mutations will behave a little like subscriptions, in that you will be able to fire updates even after the initial response. Experimental.

Returns

InterceptApi

Defined in

src/laika.ts:117


modifyRemote

modifyRemote(matcher, mapFn): Object

Modify backend (or mocked) responses before they reach subscribers.

Parameters

NameTypeDescription
matcherundefined | MatcherLeave undefined if you want to intercept every operation, otherwise provide either a matcher function or a matcher object with properties like clientName and/or a partial set of variables that have to match for a given operation to be intercepted
mapFn(result: FetchResult<Record<string, any>, Record<string, any>, Record<string, any>>, operation: Operation) => FetchResult<Record<string, any>, Record<string, any>, Record<string, any>>Mapping function to alter the responses.

Returns

Object

NameType
restore() => void

Defined in

src/laika.ts:405