Skip to content

Instantly share code, notes, and snippets.

@RStankov

RStankov/initApollo.js Secret

Created Nov 30, 2020
Embed
What would you like to do?
return new ApolloClient({
link: recordRequestsInFlight(
new HttpLink(/* ... */),
),
// ...
});
import { ApolloLink, concat } from 'apollo-link';
import { environment } from '~/config';
declare global {
// tslint:disable-next-line
interface Window {
test: {
apolloRequestsInFlight: number;
};
}
}
let recordRequestsInFlight: any = (networkLink: any) => networkLink;
if (environment.isBrowser && environment.isTest) {
recordRequestsInFlight = (networkLink: any) => {
const recorderLink = new ApolloLink((operation: any, forward: any) => {
window.test.apolloRequestsInFlight += 1;
return forward(operation).map((result: any) => {
window.test.apolloRequestsInFlight -= 1;
return result;
});
});
return concat(recorderLink, networkLink);
};
}
export default recordRequestsInFlight;
# this waits until there are no upcoming XHR request
def wait_for_ajax
Timeout.timeout(Capybara.default_max_wait_time) do
loop while page.evaluate_script('window.test.hasInFlightRequests()')
end
rescue Timeout::Error
# NOTE(rstankov): Some times this fails on CI while code is working
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment