Skip to content

Commit

Permalink
fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 9, 2022
1 parent 82f221e commit 189d811
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
31 changes: 27 additions & 4 deletions packages/core/src/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ describe('createRequest', () => {
});
});

describe('stringifyDocument ', () => {
describe.only('stringifyDocument ', () => {
it('should reprint formatted documents', () => {
const doc = parse('{ test { field } }');
const formatted = formatDocument(doc);
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it('should reprint gql documents', () => {
it.only('should reprint request documents', () => {
const request = createRequest(`query { test { field } }`, {});
const formatted = formatDocument(request.query);
expect(print(formatted)).toMatchInlineSnapshot(`
Expand All @@ -106,6 +106,29 @@ describe('stringifyDocument ', () => {
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it.only('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 @@ -145,7 +168,7 @@ describe('stringifyDocument ', () => {
expect(stringifyDocument(createRequest(doc, undefined).query))
.toMatchInlineSnapshot(`
"{
field(arg:
field(arg:
\\"test #1\\")
}"
Expand All @@ -166,7 +189,7 @@ describe('stringifyDocument ', () => {
expect(stringifyDocument(createRequest(doc, undefined).query))
.toMatchInlineSnapshot(`
"{
field(arg:
field(arg:
\\"\\"\\"
hello
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/typenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InlineFragmentNode,
Kind,
visit,
print,
} from 'graphql';

import { KeyedDocumentNode, keyDocument } from './request';
Expand Down Expand Up @@ -83,6 +84,11 @@ export const formatDocument = <T extends DocumentNode>(node: T): T => {
});

formattedDocs.set(query.__key, result);
if (typeof node !== 'string' && node.loc?.source.name === 'gql') {
const printed = print(result);
node.loc.source.body = printed;
(node.loc as any).end = printed.length;
}
}

return (result as unknown) as T;
Expand Down

0 comments on commit 189d811

Please sign in to comment.