Skip to content

Commit

Permalink
Rewrite makeFetchURL with URL/URLSearchParams
Browse files Browse the repository at this point in the history
As these are built-ins in Node 10+ and in all evergreen browsers we
support, we can just use `new URL`
  • Loading branch information
kitten committed Aug 14, 2022
1 parent 6a25565 commit 308f27a
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions packages/core/src/internal/fetchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,22 @@ export const makeFetchURL = (
body?: FetchBody
): string => {
const useGETMethod = shouldUseGet(operation);
const url = operation.context.url;
if (!useGETMethod || !body) return url;
if (!useGETMethod || !body) return operation.context.url;

const search: string[] = [];
if (body.operationName) {
search.push('operationName=' + encodeURIComponent(body.operationName));
}

if (body.query) {
search.push(
'query=' +
encodeURIComponent(body.query.replace(/#[^\n\r]+/g, ' ').trim())
);
}

if (body.variables) {
search.push(
'variables=' + encodeURIComponent(stringifyVariables(body.variables))
);
}

if (body.extensions) {
search.push(
'extensions=' + encodeURIComponent(stringifyVariables(body.extensions))
);
}

const finalUrl = `${url}?${search.join('&')}`;
const url = new URL(operation.context.url);
const search = url.searchParams;
if (body.operationName) search.set('operationName', body.operationName);
if (body.query)
search.set('query', body.query.replace(/#[^\n\r]+/g, ' ').trim());
if (body.variables)
search.set('variables', stringifyVariables(body.variables));
if (body.extensions)
search.set('extensions', stringifyVariables(body.extensions));

const finalUrl = url.toString();
if (finalUrl.length > 2047) {
operation.context.preferGetMethod = false;
return url;
return operation.context.url;
}

return finalUrl;
Expand Down

0 comments on commit 308f27a

Please sign in to comment.