Skip to content

Commit

Permalink
Undo breaking change to selectHttpOptionsBody signature (#9103)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored Nov 23, 2021
1 parent 8fc56ed commit 7385725
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.5.5 (unreleased)

### Bug Fixes

- Remove `printer: Printer` positional parameter from publicly-exported `selectHttpOptionsAndBody` function, whose addition in [#8699](https://github.com/apollographql/apollo-client/pull/8699) was a breaking change (starting in Apollo Client 3.5.0) for direct consumers of `selectHttpOptionsAndBody`. <br/>
[@benjamn](https://github.com/benjamn) in [#9103](https://github.com/apollographql/apollo-client/pull/9103)

## Apollo Client 3.5.4 (2021-11-19)

### Notices
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Array [
"resetCaches",
"rewriteURIForGET",
"selectHttpOptionsAndBody",
"selectHttpOptionsAndBodyInternal",
"selectURI",
"serializeFetchParameter",
"setLogVerbosity",
Expand Down Expand Up @@ -118,6 +119,7 @@ Array [
"resetCaches",
"rewriteURIForGET",
"selectHttpOptionsAndBody",
"selectHttpOptionsAndBodyInternal",
"selectURI",
"serializeFetchParameter",
"setLogVerbosity",
Expand Down Expand Up @@ -182,6 +184,7 @@ Array [
"parseAndCheckHttpResponse",
"rewriteURIForGET",
"selectHttpOptionsAndBody",
"selectHttpOptionsAndBodyInternal",
"selectURI",
"serializeFetchParameter",
]
Expand Down
4 changes: 2 additions & 2 deletions src/link/batch-http/batchHttpLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
selectURI,
parseAndCheckHttpResponse,
checkFetcher,
selectHttpOptionsAndBody,
selectHttpOptionsAndBodyInternal,
defaultPrinter,
fallbackHttpConfig,
HttpOptions,
Expand Down Expand Up @@ -96,7 +96,7 @@ export class BatchHttpLink extends ApolloLink {

//uses fallback, link, and then context to build options
const optsAndBody = operations.map(operation =>
selectHttpOptionsAndBody(
selectHttpOptionsAndBodyInternal(
operation,
print,
fallbackHttpConfig,
Expand Down
11 changes: 2 additions & 9 deletions src/link/http/__tests__/selectHttpOptionsAndBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ASTNode, print, stripIgnoredCharacters } from 'graphql';
import { createOperation } from '../../utils/createOperation';
import {
selectHttpOptionsAndBody,
defaultPrinter,
selectHttpOptionsAndBodyInternal,
fallbackHttpConfig,
} from '../selectHttpOptionsAndBody';

Expand All @@ -20,7 +20,6 @@ describe('selectHttpOptionsAndBody', () => {
it('includeQuery allows the query to be ignored', () => {
const { body } = selectHttpOptionsAndBody(
createOperation({}, { query }),
defaultPrinter,
{ http: { includeQuery: false } },
);
expect(body).not.toHaveProperty('query');
Expand All @@ -30,7 +29,6 @@ describe('selectHttpOptionsAndBody', () => {
const extensions = { yo: 'what up' };
const { body } = selectHttpOptionsAndBody(
createOperation({}, { query, extensions }),
defaultPrinter,
{ http: { includeExtensions: true } },
);
expect(body).toHaveProperty('extensions');
Expand All @@ -50,7 +48,6 @@ describe('selectHttpOptionsAndBody', () => {
const extensions = { yo: 'what up' };
const { options, body } = selectHttpOptionsAndBody(
createOperation({}, { query, extensions }),
defaultPrinter,
fallbackHttpConfig,
);

Expand Down Expand Up @@ -81,7 +78,6 @@ describe('selectHttpOptionsAndBody', () => {

const { options, body } = selectHttpOptionsAndBody(
createOperation({}, { query, extensions }),
defaultPrinter,
fallbackHttpConfig,
config,
);
Expand All @@ -107,7 +103,6 @@ describe('selectHttpOptionsAndBody', () => {
const config = { headers };
const { options, body } = selectHttpOptionsAndBody(
createOperation({}, { query }),
defaultPrinter,
fallbackHttpConfig,
config,
);
Expand All @@ -122,18 +117,16 @@ describe('selectHttpOptionsAndBody', () => {
});

it('applies custom printer function when provided', () => {

const customPrinter = (ast: ASTNode, originalPrint: typeof print) => {
return stripIgnoredCharacters(originalPrint(ast));
};

const { body } = selectHttpOptionsAndBody(
const { body } = selectHttpOptionsAndBodyInternal(
createOperation({}, { query }),
customPrinter,
fallbackHttpConfig,
);

expect(body.query).toBe('query SampleQuery{stub{id}}');

});
});
4 changes: 2 additions & 2 deletions src/link/http/createHttpLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { selectURI } from './selectURI';
import { parseAndCheckHttpResponse } from './parseAndCheckHttpResponse';
import { checkFetcher } from './checkFetcher';
import {
selectHttpOptionsAndBody,
selectHttpOptionsAndBodyInternal,
defaultPrinter,
fallbackHttpConfig,
HttpOptions
Expand Down Expand Up @@ -82,7 +82,7 @@ export const createHttpLink = (linkOptions: HttpOptions = {}) => {
};

//uses fallback, link, and then context to build options
const { options, body } = selectHttpOptionsAndBody(
const { options, body } = selectHttpOptionsAndBodyInternal(
operation,
print,
fallbackHttpConfig,
Expand Down
1 change: 1 addition & 0 deletions src/link/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
fallbackHttpConfig,
defaultPrinter,
selectHttpOptionsAndBody,
selectHttpOptionsAndBodyInternal, // needed by ../batch-http but not public
UriFunction
} from './selectHttpOptionsAndBody';
export { checkFetcher } from './checkFetcher';
Expand Down
35 changes: 21 additions & 14 deletions src/link/http/selectHttpOptionsAndBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,27 @@ export const fallbackHttpConfig = {

export const defaultPrinter: Printer = (ast, printer) => printer(ast);

export const selectHttpOptionsAndBody = (
export function selectHttpOptionsAndBody(
operation: Operation,
printer: Printer,
fallbackConfig: HttpConfig,
...configs: Array<HttpConfig>
) => {
let options: HttpConfig & Record<string, any> = {
...fallbackConfig.options,
headers: fallbackConfig.headers,
credentials: fallbackConfig.credentials,
};
let http: HttpQueryOptions = fallbackConfig.http || {};
) {
configs.unshift(fallbackConfig);
return selectHttpOptionsAndBodyInternal(
operation,
defaultPrinter,
...configs,
);
}

export function selectHttpOptionsAndBodyInternal(
operation: Operation,
printer: Printer,
...configs: HttpConfig[]
) {
let options = {} as HttpConfig & Record<string, any>;
let http = {} as HttpQueryOptions;

/*
* use the rest of the configs to populate the options
* configs later in the list will overwrite earlier fields
*/
configs.forEach(config => {
options = {
...options,
Expand All @@ -137,7 +141,10 @@ export const selectHttpOptionsAndBody = (
...headersToLowerCase(config.headers),
},
};
if (config.credentials) options.credentials = config.credentials;

if (config.credentials) {
options.credentials = config.credentials;
}

http = {
...http,
Expand Down

0 comments on commit 7385725

Please sign in to comment.