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): Fix regression causing stringifyDocument to not reprint after formatDocument #2871

Merged
merged 7 commits into from
Dec 9, 2022
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/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