Skip to content

Commit

Permalink
fix(core): Fix regression causing stringifyDocument to not reprint af…
Browse files Browse the repository at this point in the history
…ter formatDocument (#2871)

Co-authored-by: Phil Pluckthun <[email protected]>
  • Loading branch information
JoviDeCroock and kitten authored Dec 9, 2022
1 parent 6a4072f commit 95f7156
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-eggs-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix regression in `@urql/core`'s `stringifyDocument` that caused some formatted documents to not be reprinted.
39 changes: 37 additions & 2 deletions packages/core/src/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,43 @@ describe('stringifyDocument ', () => {
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it('should reprint request documents', () => {
const request = createRequest(`query { test { field } }`, {});
const formatted = formatDocument(request.query);
expect(print(formatted)).toMatchInlineSnapshot(`
"{
test {
field
__typename
}
}"
`);
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it('should reprint gql documents', () => {
const request = createRequest(
gql`
query {
test {
field
}
}
`,
{}
);
const formatted = formatDocument(request.query);
expect(print(formatted)).toMatchInlineSnapshot(`
"{
test {
field
__typename
}
}"
`);
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it('should remove comments', () => {
const doc = `
{ #query
Expand Down Expand Up @@ -132,7 +169,6 @@ describe('stringifyDocument ', () => {
.toMatchInlineSnapshot(`
"{
field(arg:
\\"test #1\\")
}"
`);
Expand All @@ -153,7 +189,6 @@ describe('stringifyDocument ', () => {
.toMatchInlineSnapshot(`
"{
field(arg:
\\"\\"\\"
hello
#hello
Expand Down
21 changes: 12 additions & 9 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ const replaceOutsideStrings = (str: string, idx: number) =>
const sanitizeDocument = (node: string): string =>
node.split(GRAPHQL_STRING_RE).map(replaceOutsideStrings).join('').trim();

const prints = new Map<DocumentNode | DefinitionNode, string>();
const docs = new Map<HashValue, KeyedDocumentNode>();

export const stringifyDocument = (
node: string | DefinitionNode | DocumentNode
): string => {
const printed = sanitizeDocument(
typeof node === 'string'
? node
: node.loc && node.loc.source.name === SOURCE_NAME
? node.loc.source.body
: print(node)
);
let printed: string;
if (typeof node === 'string') {
printed = sanitizeDocument(node);
} else if (node.loc && docs.get((node as KeyedDocumentNode).__key) === node) {
printed = node.loc.source.body;
} else {
printed = prints.get(node) || sanitizeDocument(print(node));
prints.set(node, printed);
}

if (typeof node !== 'string' && !node.loc) {
(node as WritableLocation).loc = {
Expand Down Expand Up @@ -67,8 +72,6 @@ const hashDocument = (
return key;
};

const docs = new Map<HashValue, KeyedDocumentNode>();

export const keyDocument = (node: string | DocumentNode): KeyedDocumentNode => {
let key: HashValue;
let query: DocumentNode;
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte-urql/src/mutationStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { print } from 'graphql';
import { createClient } from '@urql/core';
import { get } from 'svelte/store';
import { vi, expect, it, describe } from 'vitest';
Expand Down Expand Up @@ -28,7 +29,7 @@ describe('mutationStore', () => {
expect(get(store).operation.context.url).toBe('https://example.com');
expect(get(store).operation.variables).toBe(variables);

expect(get(store).operation.query.loc?.source.body).toMatchInlineSnapshot(`
expect(print(get(store).operation.query)).toMatchInlineSnapshot(`
"mutation ($input: Example!) {
doExample(input: $input) {
id
Expand Down

0 comments on commit 95f7156

Please sign in to comment.