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

fix(core): Allow path-only URLs to be used with GET requests #3514

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/curvy-sheep-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Allow `url` to be a plain, non-URL pathname (i.e. `/api/graphql`) to be used with `preferGetMethod`
20 changes: 20 additions & 0 deletions packages/core/src/internal/fetchOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe('makeFetchURL', () => {
);
});

it('returns the URL by default when only a path is provided', () => {
const operation = makeOperation(queryOperation.kind, queryOperation, {
url: '/graphql',
});
const body = makeFetchBody(operation);
expect(makeFetchURL(operation, body)).toBe('/graphql');
});

it('returns a query parameter URL when GET is preferred', () => {
const operation = makeOperation(queryOperation.kind, queryOperation, {
...queryOperation.context,
Expand All @@ -64,6 +72,18 @@ describe('makeFetchURL', () => {
);
});

it('returns a query parameter URL when GET is preferred and only a path is provided', () => {
const operation = makeOperation(queryOperation.kind, queryOperation, {
url: '/graphql',
preferGetMethod: true,
});

const body = makeFetchBody(operation);
expect(makeFetchURL(operation, body)).toMatchInlineSnapshot(
'"/graphql?query=query+getUser%28%24name%3A+String%29+%7B%0A++user%28name%3A+%24name%29+%7B%0A++++id%0A++++firstName%0A++++lastName%0A++%7D%0A%7D&operationName=getUser&variables=%7B%22name%22%3A%22Clara%22%7D"'
);
});

it('returns the URL without query parameters when it exceeds given length', () => {
const operation = makeOperation(queryOperation.kind, queryOperation, {
...queryOperation.context,
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/internal/fetchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ export const makeFetchURL = (
operation.kind === 'query' && operation.context.preferGetMethod;
if (!useGETMethod || !body) return operation.context.url;

const url = new URL(operation.context.url);
const urlParts = splitOutSearchParams(operation.context.url);
for (const key in body) {
const value = body[key];
if (value) {
url.searchParams.set(
urlParts[1].set(
key,
typeof value === 'object' ? stringifyVariables(value) : value
);
}
}

const finalUrl = url.toString();
const finalUrl = urlParts.join('?');
if (finalUrl.length > 2047 && useGETMethod !== 'force') {
operation.context.preferGetMethod = false;
return operation.context.url;
Expand All @@ -75,6 +74,15 @@ export const makeFetchURL = (
return finalUrl;
};

const splitOutSearchParams = (
url: string
): readonly [string, URLSearchParams] => {
const start = url.indexOf('?');
return start > -1
? [url.slice(0, start), new URLSearchParams(url.slice(start + 1))]
: [url, new URLSearchParams()];
};

/** Serializes a {@link FetchBody} into a {@link RequestInit.body} format. */
const serializeBody = (
operation: Operation,
Expand Down
Loading