Skip to content

Commit 0686af7

Browse files
committed
fix: remove json parse from ast node print
1 parent cac26b4 commit 0686af7

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

.changeset/friendly-tables-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/graphql-fetcher": patch
3+
---
4+
5+
remove json parse from ast node

src/client.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe("gqlClientFetch", () => {
161161
});
162162

163163
it("should use the provided timeout duration", async () => {
164+
vi.useFakeTimers();
164165
const fetcher = initClientFetcher("https://localhost/graphql", {
165166
defaultTimeout: 1,
166167
});
@@ -171,6 +172,8 @@ describe("gqlClientFetch", () => {
171172
myVar: "baz",
172173
});
173174

175+
vi.runAllTimers();
176+
174177
expect(timeoutSpy).toHaveBeenCalledWith(1);
175178

176179
// It should not try to POST the query if the persisted query cannot be parsed
@@ -201,16 +204,19 @@ describe("gqlClientFetch", () => {
201204
expect(fetchMock).toHaveBeenCalledTimes(1);
202205
});
203206

204-
205207
it("should allow passing extra HTTP headers", async () => {
206208
const mockedFetch = fetchMock.mockResponse(responseString);
207-
const gqlResponse = await fetcher(query, {
208-
myVar: "baz",
209-
}, {
210-
headers: {
211-
"X-extra-header": "foo",
209+
const gqlResponse = await fetcher(
210+
query,
211+
{
212+
myVar: "baz",
213+
},
214+
{
215+
headers: {
216+
"X-extra-header": "foo",
217+
},
212218
}
213-
});
219+
);
214220

215221
expect(gqlResponse).toEqual(response);
216222

src/client.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ export const initClientFetcher =
7171
options.signal = AbortSignal.timeout(defaultTimeout);
7272
}
7373

74-
const query = isNode(astNode)
75-
? JSON.parse(print(astNode))
76-
: astNode.toString();
74+
const query = isNode(astNode) ? print(astNode) : astNode.toString();
7775

7876
const operationName = extractOperationName(query);
7977

src/server.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ describe("gqlServerFetch", () => {
233233
});
234234

235235
it("should use the provided timeout duration", async () => {
236+
vi.useFakeTimers();
236237
const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
237238
const gqlServerFetch = initServerFetcher("https://localhost/graphql", {
238239
defaultTimeout: 1,
@@ -241,6 +242,8 @@ describe("gqlServerFetch", () => {
241242

242243
await gqlServerFetch(query, { myVar: "baz" }, {});
243244

245+
vi.runAllTimers();
246+
244247
expect(timeoutSpy).toHaveBeenCalledWith(1);
245248

246249
// It should not try to POST the query if the persisted query cannot be parsed

src/server.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
getQueryType,
1212
pruneObject,
1313
} from "./helpers";
14-
import { print, ASTNode } from "graphql";
14+
import { print } from "graphql";
1515
import { isNode } from "graphql/language/ast";
1616

1717
type Options = {
@@ -51,9 +51,7 @@ export const initServerFetcher =
5151
{ cache, next = {} }: CacheOptions,
5252
signal: AbortSignal = AbortSignal.timeout(defaultTimeout)
5353
): Promise<GqlResponse<TResponse>> => {
54-
const query = isNode(astNode)
55-
? JSON.parse(print(astNode))
56-
: astNode.toString();
54+
const query = isNode(astNode) ? print(astNode) : astNode.toString();
5755

5856
const operationName = extractOperationName(query) || "(GraphQL)";
5957

src/testing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export class TypedDocumentString<TResult, TVariables>
1212

1313
// Choosing a leaf type will trigger the print visitor to output the value directly
1414
// instead of trying to visit the children
15-
kind = "StringValue";
15+
kind = "IntValue";
1616
}

0 commit comments

Comments
 (0)