From f859190603ed69279d8a44ebd341038d2fa3d754 Mon Sep 17 00:00:00 2001 From: Rukh-Khan Date: Fri, 10 Jan 2025 12:26:08 +0530 Subject: [PATCH] #2970: autoGen-talawa-admin-docs-debugging --- src/utils/StaticMockLink.ts | 88 +++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/src/utils/StaticMockLink.ts b/src/utils/StaticMockLink.ts index e4d2bf4c13..457da3dfd4 100644 --- a/src/utils/StaticMockLink.ts +++ b/src/utils/StaticMockLink.ts @@ -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; @@ -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)( - 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)( + operation.variables, + ) + : response.result, + ); + } + observer.complete(); + } + } + }, + (response && response.delay) || 0, + ); return () => { clearTimeout(timer); @@ -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);