Skip to content

Commit

Permalink
#2970: autoGen-talawa-admin-docs-debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
bint-Eve committed Jan 10, 2025
1 parent c5efd32 commit f859190
Showing 1 changed file with 61 additions and 27 deletions.
88 changes: 61 additions & 27 deletions src/utils/StaticMockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function requestToKey(request: any, addTypename: boolean): string {
return JSON.stringify(requestKey);
}

/**
* Similar to the standard Apollo MockLink, but doesn't consume a mock
* when it is used allowing it to be used in places like Storybook.
*/
export class StaticMockLink extends ApolloLink {
public operation?: Operation;
public addTypename = true;
Expand Down Expand Up @@ -69,37 +73,63 @@ export class StaticMockLink extends ApolloLink {
},
);

let configError: Error;

if (!response || typeof responseIndex === 'undefined') {
return new Observable((observer) => {
const error = new Error(
`No more mocked responses for the query: ${print(
operation.query,
)}, variables: ${JSON.stringify(operation.variables)}`,
configError = new Error(
`No more mocked responses for the query: ${print(
operation.query,
)}, variables: ${JSON.stringify(operation.variables)}`,
);
} else {
const { newData } = response;
if (newData) {
response.result = newData(operation.variables);
this._mockedResponsesByKey[key].push(response);
}

if (!response.result && !response.error) {
configError = new Error(
`Mocked response should contain either result or error: ${key}`,
);
observer.error(error);
});
}
}

return new Observable((observer) => {
const timer = setTimeout(() => {
if (response.error) {
observer.error(response.error);
} else if (response.result) {
observer.next(
typeof response.result === 'function'
? (response.result as ResultFunction<FetchResult>)(
operation.variables,
)
: response.result,
);
observer.complete();
} else {
const error = new Error(
`Mocked response should contain either result or error: ${key}`,
);
observer.error(error);
}
}, response.delay || 50); // Default delay of 50ms
const timer = setTimeout(
() => {
if (configError) {
try {
// The onError function can return false to indicate that
// configError need not be passed to observer.error. For
// example, the default implementation of onError calls
// observer.error(configError) and then returns false to
// prevent this extra (harmless) observer.error call.
if (this.onError(configError, observer) !== false) {
throw configError;
}
} catch (error) {
observer.error(error);
}
} else if (response) {
if (response.error) {
observer.error(response.error);
} else {
if (response.result) {
observer.next(
typeof response.result === 'function'
? (response.result as ResultFunction<FetchResult>)(
operation.variables,
)
: response.result,
);
}
observer.complete();
}
}
},
(response && response.delay) || 0,
);

return () => {
clearTimeout(timer);
Expand Down Expand Up @@ -128,10 +158,14 @@ export interface InterfaceMockApolloLink extends ApolloLink {
operation?: Operation;
}

// Function to mock a single link
// Pass in multiple mocked responses, so that you can test flows that end up
// making multiple queries to the server.
// NOTE: The last arg can optionally be an `addTypename` arg.
export function mockSingleLink(
...mockedResponses: any[]
): InterfaceMockApolloLink {
// To pull off the potential typename. If this isn't a boolean, we'll just
// set it true later.
let maybeTypename = mockedResponses[mockedResponses.length - 1];
let mocks = mockedResponses.slice(0, mockedResponses.length - 1);

Expand Down

0 comments on commit f859190

Please sign in to comment.