Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo breaking change to selectHttpOptionsBody signature #9103

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
) => {
Comment on lines -114 to -119
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to make the printer parameter optional without creating a new function, since selectHttpOptionsAndBody already takes a ...configs rest parameter and thus can't have other optional positional parameters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maaaaybe a

function selectHttpOptionsAndBody(
  operation: Operation,
  printerOrFallbackConfig: Printer | HttpConfig,
  ...configs: Array<HttpConfig>
)

then a typeof function to determine type and then either add to configs and default printer, or use provided printer?

haven't tried it out, but it at least seems like something that should work (overloading sorta sucks, but meh. can be cleaned up in a major, and keeps both backwards compat to 3.4 and 3.5)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that would work (thanks!)… but on balance I think the two-function solution is a bit simpler.

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