···
1
+
// See: https://github.com/graphql/graphql-js/blob/976d64b/src/language/__tests__/printer-test.ts
3
+
import { parse } from '../parser';
4
+
import { print } from '../printer';
6
+
describe('Printer: Query document', () => {
7
+
it('prints minimal ast', () => {
10
+
name: { kind: 'Name', value: 'foo' },
12
+
expect(print(ast)).toBe('foo');
15
+
// NOTE: The shim won't throw for invalid AST nodes
16
+
it('returns empty strings for invalid AST', () => {
17
+
const badAST = { random: 'Data' };
18
+
expect(print(badAST)).toBe('');
21
+
it('correctly prints non-query operations without name', () => {
22
+
const queryASTShorthanded = parse('query { id, name }');
23
+
expect(print(queryASTShorthanded)).toBe(dedent`
30
+
const mutationAST = parse('mutation { id, name }');
31
+
expect(print(mutationAST)).toBe(dedent`
38
+
const queryASTWithArtifacts = parse(
39
+
'query ($foo: TestType) @testDirective { id, name }'
41
+
expect(print(queryASTWithArtifacts)).toBe(dedent`
42
+
query ($foo: TestType) @testDirective {
48
+
const mutationASTWithArtifacts = parse(
49
+
'mutation ($foo: TestType) @testDirective { id, name }'
51
+
expect(print(mutationASTWithArtifacts)).toBe(dedent`
52
+
mutation ($foo: TestType) @testDirective {
59
+
it('prints query with variable directives', () => {
60
+
const queryASTWithVariableDirective = parse(
61
+
'query ($foo: TestType = {a: 123} @testDirective(if: true) @test) { id }'
63
+
expect(print(queryASTWithVariableDirective)).toBe(dedent`
64
+
query ($foo: TestType = {a: 123} @testDirective(if: true) @test) {
70
+
it('keeps arguments on one line if line is short (<= 80 chars)', () => {
71
+
const printed = print(
72
+
parse('{trip(wheelchair:false arriveBy:false){dateTime}}')
75
+
expect(printed).toBe(
78
+
trip(wheelchair: false, arriveBy: false) {
86
+
it('prints kitchen sink without altering ast', () => {
87
+
const ast = parse(kitchenSinkQuery, { noLocation: true });
89
+
const astBeforePrintCall = JSON.stringify(ast);
90
+
const printed = print(ast);
91
+
const printedAST = parse(printed, { noLocation: true });
93
+
expect(printedAST).toEqual(ast);
94
+
expect(JSON.stringify(ast)).toBe(astBeforePrintCall);
96
+
expect(printed).toBe(
97
+
dedentString(String.raw`
98
+
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
99
+
whoever123is: node(id: [123, 456]) {
101
+
... on User @onInlineFragment {
104
+
alias: field1(first: 10, after: $foo) @include(if: $foo) {
106
+
...frag @onFragmentSpread
110
+
... @skip(unless: $foo) {
119
+
mutation likeStory @onMutation {
120
+
like(story: 123) @onField {
127
+
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription {
128
+
storyLikeSubscribe(input: $input) {
140
+
fragment frag on Friend @onFragmentDefinition {
141
+
foo(size: $size, bar: $b, obj: {key: "value"})
145
+
unnamed(truthy: true, falsy: false, nullish: null)
157
+
const kitchenSinkQuery = String.raw`
158
+
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
159
+
whoever123is: node(id: [123, 456]) {
161
+
... on User @onInlineFragment {
164
+
alias: field1(first: 10, after: $foo) @include(if: $foo) {
166
+
...frag @onFragmentSpread
170
+
... @skip(unless: $foo) {
178
+
mutation likeStory @onMutation {
179
+
like(story: 123) @onField {
185
+
subscription StoryLikeSubscription(
186
+
$input: StoryLikeSubscribeInput @onVariableDefinition
189
+
storyLikeSubscribe(input: $input) {
200
+
fragment frag on Friend @onFragmentDefinition {
204
+
obj: { key: "value" }
208
+
unnamed(truthy: true, falsy: false, nullish: null)
216
+
function dedentString(string) {
217
+
const trimmedStr = string
218
+
.replace(/^\n*/m, '') // remove leading newline
219
+
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs
220
+
// fixes indentation by removing leading spaces and tabs from each line
222
+
for (const char of trimmedStr) {
223
+
if (char !== ' ' && char !== '\t') {
229
+
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
232
+
function dedent(strings, ...values) {
233
+
let str = strings[0];
234
+
for (let i = 1; i < strings.length; ++i) str += values[i - 1] + strings[i]; // interpolation
235
+
return dedentString(str) + '\n';