2022-04-04 11:22:00 +05:30
|
|
|
import { InMemoryCache, ApolloClient, ApolloLink, gql } from '@apollo/client/core';
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
const FOO_QUERY = gql`
|
|
|
|
query {
|
|
|
|
foo
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns a promise that resolves to the final operation after
|
|
|
|
* running an ApolloClient query with the given ApolloLink
|
|
|
|
*
|
|
|
|
* @typedef {Object} TestApolloLinkOptions
|
|
|
|
* @property {Object} context the default context object sent along the ApolloLink chain
|
|
|
|
*
|
|
|
|
* @param {ApolloLink} subjectLink the ApolloLink which is under test
|
|
|
|
* @param {TestApolloLinkOptions} options contains options to send a long with the query
|
|
|
|
*
|
|
|
|
* @returns Promise resolving to the resulting operation after running the subjectLink
|
|
|
|
*/
|
2023-04-23 21:23:45 +05:30
|
|
|
export const testApolloLink = (subjectLink, options = {}, query = FOO_QUERY) =>
|
2021-11-11 11:23:49 +05:30
|
|
|
new Promise((resolve) => {
|
|
|
|
const { context = {} } = options;
|
|
|
|
|
|
|
|
// Use the terminating link to capture the final operation and resolve with this.
|
|
|
|
const terminatingLink = new ApolloLink((operation) => {
|
|
|
|
resolve(operation);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = new ApolloClient({
|
|
|
|
link: ApolloLink.from([subjectLink, terminatingLink]),
|
|
|
|
// cache is a required option
|
|
|
|
cache: new InMemoryCache(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Trigger a query so the ApolloLink chain will be executed.
|
|
|
|
client.query({
|
|
|
|
context,
|
2023-04-23 21:23:45 +05:30
|
|
|
query,
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
});
|