@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,785 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { tmpdir } = require('os')
5
- const { randomUUID } = require('crypto')
6
- const { join } = require('path')
7
- const sqlGraphQL = require('..')
8
- const sqlMapper = require('@platformatic/sql-mapper')
9
- const fastify = require('fastify')
10
- const { isSQLite, connInfo, isMysql, clear } = require('./helper')
11
-
12
- test('should fail when an unknown foreign key relationship exists', { skip: !isSQLite }, async ({ pass, rejects, same, teardown }) => {
13
- const file = join(tmpdir(), randomUUID())
14
- const app = fastify()
15
- app.register(sqlMapper, {
16
- connectionString: `sqlite://${file}`,
17
- async onDatabaseLoad (db, sql) {
18
- pass('onDatabaseLoad called')
19
-
20
- await db.query(sql`
21
- CREATE TABLE categories (
22
- id INTEGER PRIMARY KEY,
23
- name TEXT NOT NULL
24
- );
25
- );`)
26
-
27
- await db.query(sql`
28
- CREATE TABLE pages (
29
- id INTEGER PRIMARY KEY,
30
- title TEXT,
31
- body TEXT,
32
- category_id INTEGER,
33
- FOREIGN KEY (category_id) REFERENCES subcategories(id) ON DELETE CASCADE
34
- );
35
- );`)
36
- }
37
- })
38
- app.register(sqlGraphQL)
39
- teardown(app.close.bind(app))
40
-
41
- await rejects(app.inject({
42
- method: 'POST',
43
- url: '/graphql',
44
- body: {
45
- query: `
46
- mutation {
47
- saveCategory(input: { name: "pets" }) {
48
- id
49
- name
50
- }
51
- }
52
- `
53
- }
54
- }), 'No foreign table named "subcategories" was found')
55
- })
56
-
57
- test('should handle multi references', async ({ pass, teardown, same, equal }) => {
58
- const app = fastify()
59
- app.register(sqlMapper, {
60
- ...connInfo,
61
- async onDatabaseLoad (db, sql) {
62
- pass('onDatabaseLoad called')
63
-
64
- await clear(db, sql)
65
-
66
- if (isMysql) {
67
- await db.query(sql`
68
- CREATE TABLE authors (
69
- id SERIAL PRIMARY KEY,
70
- name VARCHAR(255)
71
- );
72
- CREATE TABLE books (
73
- id SERIAL PRIMARY KEY,
74
- title VARCHAR(42),
75
- author_id BIGINT UNSIGNED,
76
- another_author_id BIGINT UNSIGNED,
77
- FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE,
78
- FOREIGN KEY (another_author_id) REFERENCES authors(id) ON DELETE CASCADE
79
- );
80
- `)
81
- } else if (isSQLite) {
82
- await db.query(sql`
83
- CREATE TABLE authors (
84
- id INTEGER PRIMARY KEY,
85
- name VARCHAR(255)
86
- );
87
- `)
88
- await db.query(sql`
89
- CREATE TABLE books (
90
- id INTEGER PRIMARY KEY,
91
- title VARCHAR(42),
92
- author_id BIGINT UNSIGNED,
93
- another_author_id BIGINT UNSIGNED,
94
- FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE,
95
- FOREIGN KEY (another_author_id) REFERENCES authors(id) ON DELETE CASCADE
96
- );
97
- `)
98
- } else {
99
- await db.query(sql`
100
- CREATE TABLE authors (
101
- id SERIAL PRIMARY KEY,
102
- name VARCHAR(42)
103
- );
104
- CREATE TABLE books (
105
- id SERIAL PRIMARY KEY,
106
- title VARCHAR(42),
107
- author_id INTEGER REFERENCES authors(id),
108
- another_author_id INTEGER REFERENCES authors(id)
109
- );
110
- `)
111
- }
112
- }
113
- })
114
- app.register(sqlGraphQL)
115
- teardown(app.close.bind(app))
116
-
117
- await app.ready()
118
-
119
- const authors = [{
120
- id: 1,
121
- name: 'Mark'
122
- }]
123
-
124
- const books = [{
125
- id: 1,
126
- title: 'Harry',
127
- authorId: 1,
128
- anotherAuthorId: 1
129
- }]
130
-
131
- {
132
- const res = await app.inject({
133
- method: 'POST',
134
- url: '/graphql',
135
- body: {
136
- query: `
137
- mutation batch($inputs : [AuthorInput]!) {
138
- insertAuthors(inputs: $inputs) {
139
- id
140
- name
141
- }
142
- }
143
- `,
144
- variables: {
145
- inputs: authors
146
- }
147
- }
148
- })
149
- equal(res.statusCode, 200, 'authors status code')
150
- }
151
-
152
- {
153
- const res = await app.inject({
154
- method: 'POST',
155
- url: '/graphql',
156
- body: {
157
- query: `
158
- mutation batch($inputs : [BookInput]!) {
159
- insertBooks(inputs: $inputs) {
160
- id
161
- title
162
- }
163
- }
164
- `,
165
- variables: {
166
- inputs: books
167
- }
168
- }
169
- })
170
- equal(res.statusCode, 200, 'books status code')
171
- }
172
-
173
- {
174
- const res = await app.inject({
175
- method: 'POST',
176
- url: '/graphql',
177
- body: {
178
- query: `
179
- query {
180
- books {
181
- id
182
- author {
183
- id
184
- }
185
- anotherAuthor {
186
- id
187
- }
188
- }
189
- }
190
- `
191
- }
192
- })
193
- equal(res.statusCode, 200, 'query books')
194
- same(res.json(), {
195
- data: {
196
- books: [{
197
- id: 1,
198
- author: {
199
- id: 1
200
- },
201
- anotherAuthor: {
202
- id: 1
203
- }
204
- }]
205
- }
206
- }, 'query book response')
207
- }
208
-
209
- {
210
- const res = await app.inject({
211
- method: 'POST',
212
- url: '/graphql',
213
- body: {
214
- query: `
215
- query {
216
- authors {
217
- id
218
- books {
219
- id
220
- }
221
- }
222
- }
223
- `
224
- }
225
- })
226
- equal(res.statusCode, 200, 'query authors')
227
- same(res.json(), {
228
- data: {
229
- authors: [{
230
- id: 1,
231
- books: [{
232
- id: 1
233
- }]
234
- }]
235
- }
236
- }, 'query authors response')
237
- }
238
- })
239
-
240
- test('cut out id exactly from ending when forming a name of relation', async ({ pass, teardown, same, equal }) => {
241
- const app = fastify()
242
- app.register(sqlMapper, {
243
- ...connInfo,
244
- async onDatabaseLoad (db, sql) {
245
- pass('onDatabaseLoad called')
246
-
247
- await clear(db, sql)
248
-
249
- if (isMysql) {
250
- await db.query(sql`
251
- CREATE TABLE individuals (
252
- id SERIAL PRIMARY KEY,
253
- name VARCHAR(255)
254
- );
255
- CREATE TABLE organizations (
256
- id SERIAL PRIMARY KEY,
257
- name VARCHAR(42),
258
- individual_id BIGINT UNSIGNED,
259
- FOREIGN KEY (individual_id) REFERENCES individuals(id) ON DELETE CASCADE
260
- );
261
- `)
262
- } else if (isSQLite) {
263
- await db.query(sql`
264
- CREATE TABLE individuals (
265
- id INTEGER PRIMARY KEY,
266
- name VARCHAR(255)
267
- );
268
- `)
269
- await db.query(sql`
270
- CREATE TABLE organizations (
271
- id INTEGER PRIMARY KEY,
272
- name VARCHAR(42),
273
- individual_id BIGINT UNSIGNED,
274
- FOREIGN KEY (individual_id) REFERENCES individuals(id) ON DELETE CASCADE
275
- );
276
- `)
277
- } else {
278
- await db.query(sql`
279
- CREATE TABLE individuals (
280
- id SERIAL PRIMARY KEY,
281
- name VARCHAR(42)
282
- );
283
- CREATE TABLE organizations (
284
- id SERIAL PRIMARY KEY,
285
- name VARCHAR(42),
286
- individual_id INTEGER REFERENCES individuals(id)
287
- );
288
- `)
289
- }
290
- }
291
- })
292
- app.register(sqlGraphQL)
293
- teardown(app.close.bind(app))
294
-
295
- await app.ready()
296
-
297
- const individuals = [{
298
- id: 1,
299
- name: 'Mark'
300
- }]
301
-
302
- const organization = [{
303
- id: 1,
304
- name: 'Platformatic',
305
- individualId: 1
306
- }]
307
-
308
- {
309
- const res = await app.inject({
310
- method: 'POST',
311
- url: '/graphql',
312
- body: {
313
- query: `
314
- mutation batch($inputs : [IndividualInput]!) {
315
- insertIndividuals(inputs: $inputs) {
316
- id
317
- name
318
- }
319
- }
320
- `,
321
- variables: {
322
- inputs: individuals
323
- }
324
- }
325
- })
326
- equal(res.statusCode, 200, 'individuals status code')
327
- }
328
-
329
- {
330
- const res = await app.inject({
331
- method: 'POST',
332
- url: '/graphql',
333
- body: {
334
- query: `
335
- mutation batch($inputs : [OrganizationInput]!) {
336
- insertOrganizations(inputs: $inputs) {
337
- id
338
- name
339
- }
340
- }
341
- `,
342
- variables: {
343
- inputs: organization
344
- }
345
- }
346
- })
347
- equal(res.statusCode, 200, 'organization status code')
348
- }
349
-
350
- {
351
- const res = await app.inject({
352
- method: 'POST',
353
- url: '/graphql',
354
- body: {
355
- query: `
356
- query {
357
- organizations {
358
- id
359
- name
360
- individual {
361
- id
362
- name
363
- }
364
- }
365
- }
366
- `
367
- }
368
- })
369
- equal(res.statusCode, 200, 'query organization')
370
- same(res.json(), {
371
- data: {
372
- organizations: [{
373
- id: 1,
374
- name: 'Platformatic',
375
- individual: {
376
- id: 1,
377
- name: 'Mark'
378
- }
379
- }]
380
- }
381
- }, 'query organization response')
382
- }
383
- })
384
-
385
- test('should handle reads from save', { only: true }, async ({ pass, teardown, same, equal }) => {
386
- const app = fastify({ logger: false })
387
- app.register(sqlMapper, {
388
- ...connInfo,
389
- async onDatabaseLoad (db, sql) {
390
- pass('onDatabaseLoad called')
391
-
392
- await clear(db, sql)
393
-
394
- if (isMysql) {
395
- await db.query(sql`
396
- CREATE TABLE categories (
397
- id SERIAL PRIMARY KEY,
398
- name VARCHAR(255)
399
- );
400
- CREATE TABLE authors (
401
- id SERIAL PRIMARY KEY,
402
- name VARCHAR(255),
403
- category_id BIGINT UNSIGNED,
404
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
405
- );
406
- CREATE TABLE books (
407
- id SERIAL PRIMARY KEY,
408
- title VARCHAR(42),
409
- author_id BIGINT UNSIGNED,
410
- category_id BIGINT UNSIGNED,
411
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
412
- FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
413
- );
414
- `)
415
- } else if (isSQLite) {
416
- await db.query(sql`
417
- CREATE TABLE categories (
418
- id INTEGER PRIMARY KEY,
419
- name VARCHAR(255)
420
- );
421
- `)
422
- await db.query(sql`
423
- CREATE TABLE authors (
424
- id INTEGER PRIMARY KEY,
425
- name VARCHAR(255),
426
- category_id BIGINT UNSIGNED,
427
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
428
- );
429
- `)
430
- await db.query(sql`
431
- CREATE TABLE books (
432
- id INTEGER PRIMARY KEY,
433
- title VARCHAR(42),
434
- author_id BIGINT UNSIGNED,
435
- category_id BIGINT UNSIGNED,
436
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
437
- FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
438
- );
439
- `)
440
- } else {
441
- await db.query(sql`
442
- CREATE TABLE categories (
443
- id SERIAL PRIMARY KEY,
444
- name VARCHAR(42)
445
- );
446
-
447
- CREATE TABLE authors (
448
- id SERIAL PRIMARY KEY,
449
- name VARCHAR(42),
450
- category_id INTEGER REFERENCES categories(id)
451
- );
452
-
453
- CREATE TABLE books (
454
- id SERIAL PRIMARY KEY,
455
- title VARCHAR(42),
456
- category_id INTEGER REFERENCES categories(id),
457
- author_id INTEGER REFERENCES authors(id)
458
- );
459
- `)
460
- }
461
- }
462
- })
463
- app.register(sqlGraphQL)
464
- teardown(app.close.bind(app))
465
-
466
- await app.ready()
467
-
468
- const categories = [{
469
- name: 'Sci-Fi'
470
- }]
471
-
472
- const authors = [{
473
- name: 'Mark',
474
- categoryId: 1
475
- }]
476
-
477
- const books = [{
478
- title: 'Harry',
479
- authorId: 1,
480
- categoryId: 1
481
- }]
482
-
483
- {
484
- const res = await app.inject({
485
- method: 'POST',
486
- url: '/graphql',
487
- body: {
488
- query: `
489
- mutation batch($inputs : [CategoryInput]!) {
490
- insertCategories(inputs: $inputs) {
491
- id
492
- name
493
- }
494
- }
495
- `,
496
- variables: {
497
- inputs: categories
498
- }
499
- }
500
- })
501
- equal(res.statusCode, 200, 'categories status code')
502
- }
503
-
504
- {
505
- const res = await app.inject({
506
- method: 'POST',
507
- url: '/graphql',
508
- body: {
509
- query: `
510
- mutation batch($inputs : [AuthorInput]!) {
511
- insertAuthors(inputs: $inputs) {
512
- id
513
- name
514
- }
515
- }
516
- `,
517
- variables: {
518
- inputs: authors
519
- }
520
- }
521
- })
522
- equal(res.statusCode, 200, 'authors status code')
523
- }
524
-
525
- {
526
- const res = await app.inject({
527
- method: 'POST',
528
- url: '/graphql',
529
- body: {
530
- query: `
531
- mutation batch($inputs : [BookInput]!) {
532
- insertBooks(inputs: $inputs) {
533
- id
534
- title
535
- }
536
- }
537
- `,
538
- variables: {
539
- inputs: books
540
- }
541
- }
542
- })
543
- equal(res.statusCode, 200, 'books status code')
544
- }
545
-
546
- {
547
- const res = await app.inject({
548
- method: 'POST',
549
- url: '/graphql',
550
- body: {
551
- query: `
552
- mutation {
553
- saveAuthor(input: { id: 1, name: "Mark II" }) {
554
- id
555
- books {
556
- id
557
- title
558
- }
559
- }
560
- }
561
- `,
562
- variables: {
563
- inputs: books
564
- }
565
- }
566
- })
567
- equal(res.statusCode, 200, 'books status code')
568
- same(res.json(), {
569
- data: {
570
- saveAuthor: {
571
- id: 1,
572
- books: [{
573
- id: 1,
574
- title: 'Harry'
575
- }]
576
- }
577
- }
578
- })
579
- }
580
- })
581
-
582
- test('should handle nullable relation', async ({ pass, teardown, same, equal }) => {
583
- const app = fastify()
584
- app.register(sqlMapper, {
585
- ...connInfo,
586
- async onDatabaseLoad (db, sql) {
587
- pass('onDatabaseLoad called')
588
-
589
- await clear(db, sql)
590
-
591
- if (isMysql) {
592
- await db.query(sql`
593
- CREATE TABLE authors (
594
- id SERIAL PRIMARY KEY,
595
- name VARCHAR(255)
596
- );
597
- CREATE TABLE books (
598
- id SERIAL PRIMARY KEY,
599
- title VARCHAR(42),
600
- author_id BIGINT UNSIGNED,
601
- FOREIGN KEY (author_id) REFERENCES authors(id)
602
- );
603
- `)
604
- } else if (isSQLite) {
605
- await db.query(sql`
606
- CREATE TABLE authors (
607
- id INTEGER PRIMARY KEY,
608
- name VARCHAR(255)
609
- );
610
- `)
611
- await db.query(sql`
612
- CREATE TABLE books (
613
- id INTEGER PRIMARY KEY,
614
- title VARCHAR(42),
615
- author_id BIGINT UNSIGNED,
616
- FOREIGN KEY (author_id) REFERENCES authors(id)
617
- );
618
- `)
619
- } else {
620
- await db.query(sql`
621
- CREATE TABLE authors (
622
- id SERIAL PRIMARY KEY,
623
- name VARCHAR(42)
624
- );
625
- CREATE TABLE books (
626
- id SERIAL PRIMARY KEY,
627
- title VARCHAR(42),
628
- author_id INTEGER REFERENCES authors(id)
629
- );
630
- `)
631
- }
632
- }
633
- })
634
- app.register(sqlGraphQL)
635
- teardown(app.close.bind(app))
636
-
637
- await app.ready()
638
-
639
- const authors = [
640
- {
641
- id: 1,
642
- name: 'Mark 1'
643
- },
644
- {
645
- id: 2,
646
- name: 'Mark 2'
647
- }
648
- ]
649
-
650
- const books = [
651
- {
652
- id: 1,
653
- title: 'Harry 1',
654
- authorId: 1
655
- },
656
- {
657
- id: 2,
658
- title: 'Harry 2',
659
- authorId: null
660
- },
661
- {
662
- id: 3,
663
- title: 'Harry 3',
664
- authorId: 2
665
- }
666
- ]
667
-
668
- {
669
- const res = await app.inject({
670
- method: 'POST',
671
- url: '/graphql',
672
- body: {
673
- query: `
674
- mutation batch($inputs : [AuthorInput]!) {
675
- insertAuthors(inputs: $inputs) {
676
- id
677
- name
678
- }
679
- }
680
- `,
681
- variables: {
682
- inputs: authors
683
- }
684
- }
685
- })
686
- equal(res.statusCode, 200, 'authors status code')
687
- }
688
-
689
- {
690
- const res = await app.inject({
691
- method: 'POST',
692
- url: '/graphql',
693
- body: {
694
- query: `
695
- mutation batch($inputs : [BookInput]!) {
696
- insertBooks(inputs: $inputs) {
697
- id
698
- title
699
- }
700
- }
701
- `,
702
- variables: {
703
- inputs: books
704
- }
705
- }
706
- })
707
- equal(res.statusCode, 200, 'books status code')
708
- }
709
-
710
- {
711
- const res = await app.inject({
712
- method: 'POST',
713
- url: '/graphql',
714
- body: {
715
- query: `
716
- query {
717
- books (orderBy: { field: id, direction: ASC }) {
718
- id
719
- author {
720
- id
721
- }
722
- }
723
- }
724
- `
725
- }
726
- })
727
- equal(res.statusCode, 200, 'query books')
728
- same(res.json(), {
729
- data: {
730
- books: [
731
- {
732
- id: 1,
733
- author: {
734
- id: 1
735
- }
736
- },
737
- {
738
- id: 2,
739
- author: null
740
- },
741
- {
742
- id: 3,
743
- author: {
744
- id: 2
745
- }
746
- }
747
- ]
748
- }
749
- }, 'query book response')
750
- }
751
-
752
- {
753
- const res = await app.inject({
754
- method: 'POST',
755
- url: '/graphql',
756
- body: {
757
- query: `
758
- query {
759
- authors {
760
- id
761
- books {
762
- id
763
- }
764
- }
765
- }
766
- `
767
- }
768
- })
769
- equal(res.statusCode, 200, 'query authors')
770
- same(res.json(), {
771
- data: {
772
- authors: [
773
- {
774
- id: 1,
775
- books: [{ id: 1 }]
776
- },
777
- {
778
- id: 2,
779
- books: [{ id: 3 }]
780
- }
781
- ]
782
- }
783
- }, 'query authors response')
784
- }
785
- })