UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

56 lines (55 loc) 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rewriteURIForGET = rewriteURIForGET; // For GET operations, returns the given URI rewritten with parameters, or a // parse error. function rewriteURIForGET(chosenURI, body) { // Implement the standard HTTP GET serialization, plus 'extensions'. Note // the extra level of JSON serialization! const queryParams = []; const addQueryParam = (key, value) => { queryParams.push(`${key}=${encodeURIComponent(value)}`); }; if ("query" in body) { addQueryParam("query", body.query); } if (body.operationName) { addQueryParam("operationName", body.operationName); } if (body.variables) { let serializedVariables; try { serializedVariables = JSON.stringify(body.variables); } catch (parseError) { return { parseError }; } addQueryParam("variables", serializedVariables); } if (body.extensions) { let serializedExtensions; try { serializedExtensions = JSON.stringify(body.extensions); } catch (parseError) { return { parseError }; } addQueryParam("extensions", serializedExtensions); } // Reconstruct the URI with added query params. // XXX This assumes that the URI is well-formed and that it doesn't // already contain any of these query params. We could instead use the // URL API and take a polyfill (whatwg-url@6) for older browsers that // don't support URLSearchParams. Note that some browsers (and // versions of whatwg-url) support URL but not URLSearchParams! let fragment = "", preFragment = chosenURI; const fragmentStart = chosenURI.indexOf("#"); if (fragmentStart !== -1) { fragment = chosenURI.substr(fragmentStart); preFragment = chosenURI.substr(0, fragmentStart); } const queryParamsPrefix = preFragment.indexOf("?") === -1 ? "?" : "&"; const newURI = preFragment + queryParamsPrefix + queryParams.join("&") + fragment; return { newURI }; } //# sourceMappingURL=rewriteURIForGET.cjs.map