Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('calls unsubscribe on the appropriate downstream observable', async () => {
const retry = new RetryLink({
delay: { initial: 1 },
attempts: { max: 2 },
});
const data = { data: { hello: 'world' } };
const unsubscribeStub = jest.fn();
const firstTry = fromError(standardError);
// Hold the test hostage until we're hit
let secondTry;
const untilSecondTry = new Promise(resolve => {
secondTry = {
subscribe(observer) {
resolve(); // Release hold on test.
Promise.resolve().then(() => {
observer.next(data);
observer.complete();
});
return { unsubscribe: unsubscribeStub };
},
};
});
// If requested, set method to GET if there are no mutations.
const definitionIsMutation = (d: DefinitionNode) => {
return d.kind === 'OperationDefinition' && d.operation === 'mutation';
};
if (
useGETForQueries &&
!operation.query.definitions.some(definitionIsMutation)
) {
options.method = 'GET';
}
if (options.method === 'GET') {
const { newURI, parseError } = rewriteURIForGET(chosenURI, body);
if (parseError) {
return fromError(parseError);
}
chosenURI = newURI;
} else {
try {
(options as any).body = serializeFetchParameter(body, 'Payload');
} catch (parseError) {
return fromError(parseError);
}
}
return new Observable(observer => {
fetcher(chosenURI, options)
.then(response => {
operation.setContext({ response });
return response;
})
);
const loadedBody = optsAndBody.map(({ body }) => body);
const options = optsAndBody[0].options;
// There's no spec for using GET with batches.
if (options.method === 'GET') {
return fromError(
new Error('apollo-link-batch-http does not support GET requests'),
);
}
try {
(options as any).body = serializeFetchParameter(loadedBody, 'Payload');
} catch (parseError) {
return fromError(parseError);
}
let controller;
if (!(options as any).signal) {
const { controller: _controller, signal } = createSignalIfSupported();
controller = _controller;
if (controller) (options as any).signal = signal;
}
return new Observable(observer => {
fetcher(chosenURI, options)
.then(response => {
// Make the raw response available in the context.
operations.forEach(operation => operation.setContext({ response }));
return response;
})
//uses fallback, link, and then context to build options
const optsAndBody = operations.map(operation =>
selectHttpOptionsAndBody(
operation,
fallbackHttpConfig,
linkConfig,
contextConfig,
),
);
const loadedBody = optsAndBody.map(({ body }) => body);
const options = optsAndBody[0].options;
// There's no spec for using GET with batches.
if (options.method === 'GET') {
return fromError(
new Error('apollo-link-batch-http does not support GET requests'),
);
}
try {
(options as any).body = serializeFetchParameter(loadedBody, 'Payload');
} catch (parseError) {
return fromError(parseError);
}
let controller;
if (!(options as any).signal) {
const { controller: _controller, signal } = createSignalIfSupported();
controller = _controller;
if (controller) (options as any).signal = signal;
}
!operation.query.definitions.some(definitionIsMutation)
) {
options.method = 'GET';
}
if (options.method === 'GET') {
const { newURI, parseError } = rewriteURIForGET(chosenURI, body);
if (parseError) {
return fromError(parseError);
}
chosenURI = newURI;
} else {
try {
(options as any).body = serializeFetchParameter(body, 'Payload');
} catch (parseError) {
return fromError(parseError);
}
}
return new Observable(observer => {
fetcher(chosenURI, options)
.then(response => {
operation.setContext({ response });
return response;
})
.then(parseAndCheckHttpResponse(operation))
.then(result => {
// we have data and can send it to back up the link chain
observer.next(result);
observer.complete();
return result;
})
it('returns data from the underlying link on a successful retry', async () => {
const retry = new RetryLink({
delay: { initial: 1 },
attempts: { max: 2 },
});
const data = { data: { hello: 'world' } };
const stub = jest.fn();
stub.mockReturnValueOnce(fromError(standardError));
stub.mockReturnValueOnce(Observable.of(data));
const link = ApolloLink.from([retry, stub]);
const [{ values }] = await waitFor(execute(link, { query }));
expect(values).toEqual([data]);
expect(stub).toHaveBeenCalledTimes(2);
});
const linkStub = jest.fn(() => fromError(standardError));
const link = ApolloLink.from([retry, linkStub]);