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 6673abf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/core/src/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('stringifyDocument ', () => {
expect(stringifyDocument(formatted)).toBe(print(formatted));
});

it('should reprint gql documents', () => {
it('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
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 6673abf

Please sign in to comment.