@luvio/graphql-parser 0.126.0 → 0.126.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/argument-node.d.ts +4 -0
  2. package/dist/ast.d.ts +95 -0
  3. package/dist/constants.d.ts +12 -0
  4. package/dist/directive-node.d.ts +5 -0
  5. package/dist/document.d.ts +3 -0
  6. package/dist/field-node.d.ts +4 -0
  7. package/dist/fragment-spread-node.d.ts +4 -0
  8. package/dist/fragment.d.ts +3 -0
  9. package/dist/gql.d.ts +51 -0
  10. package/dist/inline-fragment-node.d.ts +4 -0
  11. package/dist/luvioGraphqlParser.js +12945 -0
  12. package/dist/luvioGraphqlParser.mjs +12923 -0
  13. package/dist/main.d.ts +18 -0
  14. package/dist/metaschema.d.ts +10 -0
  15. package/dist/operation/index.d.ts +3 -0
  16. package/dist/operation/query/index.d.ts +6 -0
  17. package/dist/type-node.d.ts +3 -0
  18. package/dist/util/language.d.ts +9 -0
  19. package/dist/value-node.d.ts +4 -0
  20. package/dist/variable-definition.d.ts +4 -0
  21. package/dist/visitor.d.ts +4 -0
  22. package/package.json +4 -1
  23. package/babel.config.js +0 -1
  24. package/jest.config.js +0 -8
  25. package/project.json +0 -7
  26. package/rollup.config.js +0 -24
  27. package/scripts/cli.mjs +0 -18
  28. package/src/__tests__/ast.json +0 -403
  29. package/src/__tests__/ast.spec.ts +0 -109
  30. package/src/__tests__/astNoLoc.json +0 -147
  31. package/src/__tests__/gql.spec.ts +0 -665
  32. package/src/__tests__/main.spec.ts +0 -651
  33. package/src/__tests__/metaschema.spec.ts +0 -230
  34. package/src/__tests__/type-node.spec.ts +0 -82
  35. package/src/argument-node.ts +0 -18
  36. package/src/ast.ts +0 -200
  37. package/src/constants.ts +0 -14
  38. package/src/directive-node.ts +0 -36
  39. package/src/document.ts +0 -29
  40. package/src/field-node.ts +0 -72
  41. package/src/fragment-spread-node.ts +0 -28
  42. package/src/fragment.ts +0 -46
  43. package/src/gql.ts +0 -222
  44. package/src/inline-fragment-node.ts +0 -31
  45. package/src/main.ts +0 -134
  46. package/src/metaschema.ts +0 -162
  47. package/src/operation/index.ts +0 -12
  48. package/src/operation/query/index.ts +0 -78
  49. package/src/type-node.ts +0 -40
  50. package/src/util/language.ts +0 -10
  51. package/src/value-node.ts +0 -71
  52. package/src/variable-definition.ts +0 -37
  53. package/src/visitor.ts +0 -63
  54. package/tsconfig.json +0 -9
@@ -1,665 +0,0 @@
1
- import { gql, docMap, astResolver, processSubstitutions, stripLocation } from '../gql';
2
- import ast from './ast.json';
3
- import astNoLoc from './astNoLoc.json';
4
-
5
- var mockParse;
6
-
7
- jest.mock('graphql/language', () => {
8
- const originalModule = jest.requireActual('graphql/language');
9
- // we keep the original behavior, just wrapping it with a mock so we can spy
10
- mockParse = jest.fn().mockImplementation((source, options) => {
11
- return originalModule.parse(source, options);
12
- });
13
-
14
- return {
15
- __esModule: true,
16
- ...originalModule,
17
- parse: mockParse,
18
- };
19
- });
20
-
21
- const testProcessSubstitution = (literals: any, ...args: any[]) => {
22
- return processSubstitutions(literals, args);
23
- };
24
-
25
- beforeEach(() => {
26
- jest.clearAllMocks();
27
- docMap.clear();
28
- });
29
-
30
- const mockFragment1 = `fragment fragmentName1 on SomeType1 { someField1 }`;
31
- const mockFragment2 = `fragment fragmentName2 on SomeType2 { someField2 }`;
32
- const mockTwoFragments = `${mockFragment1} ${mockFragment2}`;
33
-
34
- describe('processSubstitutions', () => {
35
- it('should return correct operation string when no substitution is present', () => {
36
- const result = testProcessSubstitution/* GraphQL */ `
37
- query accounts {
38
- uiapi {
39
- query {
40
- Account {
41
- edges {
42
- node {
43
- Id
44
- Name {
45
- value
46
- }
47
- }
48
- }
49
- }
50
- }
51
- }
52
- }
53
- `;
54
-
55
- expect(result.operationString.trim()).toBe(
56
- /* GraphQL */ `
57
- query accounts {
58
- uiapi {
59
- query {
60
- Account {
61
- edges {
62
- node {
63
- Id
64
- Name {
65
- value
66
- }
67
- }
68
- }
69
- }
70
- }
71
- }
72
- }
73
- `.trim()
74
- );
75
- expect(result.fragments).toStrictEqual([]);
76
- });
77
-
78
- it('should substitute strings correctly in the operation string', () => {
79
- const field = `Contact`;
80
- const subField = `displayName`;
81
- const result = testProcessSubstitution/* GraphQL */ `
82
- query accounts {
83
- uiapi {
84
- query {
85
- Account {
86
- edges {
87
- node {
88
- Id
89
- Name {
90
- value
91
- ${subField}
92
- }
93
- ${field}
94
- }
95
- }
96
- }
97
- }
98
- }
99
- }
100
- `;
101
-
102
- expect(result.operationString.trim()).toBe(
103
- /* GraphQL */ `
104
- query accounts {
105
- uiapi {
106
- query {
107
- Account {
108
- edges {
109
- node {
110
- Id
111
- Name {
112
- value
113
- displayName
114
- }
115
- Contact
116
- }
117
- }
118
- }
119
- }
120
- }
121
- }
122
- `.trim()
123
- );
124
- expect(result.fragments).toStrictEqual([]);
125
- });
126
-
127
- it('should return correct operation string and fragments', () => {
128
- const fragment = gql(mockFragment1);
129
- const result = testProcessSubstitution/* GraphQL */ `
130
- query accounts {
131
- uiapi {
132
- query {
133
- Account {
134
- edges {
135
- node {
136
- Id
137
- Name {
138
- value
139
- }
140
- ...fragmentName1
141
- }
142
- }
143
- }
144
- }
145
- }
146
- }
147
- ${fragment}
148
- `;
149
-
150
- expect(result.operationString.trim()).toBe(
151
- /* GraphQL */ `
152
- query accounts {
153
- uiapi {
154
- query {
155
- Account {
156
- edges {
157
- node {
158
- Id
159
- Name {
160
- value
161
- }
162
- ...fragmentName1
163
- }
164
- }
165
- }
166
- }
167
- }
168
- }
169
- `.trim()
170
- );
171
-
172
- expect(result.fragments.length).toBe(1);
173
- expect(result.fragments[0].kind).toBe('FragmentDefinition');
174
- });
175
-
176
- it('should return correct operation string and fragments when has multiple fragments in one substitute', () => {
177
- const fragment = gql(mockTwoFragments);
178
- const result = testProcessSubstitution/* GraphQL */ `
179
- query accounts {
180
- uiapi {
181
- query {
182
- Account {
183
- edges {
184
- node {
185
- Id
186
- Name {
187
- value
188
- }
189
- ...fragmentName1
190
- ...fragmentName2
191
- }
192
- }
193
- }
194
- }
195
- }
196
- }
197
- ${fragment}
198
- `;
199
-
200
- expect(result.operationString.trim()).toBe(
201
- /* GraphQL */ `
202
- query accounts {
203
- uiapi {
204
- query {
205
- Account {
206
- edges {
207
- node {
208
- Id
209
- Name {
210
- value
211
- }
212
- ...fragmentName1
213
- ...fragmentName2
214
- }
215
- }
216
- }
217
- }
218
- }
219
- }
220
- `.trim()
221
- );
222
-
223
- expect(result.fragments.length).toBe(2);
224
- expect(result.fragments[0].kind).toBe('FragmentDefinition');
225
- expect(result.fragments[0].name.value).toBe('fragmentName1');
226
- expect(result.fragments[1].kind).toBe('FragmentDefinition');
227
- expect(result.fragments[1].name.value).toBe('fragmentName2');
228
- });
229
-
230
- it('should return correct operation string and fragments when both type of substitutions are present', () => {
231
- const field = `Contact`;
232
- const subField = `displayValue`;
233
- const fragment1 = gql(mockFragment1);
234
- const fragment2 = gql(mockFragment2);
235
-
236
- const result = testProcessSubstitution/* GraphQL */ `
237
- query accounts {
238
- uiapi {
239
- query {
240
- Account {
241
- edges {
242
- node {
243
- Id
244
- Name {
245
- value
246
- ${subField}
247
- }
248
- ${field}
249
- ...fragmentName1
250
- ...fragmentName2
251
- }
252
- }
253
- }
254
- }
255
- }
256
- }
257
- ${fragment1}
258
- ${fragment2}
259
- `;
260
-
261
- expect(result.operationString.trim()).toBe(
262
- /* GraphQL */ `
263
- query accounts {
264
- uiapi {
265
- query {
266
- Account {
267
- edges {
268
- node {
269
- Id
270
- Name {
271
- value
272
- displayValue
273
- }
274
- Contact
275
- ...fragmentName1
276
- ...fragmentName2
277
- }
278
- }
279
- }
280
- }
281
- }
282
- }
283
- `.trim()
284
- );
285
- expect(result.fragments.length).toBe(2);
286
- expect(result.fragments[0].kind).toBe('FragmentDefinition');
287
- expect(result.fragments[1].kind).toBe('FragmentDefinition');
288
- });
289
-
290
- it('should throw if the fragment document is not found', () => {
291
- expect(() => {
292
- return testProcessSubstitution/* GraphQL */ `
293
- query accounts {
294
- uiapi {
295
- query {
296
- Account {
297
- edges {
298
- node {
299
- Id
300
- Name {
301
- value
302
- }
303
- ...fragmentName1
304
- }
305
- }
306
- }
307
- }
308
- }
309
- }
310
- ${{}}
311
- `;
312
- }).toThrowError('Invalid substitution fragment');
313
- });
314
-
315
- it('should throw if an unsupported substitution is found', () => {
316
- expect(() => {
317
- return testProcessSubstitution/* GraphQL */ `
318
- query accounts {
319
- uiapi {
320
- query {
321
- Account {
322
- edges {
323
- node {
324
- Id
325
- Name {
326
- value
327
- }
328
- ...fragmentName1
329
- }
330
- }
331
- }
332
- }
333
- }
334
- }
335
- ${true}
336
- `;
337
- }).toThrowError('Unsupported substitution type');
338
- });
339
- });
340
-
341
- describe('gql', () => {
342
- it(`should call the parsing api when response isn't cached`, () => {
343
- const ref = gql`
344
- query accounts {
345
- uiapi {
346
- query {
347
- Account {
348
- edges {
349
- node {
350
- Id
351
- Name {
352
- value
353
- }
354
- }
355
- }
356
- }
357
- }
358
- }
359
- }
360
- `;
361
- const doc = astResolver(ref);
362
-
363
- expect(mockParse).toHaveBeenCalledTimes(1);
364
- expect(doc.kind).toEqual('Document');
365
- });
366
-
367
- it(`should call the parsing api when query is passed as an argument and isn't cached`, () => {
368
- const ref = gql(/* GraphQL */ `
369
- query accounts {
370
- uiapi {
371
- query {
372
- Account {
373
- edges {
374
- node {
375
- Id
376
- Name {
377
- value
378
- }
379
- }
380
- }
381
- }
382
- }
383
- }
384
- }
385
- `);
386
- const doc = astResolver(ref);
387
-
388
- expect(mockParse).toHaveBeenCalledTimes(1);
389
- expect(doc.kind).toEqual('Document');
390
- });
391
-
392
- it('should get the document from cache when same query is passed again', () => {
393
- // Populate doc map cache
394
- const ref1 = gql`
395
- query accounts {
396
- uiapi {
397
- query {
398
- Account {
399
- edges {
400
- node {
401
- Id
402
- Name {
403
- value
404
- }
405
- }
406
- }
407
- }
408
- }
409
- }
410
- }
411
- `;
412
-
413
- const ref2 = gql`
414
- query accounts {
415
- uiapi {
416
- query {
417
- Account {
418
- edges {
419
- node {
420
- Id
421
- Name {
422
- value
423
- }
424
- }
425
- }
426
- }
427
- }
428
- }
429
- }
430
- `;
431
- const doc1 = astResolver(ref1);
432
- const doc2 = astResolver(ref2);
433
-
434
- expect(mockParse).toHaveBeenCalledTimes(1);
435
- expect(doc2.kind).toEqual('Document');
436
- expect(doc1).toBe(doc2);
437
- });
438
-
439
- it('should get the document from cache when we get a semantically similar query again', () => {
440
- // Populate doc map cache
441
- const ref1 = gql`
442
- query accounts {
443
- uiapi {
444
- query {
445
- Account {
446
- edges {
447
- node {
448
- Id
449
- Name {
450
- value
451
- displayValue
452
- }
453
- }
454
- }
455
- }
456
- }
457
- }
458
- }
459
- `;
460
-
461
- const ref2 = gql`
462
- query accounts {
463
- uiapi {
464
- query {
465
- Account {
466
- edges {
467
- node {
468
- # This is a comment
469
- Id
470
-
471
- Name {
472
- value
473
- displayValue
474
- }
475
- }
476
- }
477
- }
478
- }
479
- }
480
- }
481
- `;
482
- const doc1 = astResolver(ref1);
483
- const doc2 = astResolver(ref2);
484
-
485
- expect(mockParse).toHaveBeenCalledTimes(1);
486
- expect(doc2.kind).toEqual('Document');
487
- expect(doc1).toBe(doc2);
488
- });
489
-
490
- it('should return the document with appended substitutions', () => {
491
- const field = `Contact`;
492
- const fragment = gql(mockFragment1);
493
- const ref = gql`
494
- query accounts {
495
- uiapi {
496
- query {
497
- Account {
498
- edges {
499
- node {
500
- Id
501
- Name {
502
- value
503
- }
504
- ${field}
505
- ...ReusableFragment1
506
- }
507
- }
508
- }
509
- }
510
- }
511
- }
512
- ${fragment}
513
- `;
514
- const doc = astResolver(ref);
515
- const fragmentDoc = astResolver(fragment);
516
-
517
- expect(mockParse).toHaveBeenCalledTimes(2);
518
- expect(fragmentDoc.kind).toBe('Document');
519
- expect(fragmentDoc.definitions.length).toBe(1);
520
- expect(doc.kind).toBe('Document');
521
- expect(doc.definitions.length).toBe(2);
522
- expect(doc.definitions[0].kind).toBe('OperationDefinition');
523
- expect(doc.definitions[1].kind).toBe('FragmentDefinition');
524
- });
525
-
526
- describe('invalid queries', () => {
527
- it('should return null in case of an invalid query - in prod', () => {
528
- process.env.NODE_ENV = 'production';
529
- try {
530
- expect(gql(undefined)).toEqual(null);
531
- expect(mockParse).not.toHaveBeenCalled();
532
- } finally {
533
- process.env.NODE_ENV = 'test';
534
- }
535
- });
536
-
537
- it('should throw in case of an undefined query', () => {
538
- // eslint-disable-next-line no-unused-expressions
539
- expect(() => gql(undefined)).toThrowError('Invalid query');
540
- expect(mockParse).not.toHaveBeenCalled();
541
- });
542
-
543
- it('should throw in case of an invalid query', () => {
544
- // eslint-disable-next-line no-unused-expressions
545
- expect(() => gql``).toThrowError('Invalid query');
546
- expect(mockParse).not.toHaveBeenCalled();
547
- });
548
-
549
- it('should return null in case of an invalid query - prod', () => {
550
- process.env.NODE_ENV = 'production';
551
- try {
552
- expect(gql``).toBe(null);
553
- expect(mockParse).not.toHaveBeenCalled();
554
- } finally {
555
- process.env.NODE_ENV = 'test';
556
- }
557
- });
558
-
559
- it('should throw in case a substitution reference is invalid', () => {
560
- const sub = '';
561
- expect(
562
- () => gql`
563
- query accounts {
564
- uiapi {
565
- query {
566
- Account {
567
- edges {
568
- node {
569
- Id
570
- Name {
571
- value
572
- }
573
- ${sub}
574
- ...someFragment
575
- }
576
- }
577
- }
578
- }
579
- }
580
- }
581
- ${{}}
582
- `
583
- ).toThrow(`Invalid substitution fragment`);
584
- expect(mockParse).not.toHaveBeenCalled();
585
- });
586
-
587
- it('should return null when in production environment and passed an invalid reference', () => {
588
- process.env.NODE_ENV = 'production';
589
- try {
590
- const ref = gql`
591
- query accounts {
592
- uiapi {
593
- query {
594
- Account {
595
- edges {
596
- node {
597
- Id
598
- Name {
599
- value
600
- }
601
- ...someFragment
602
- }
603
- }
604
- }
605
- }
606
- }
607
- }
608
- ${{}}
609
- `;
610
-
611
- expect(ref).toBe(null);
612
- } finally {
613
- process.env.NODE_ENV = 'test';
614
- }
615
- });
616
-
617
- it('should return null when in production environment and parsed document is invalid', () => {
618
- process.env.NODE_ENV = 'production';
619
- try {
620
- mockParse.mockImplementation(() => ({}));
621
- const ref = gql`
622
- query accounts {
623
- uiapi {
624
- query {
625
- Account {
626
- edges {
627
- node {
628
- Id
629
- Name {
630
- value
631
- }
632
- ...someFragment
633
- }
634
- }
635
- }
636
- }
637
- }
638
- }
639
- `;
640
- expect(ref).toBe(null);
641
- } finally {
642
- process.env.NODE_ENV = 'test';
643
- }
644
- });
645
- });
646
- });
647
-
648
- describe('stripLocation', () => {
649
- it('should remove all loc references from the doc', () => {
650
- const doc = stripLocation({
651
- loc: 1,
652
- arr: [{ loc: 2, a: 3 }],
653
- obj: { b: 4, loc: {} },
654
- noLoc: 5,
655
- });
656
-
657
- expect(doc).toStrictEqual({ arr: [{ a: 3 }], obj: { b: 4 }, noLoc: 5 });
658
- });
659
-
660
- it('should remove all loc references from the doc and deeply nested objects', () => {
661
- const doc = stripLocation(ast);
662
-
663
- expect(doc).toStrictEqual(astNoLoc);
664
- });
665
- });