@platformatic/sql-graphql 0.45.1 → 0.46.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.
@@ -1,1269 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const sqlGraphQL = require('..')
5
- const sqlMapper = require('@platformatic/sql-mapper')
6
- const fastify = require('fastify')
7
- const { clear, connInfo, isMysql, isSQLite } = require('./helper')
8
-
9
- test('nested resolver', async ({ pass, teardown, same, equal }) => {
10
- const app = fastify()
11
- app.register(sqlMapper, {
12
- ...connInfo,
13
- async onDatabaseLoad (db, sql) {
14
- pass('onDatabaseLoad called')
15
-
16
- await clear(db, sql)
17
-
18
- if (isMysql) {
19
- await db.query(sql`
20
- CREATE TABLE categories (
21
- id SERIAL PRIMARY KEY,
22
- name VARCHAR(255)
23
- );
24
- CREATE TABLE pages (
25
- id SERIAL PRIMARY KEY,
26
- title VARCHAR(42),
27
- category_id BIGINT UNSIGNED,
28
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
29
- );
30
- `)
31
- } else if (isSQLite) {
32
- await db.query(sql`
33
- CREATE TABLE categories (
34
- id INTEGER PRIMARY KEY,
35
- name VARCHAR(42)
36
- );
37
- `)
38
- await db.query(sql`
39
- CREATE TABLE pages (
40
- id INTEGER PRIMARY KEY,
41
- title VARCHAR(42),
42
- category_id INTEGER REFERENCES categories(id)
43
- );
44
- `)
45
- } else {
46
- await db.query(sql`
47
- CREATE TABLE categories (
48
- id SERIAL PRIMARY KEY,
49
- name VARCHAR(42)
50
- );
51
- CREATE TABLE pages (
52
- id SERIAL PRIMARY KEY,
53
- title VARCHAR(42),
54
- category_id INTEGER REFERENCES categories(id)
55
- );
56
- `)
57
- }
58
- }
59
- })
60
- app.register(sqlGraphQL)
61
- teardown(app.close.bind(app))
62
-
63
- const categories = [{
64
- name: 'Pets'
65
- }, {
66
- name: 'Food'
67
- }]
68
-
69
- await app.inject({
70
- method: 'POST',
71
- url: '/graphql',
72
- body: {
73
- query: `
74
- mutation batch($inputs: [CategoryInput]!) {
75
- insertCategories(inputs: $inputs) {
76
- id
77
- name
78
- }
79
- }
80
- `,
81
- variables: {
82
- inputs: categories
83
- }
84
- }
85
- })
86
-
87
- {
88
- const res = await app.inject({
89
- method: 'POST',
90
- url: '/graphql',
91
- body: {
92
- query: `
93
- mutation {
94
- savePage(input: { title: "Hello", categoryId: 1 }) {
95
- id
96
- title
97
- category {
98
- id
99
- name
100
- }
101
- }
102
- }
103
- `
104
- }
105
- })
106
- equal(res.statusCode, 200, 'savePage status code')
107
- same(res.json(), {
108
- data: {
109
- savePage: {
110
- id: 1,
111
- title: 'Hello',
112
- category: {
113
- id: 1,
114
- name: 'Pets'
115
- }
116
- }
117
- }
118
- }, 'savePage response')
119
- }
120
-
121
- {
122
- const res = await app.inject({
123
- method: 'POST',
124
- url: '/graphql',
125
- body: {
126
- query: `
127
- query {
128
- getPageById(id: 1) {
129
- id
130
- title
131
- category {
132
- id
133
- name
134
- pages {
135
- id
136
- title
137
- category {
138
- id
139
- name
140
- }
141
- }
142
- }
143
- }
144
- }
145
- `
146
- }
147
- })
148
- equal(res.statusCode, 200, 'pages status code')
149
- same(res.json(), {
150
- data: {
151
- getPageById: {
152
- id: 1,
153
- title: 'Hello',
154
- category: {
155
- id: 1,
156
- name: 'Pets',
157
- pages: [{
158
- id: 1,
159
- title: 'Hello',
160
- category: {
161
- id: 1,
162
- name: 'Pets'
163
- }
164
- }]
165
- }
166
- }
167
- }
168
- }, 'pages response')
169
- }
170
-
171
- // Without ids
172
- {
173
- const res = await app.inject({
174
- method: 'POST',
175
- url: '/graphql',
176
- body: {
177
- query: `
178
- query {
179
- getPageById(id: 1) {
180
- title
181
- category {
182
- name
183
- pages {
184
- title
185
- category {
186
- name
187
- }
188
- }
189
- }
190
- }
191
- }
192
- `
193
- }
194
- })
195
- equal(res.statusCode, 200, 'pages status code')
196
- same(res.json(), {
197
- data: {
198
- getPageById: {
199
- title: 'Hello',
200
- category: {
201
- name: 'Pets',
202
- pages: [{
203
- title: 'Hello',
204
- category: {
205
- name: 'Pets'
206
- }
207
- }]
208
- }
209
- }
210
- }
211
- }, 'pages response')
212
- }
213
-
214
- {
215
- const res = await app.inject({
216
- method: 'POST',
217
- url: '/graphql',
218
- body: {
219
- query: `
220
- query {
221
- categories {
222
- id
223
- name
224
- pages {
225
- id
226
- title
227
- category {
228
- id
229
- name
230
- }
231
- }
232
- }
233
- }
234
- `
235
- }
236
- })
237
- equal(res.statusCode, 200, 'categories.posts status code')
238
- same(res.json(), {
239
- data: {
240
- categories: [{
241
- id: 1,
242
- name: 'Pets',
243
- pages: [{
244
- id: 1,
245
- title: 'Hello',
246
- category: {
247
- id: 1,
248
- name: 'Pets'
249
- }
250
- }]
251
- }, {
252
- id: 2,
253
- name: 'Food',
254
- pages: []
255
- }]
256
- }
257
- }, 'categories.posts response')
258
- }
259
- })
260
-
261
- test('nested resolver with more of 10 rows in nested entity', async ({ pass, teardown, same, equal }) => {
262
- const app = fastify()
263
- app.register(sqlMapper, {
264
- ...connInfo,
265
- async onDatabaseLoad (db, sql) {
266
- pass('onDatabaseLoad called')
267
-
268
- await clear(db, sql)
269
-
270
- if (isMysql) {
271
- await db.query(sql`
272
- CREATE TABLE categories (
273
- id SERIAL PRIMARY KEY,
274
- name VARCHAR(255)
275
- );
276
- CREATE TABLE pages (
277
- id SERIAL PRIMARY KEY,
278
- title VARCHAR(42),
279
- category_id BIGINT UNSIGNED,
280
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
281
- );
282
- `)
283
- } else if (isSQLite) {
284
- await db.query(sql`
285
- CREATE TABLE categories (
286
- id INTEGER PRIMARY KEY,
287
- name VARCHAR(42)
288
- );
289
- `)
290
- await db.query(sql`
291
- CREATE TABLE pages (
292
- id INTEGER PRIMARY KEY,
293
- title VARCHAR(42),
294
- category_id INTEGER REFERENCES categories(id)
295
- );
296
- `)
297
- } else {
298
- await db.query(sql`
299
- CREATE TABLE categories (
300
- id SERIAL PRIMARY KEY,
301
- name VARCHAR(42)
302
- );
303
- CREATE TABLE pages (
304
- id SERIAL PRIMARY KEY,
305
- title VARCHAR(42),
306
- category_id INTEGER REFERENCES categories(id)
307
- );
308
- `)
309
- }
310
- }
311
- })
312
- app.register(sqlGraphQL)
313
- teardown(app.close.bind(app))
314
-
315
- const categories = [
316
- { name: 'Category 01' },
317
- { name: 'Category 02' },
318
- { name: 'Category 03' },
319
- { name: 'Category 04' },
320
- { name: 'Category 05' },
321
- { name: 'Category 06' },
322
- { name: 'Category 07' },
323
- { name: 'Category 08' },
324
- { name: 'Category 09' },
325
- { name: 'Category 10' },
326
- { name: 'Category 11' }
327
- ]
328
- await app.inject({
329
- method: 'POST',
330
- url: '/graphql',
331
- body: {
332
- query: `
333
- mutation batch($inputs: [CategoryInput]!) {
334
- insertCategories(inputs: $inputs) {
335
- id
336
- name
337
- }
338
- }
339
- `,
340
- variables: {
341
- inputs: categories
342
- }
343
- }
344
- })
345
-
346
- const pages = [
347
- { title: 'Page 01', categoryId: 1 },
348
- { title: 'Page 02', categoryId: 2 },
349
- { title: 'Page 03', categoryId: 3 },
350
- { title: 'Page 04', categoryId: 4 },
351
- { title: 'Page 05', categoryId: 5 },
352
- { title: 'Page 06', categoryId: 6 },
353
- { title: 'Page 07', categoryId: 7 },
354
- { title: 'Page 08', categoryId: 8 },
355
- { title: 'Page 09', categoryId: 9 },
356
- { title: 'Page 10', categoryId: 10 },
357
- { title: 'Page 11', categoryId: 11 }
358
- ]
359
- await app.inject({
360
- method: 'POST',
361
- url: '/graphql',
362
- body: {
363
- query: `
364
- mutation batch($inputs: [PageInput]!) {
365
- insertPages(inputs: $inputs) {
366
- id
367
- title
368
- }
369
- }
370
- `,
371
- variables: {
372
- inputs: pages
373
- }
374
- }
375
- })
376
-
377
- {
378
- const res = await app.inject({
379
- method: 'POST',
380
- url: '/graphql',
381
- body: {
382
- query: `
383
- query {
384
- pages(limit: 100) {
385
- id
386
- title
387
- category {
388
- id
389
- }
390
- }
391
- }
392
- `
393
- }
394
- })
395
- equal(res.statusCode, 200, 'pages.category status code')
396
- same(res.json(), {
397
- data: {
398
- pages: [
399
- { id: 1, title: 'Page 01', category: { id: 1 } },
400
- { id: 2, title: 'Page 02', category: { id: 2 } },
401
- { id: 3, title: 'Page 03', category: { id: 3 } },
402
- { id: 4, title: 'Page 04', category: { id: 4 } },
403
- { id: 5, title: 'Page 05', category: { id: 5 } },
404
- { id: 6, title: 'Page 06', category: { id: 6 } },
405
- { id: 7, title: 'Page 07', category: { id: 7 } },
406
- { id: 8, title: 'Page 08', category: { id: 8 } },
407
- { id: 9, title: 'Page 09', category: { id: 9 } },
408
- { id: 10, title: 'Page 10', category: { id: 10 } },
409
- { id: 11, title: 'Page 11', category: { id: 11 } }
410
- ]
411
- }
412
- }, 'pages.category response')
413
- }
414
- })
415
-
416
- test('disable one-too-many', async ({ pass, teardown, same, equal, match }) => {
417
- const app = fastify()
418
- app.register(sqlMapper, {
419
- ...connInfo,
420
- async onDatabaseLoad (db, sql) {
421
- pass('onDatabaseLoad called')
422
-
423
- await clear(db, sql)
424
-
425
- if (isMysql) {
426
- await db.query(sql`
427
- CREATE TABLE categories (
428
- id SERIAL PRIMARY KEY,
429
- name VARCHAR(255)
430
- );
431
- CREATE TABLE pages (
432
- id SERIAL PRIMARY KEY,
433
- title VARCHAR(42),
434
- category_id BIGINT UNSIGNED,
435
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
436
- );
437
- `)
438
- } else if (isSQLite) {
439
- await db.query(sql`
440
- CREATE TABLE categories (
441
- id INTEGER PRIMARY KEY,
442
- name VARCHAR(42)
443
- );
444
- `)
445
- await db.query(sql`
446
- CREATE TABLE pages (
447
- id INTEGER PRIMARY KEY,
448
- title VARCHAR(42),
449
- category_id INTEGER REFERENCES categories(id)
450
- );
451
- `)
452
- } else {
453
- await db.query(sql`
454
- CREATE TABLE categories (
455
- id SERIAL PRIMARY KEY,
456
- name VARCHAR(42)
457
- );
458
- CREATE TABLE pages (
459
- id SERIAL PRIMARY KEY,
460
- title VARCHAR(42),
461
- category_id INTEGER REFERENCES categories(id)
462
- );
463
- `)
464
- }
465
- }
466
- })
467
- app.register(sqlGraphQL, {
468
- resolvers: {
469
- Category: {
470
- pages: false
471
- }
472
- }
473
- })
474
- teardown(app.close.bind(app))
475
-
476
- const categories = [{
477
- name: 'Pets'
478
- }, {
479
- name: 'Food'
480
- }]
481
-
482
- await app.inject({
483
- method: 'POST',
484
- url: '/graphql',
485
- body: {
486
- query: `
487
- mutation batch($inputs: [CategoryInput]!) {
488
- insertCategories(inputs: $inputs) {
489
- id
490
- name
491
- }
492
- }
493
- `,
494
- variables: {
495
- inputs: categories
496
- }
497
- }
498
- })
499
-
500
- {
501
- const res = await app.inject({
502
- method: 'POST',
503
- url: '/graphql',
504
- body: {
505
- query: `
506
- mutation {
507
- savePage(input: { title: "Hello", categoryId: 1 }) {
508
- id
509
- title
510
- category {
511
- id
512
- name
513
- }
514
- }
515
- }
516
- `
517
- }
518
- })
519
- equal(res.statusCode, 200, 'savePage status code')
520
- same(res.json(), {
521
- data: {
522
- savePage: {
523
- id: 1,
524
- title: 'Hello',
525
- category: {
526
- id: 1,
527
- name: 'Pets'
528
- }
529
- }
530
- }
531
- }, 'savePage response')
532
- }
533
-
534
- {
535
- const res = await app.inject({
536
- method: 'POST',
537
- url: '/graphql',
538
- body: {
539
- query: `
540
- query {
541
- getPageById(id: 1) {
542
- id
543
- title
544
- category {
545
- id
546
- name
547
- }
548
- }
549
- }
550
- `
551
- }
552
- })
553
- equal(res.statusCode, 200, 'pages status code')
554
- same(res.json(), {
555
- data: {
556
- getPageById: {
557
- id: 1,
558
- title: 'Hello',
559
- category: {
560
- id: 1,
561
- name: 'Pets'
562
- }
563
- }
564
- }
565
- }, 'pages response')
566
- }
567
-
568
- {
569
- const res = await app.inject({
570
- method: 'POST',
571
- url: '/graphql',
572
- body: {
573
- query: `
574
- query {
575
- categories {
576
- id
577
- name
578
- pages {
579
- id
580
- title
581
- category {
582
- id
583
- name
584
- }
585
- }
586
- }
587
- }
588
- `
589
- }
590
- })
591
- equal(res.statusCode, 400, 'categories.posts status code')
592
- match(res.json(), {
593
- errors: [{
594
- message: 'Cannot query field "pages" on type "Category". Did you mean "name"?'
595
- }]
596
- }, 'categories.posts response')
597
- }
598
- })
599
-
600
- test('disable many-to-one relationship', async ({ pass, teardown, same, equal, match }) => {
601
- const app = fastify()
602
- app.register(sqlMapper, {
603
- ...connInfo,
604
- async onDatabaseLoad (db, sql) {
605
- pass('onDatabaseLoad called')
606
-
607
- await clear(db, sql)
608
-
609
- if (isMysql) {
610
- await db.query(sql`
611
- CREATE TABLE categories (
612
- id SERIAL PRIMARY KEY,
613
- name VARCHAR(255)
614
- );
615
- CREATE TABLE pages (
616
- id SERIAL PRIMARY KEY,
617
- title VARCHAR(42),
618
- category_id BIGINT UNSIGNED,
619
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
620
- );
621
- `)
622
- } else if (isSQLite) {
623
- await db.query(sql`
624
- CREATE TABLE categories (
625
- id INTEGER PRIMARY KEY,
626
- name VARCHAR(42)
627
- );
628
- `)
629
- await db.query(sql`
630
- CREATE TABLE pages (
631
- id INTEGER PRIMARY KEY,
632
- title VARCHAR(42),
633
- category_id INTEGER REFERENCES categories(id)
634
- );
635
- `)
636
- } else {
637
- await db.query(sql`
638
- CREATE TABLE categories (
639
- id SERIAL PRIMARY KEY,
640
- name VARCHAR(42)
641
- );
642
- CREATE TABLE pages (
643
- id SERIAL PRIMARY KEY,
644
- title VARCHAR(42),
645
- category_id INTEGER REFERENCES categories(id)
646
- );
647
- `)
648
- }
649
- }
650
- })
651
- app.register(sqlGraphQL, {
652
- resolvers: {
653
- Page: {
654
- category: false
655
- }
656
- }
657
- })
658
- teardown(app.close.bind(app))
659
-
660
- const categories = [{
661
- name: 'Pets'
662
- }, {
663
- name: 'Food'
664
- }]
665
-
666
- await app.inject({
667
- method: 'POST',
668
- url: '/graphql',
669
- body: {
670
- query: `
671
- mutation batch($inputs: [CategoryInput]!) {
672
- insertCategories(inputs: $inputs) {
673
- id
674
- name
675
- }
676
- }
677
- `,
678
- variables: {
679
- inputs: categories
680
- }
681
- }
682
- })
683
-
684
- {
685
- const res = await app.inject({
686
- method: 'POST',
687
- url: '/graphql',
688
- body: {
689
- query: `
690
- mutation {
691
- savePage(input: { title: "Hello", categoryId: 1 }) {
692
- id
693
- title
694
- }
695
- }
696
- `
697
- }
698
- })
699
- equal(res.statusCode, 200, 'savePage status code')
700
- same(res.json(), {
701
- data: {
702
- savePage: {
703
- id: 1,
704
- title: 'Hello'
705
- }
706
- }
707
- }, 'savePage response')
708
- }
709
-
710
- {
711
- const res = await app.inject({
712
- method: 'POST',
713
- url: '/graphql',
714
- body: {
715
- query: `
716
- query {
717
- getPageById(id: 1) {
718
- id
719
- title
720
- category {
721
- id
722
- name
723
- }
724
- }
725
- }
726
- `
727
- }
728
- })
729
- equal(res.statusCode, 400, 'pages status code')
730
- match(res.json(), {
731
- errors: [{
732
- message: 'Cannot query field "category" on type "Page". Did you mean "categoryId"?'
733
- }]
734
- }, 'pages response')
735
- }
736
-
737
- {
738
- const res = await app.inject({
739
- method: 'POST',
740
- url: '/graphql',
741
- body: {
742
- query: `
743
- query {
744
- categories {
745
- id
746
- name
747
- pages {
748
- id
749
- title
750
- }
751
- }
752
- }
753
- `
754
- }
755
- })
756
- equal(res.statusCode, 200, 'categories.posts status code')
757
- same(res.json(), {
758
- data: {
759
- categories: [{
760
- id: 1,
761
- name: 'Pets',
762
- pages: [{
763
- id: 1,
764
- title: 'Hello'
765
- }]
766
- }, {
767
- id: 2,
768
- name: 'Food',
769
- pages: []
770
- }]
771
- }
772
- }, 'categories.posts response')
773
- }
774
- })
775
-
776
- test('nested update', async ({ pass, teardown, same, equal }) => {
777
- const app = fastify()
778
- app.register(sqlMapper, {
779
- ...connInfo,
780
- async onDatabaseLoad (db, sql) {
781
- pass('onDatabaseLoad called')
782
-
783
- await clear(db, sql)
784
-
785
- if (isMysql) {
786
- await db.query(sql`
787
- CREATE TABLE categories (
788
- id SERIAL PRIMARY KEY,
789
- name VARCHAR(255)
790
- );
791
- CREATE TABLE pages (
792
- id SERIAL PRIMARY KEY,
793
- title VARCHAR(42),
794
- category_id BIGINT UNSIGNED,
795
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
796
- );
797
- `)
798
- } else if (isSQLite) {
799
- await db.query(sql`
800
- CREATE TABLE categories (
801
- id INTEGER PRIMARY KEY,
802
- name VARCHAR(42)
803
- );
804
- `)
805
- await db.query(sql`
806
- CREATE TABLE pages (
807
- id INTEGER PRIMARY KEY,
808
- title VARCHAR(42),
809
- category_id INTEGER REFERENCES categories(id)
810
- );
811
- `)
812
- } else {
813
- await db.query(sql`
814
- CREATE TABLE categories (
815
- id SERIAL PRIMARY KEY,
816
- name VARCHAR(42)
817
- );
818
- CREATE TABLE pages (
819
- id SERIAL PRIMARY KEY,
820
- title VARCHAR(42),
821
- category_id INTEGER REFERENCES categories(id)
822
- );
823
- `)
824
- }
825
- }
826
- })
827
- app.register(sqlGraphQL)
828
- teardown(app.close.bind(app))
829
-
830
- const categories = [{
831
- name: 'Pets'
832
- }, {
833
- name: 'Food'
834
- }]
835
-
836
- await app.inject({
837
- method: 'POST',
838
- url: '/graphql',
839
- body: {
840
- query: `
841
- mutation batch($inputs: [CategoryInput]!) {
842
- insertCategories(inputs: $inputs) {
843
- id
844
- name
845
- }
846
- }
847
- `,
848
- variables: {
849
- inputs: categories
850
- }
851
- }
852
- })
853
-
854
- {
855
- const res = await app.inject({
856
- method: 'POST',
857
- url: '/graphql',
858
- body: {
859
- query: `
860
- mutation {
861
- savePage(input: { title: "Hello", categoryId: 1 }) {
862
- id
863
- title
864
- category {
865
- id
866
- name
867
- }
868
- }
869
- }
870
- `
871
- }
872
- })
873
- equal(res.statusCode, 200, 'savePage status code')
874
- same(res.json(), {
875
- data: {
876
- savePage: {
877
- id: 1,
878
- title: 'Hello',
879
- category: {
880
- id: 1,
881
- name: 'Pets'
882
- }
883
- }
884
- }
885
- }, 'savePage response')
886
- }
887
-
888
- {
889
- const res = await app.inject({
890
- method: 'POST',
891
- url: '/graphql',
892
- body: {
893
- query: `
894
- query {
895
- getPageById(id: 1) {
896
- id
897
- title
898
- category {
899
- id
900
- name
901
- pages {
902
- id
903
- title
904
- category {
905
- id
906
- name
907
- }
908
- }
909
- }
910
- }
911
- }
912
- `
913
- }
914
- })
915
- equal(res.statusCode, 200, 'pages status code')
916
- same(res.json(), {
917
- data: {
918
- getPageById: {
919
- id: 1,
920
- title: 'Hello',
921
- category: {
922
- id: 1,
923
- name: 'Pets',
924
- pages: [{
925
- id: 1,
926
- title: 'Hello',
927
- category: {
928
- id: 1,
929
- name: 'Pets'
930
- }
931
- }]
932
- }
933
- }
934
- }
935
- }, 'pages response')
936
- }
937
-
938
- {
939
- const res = await app.inject({
940
- method: 'POST',
941
- url: '/graphql',
942
- body: {
943
- query: `
944
- query {
945
- categories {
946
- id
947
- name
948
- pages {
949
- id
950
- title
951
- category {
952
- id
953
- name
954
- }
955
- }
956
- }
957
- }
958
- `
959
- }
960
- })
961
- equal(res.statusCode, 200, 'categories.posts status code')
962
- same(res.json(), {
963
- data: {
964
- categories: [{
965
- id: 1,
966
- name: 'Pets',
967
- pages: [{
968
- id: 1,
969
- title: 'Hello',
970
- category: {
971
- id: 1,
972
- name: 'Pets'
973
- }
974
- }]
975
- }, {
976
- id: 2,
977
- name: 'Food',
978
- pages: []
979
- }]
980
- }
981
- }, 'categories.posts response')
982
- }
983
-
984
- {
985
- const res = await app.inject({
986
- method: 'POST',
987
- url: '/graphql',
988
- body: {
989
- query: `
990
- mutation {
991
- savePage(input: { title: "Updated", id: 1 }) {
992
- id
993
- title
994
- category {
995
- id
996
- name
997
- }
998
- }
999
- }
1000
- `
1001
- }
1002
- })
1003
- equal(res.statusCode, 200, 'savePage status code')
1004
- same(res.json(), {
1005
- data: {
1006
- savePage: {
1007
- id: 1,
1008
- title: 'Updated',
1009
- category: {
1010
- id: 1,
1011
- name: 'Pets'
1012
- }
1013
- }
1014
- }
1015
- }, 'savePage response')
1016
- }
1017
- })
1018
-
1019
- test('nested resolver without `id` suffix', async ({ pass, teardown, same, equal }) => {
1020
- const app = fastify()
1021
- app.register(sqlMapper, {
1022
- ...connInfo,
1023
- async onDatabaseLoad (db, sql) {
1024
- pass('onDatabaseLoad called')
1025
-
1026
- await clear(db, sql)
1027
-
1028
- if (isMysql) {
1029
- await db.query(sql`
1030
- CREATE TABLE categories (
1031
- id SERIAL PRIMARY KEY,
1032
- name VARCHAR(255)
1033
- );
1034
- CREATE TABLE pages (
1035
- id SERIAL PRIMARY KEY,
1036
- title VARCHAR(42),
1037
- category BIGINT UNSIGNED,
1038
- FOREIGN KEY (category) REFERENCES categories(id) ON DELETE CASCADE
1039
- );
1040
- `)
1041
- } else if (isSQLite) {
1042
- await db.query(sql`
1043
- CREATE TABLE categories (
1044
- id INTEGER PRIMARY KEY,
1045
- name VARCHAR(42)
1046
- );
1047
- `)
1048
- await db.query(sql`
1049
- CREATE TABLE pages (
1050
- id INTEGER PRIMARY KEY,
1051
- title VARCHAR(42),
1052
- category INTEGER REFERENCES categories(id)
1053
- );
1054
- `)
1055
- } else {
1056
- await db.query(sql`
1057
- CREATE TABLE categories (
1058
- id SERIAL PRIMARY KEY,
1059
- name VARCHAR(42)
1060
- );
1061
- CREATE TABLE pages (
1062
- id SERIAL PRIMARY KEY,
1063
- title VARCHAR(42),
1064
- category INTEGER REFERENCES categories(id)
1065
- );
1066
- `)
1067
- }
1068
- }
1069
- })
1070
- app.register(sqlGraphQL)
1071
- teardown(app.close.bind(app))
1072
-
1073
- const categories = [{
1074
- name: 'Pets'
1075
- }, {
1076
- name: 'Food'
1077
- }]
1078
-
1079
- await app.inject({
1080
- method: 'POST',
1081
- url: '/graphql',
1082
- body: {
1083
- query: `
1084
- mutation batch($inputs: [CategoryInput]!) {
1085
- insertCategories(inputs: $inputs) {
1086
- id
1087
- name
1088
- }
1089
- }
1090
- `,
1091
- variables: {
1092
- inputs: categories
1093
- }
1094
- }
1095
- })
1096
-
1097
- {
1098
- const res = await app.inject({
1099
- method: 'POST',
1100
- url: '/graphql',
1101
- body: {
1102
- query: `
1103
- mutation {
1104
- savePage(input: { title: "Hello", category: 1 }) {
1105
- id
1106
- title
1107
- category {
1108
- id
1109
- name
1110
- }
1111
- }
1112
- }
1113
- `
1114
- }
1115
- })
1116
- equal(res.statusCode, 200, 'savePage status code')
1117
- same(res.json(), {
1118
- data: {
1119
- savePage: {
1120
- id: 1,
1121
- title: 'Hello',
1122
- category: {
1123
- id: 1,
1124
- name: 'Pets'
1125
- }
1126
- }
1127
- }
1128
- }, 'savePage response')
1129
- }
1130
-
1131
- {
1132
- const res = await app.inject({
1133
- method: 'POST',
1134
- url: '/graphql',
1135
- body: {
1136
- query: `
1137
- query {
1138
- getPageById(id: 1) {
1139
- id
1140
- title
1141
- category {
1142
- id
1143
- name
1144
- pages {
1145
- id
1146
- title
1147
- category {
1148
- id
1149
- name
1150
- }
1151
- }
1152
- }
1153
- }
1154
- }
1155
- `
1156
- }
1157
- })
1158
- equal(res.statusCode, 200, 'pages status code')
1159
- same(res.json(), {
1160
- data: {
1161
- getPageById: {
1162
- id: 1,
1163
- title: 'Hello',
1164
- category: {
1165
- id: 1,
1166
- name: 'Pets',
1167
- pages: [{
1168
- id: 1,
1169
- title: 'Hello',
1170
- category: {
1171
- id: 1,
1172
- name: 'Pets'
1173
- }
1174
- }]
1175
- }
1176
- }
1177
- }
1178
- }, 'pages response')
1179
- }
1180
-
1181
- // Without ids
1182
- {
1183
- const res = await app.inject({
1184
- method: 'POST',
1185
- url: '/graphql',
1186
- body: {
1187
- query: `
1188
- query {
1189
- getPageById(id: 1) {
1190
- title
1191
- category {
1192
- name
1193
- pages {
1194
- title
1195
- category {
1196
- name
1197
- }
1198
- }
1199
- }
1200
- }
1201
- }
1202
- `
1203
- }
1204
- })
1205
- equal(res.statusCode, 200, 'pages status code')
1206
- same(res.json(), {
1207
- data: {
1208
- getPageById: {
1209
- title: 'Hello',
1210
- category: {
1211
- name: 'Pets',
1212
- pages: [{
1213
- title: 'Hello',
1214
- category: {
1215
- name: 'Pets'
1216
- }
1217
- }]
1218
- }
1219
- }
1220
- }
1221
- }, 'pages response')
1222
- }
1223
-
1224
- {
1225
- const res = await app.inject({
1226
- method: 'POST',
1227
- url: '/graphql',
1228
- body: {
1229
- query: `
1230
- query {
1231
- categories {
1232
- id
1233
- name
1234
- pages {
1235
- id
1236
- title
1237
- category {
1238
- id
1239
- name
1240
- }
1241
- }
1242
- }
1243
- }
1244
- `
1245
- }
1246
- })
1247
- equal(res.statusCode, 200, 'categories.posts status code')
1248
- same(res.json(), {
1249
- data: {
1250
- categories: [{
1251
- id: 1,
1252
- name: 'Pets',
1253
- pages: [{
1254
- id: 1,
1255
- title: 'Hello',
1256
- category: {
1257
- id: 1,
1258
- name: 'Pets'
1259
- }
1260
- }]
1261
- }, {
1262
- id: 2,
1263
- name: 'Food',
1264
- pages: []
1265
- }]
1266
- }
1267
- }, 'categories.posts response')
1268
- }
1269
- })