From 308f27a8fedba09a190d9901c8d12f3d03122ec8 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Sun, 14 Aug 2022 12:31:36 +0100 Subject: [PATCH] Rewrite makeFetchURL with URL/URLSearchParams As these are built-ins in Node 10+ and in all evergreen browsers we support, we can just use `new URL` --- packages/core/src/internal/fetchOptions.ts | 40 +++++++--------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/packages/core/src/internal/fetchOptions.ts b/packages/core/src/internal/fetchOptions.ts index 51874693dc..b278d3307c 100644 --- a/packages/core/src/internal/fetchOptions.ts +++ b/packages/core/src/internal/fetchOptions.ts @@ -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;