@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,803 +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, isPg, isMysql, isSQLite } = require('./helper')
8
-
9
- test('[PG] simple db simple graphql schema', { skip: !isPg }, 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
- await db.query(sql`
19
- CREATE TYPE simple_enum as ENUM ('value1', 'value2');
20
- CREATE TABLE simple_types (
21
- id SERIAL8 PRIMARY KEY,
22
- published BOOL,
23
- current DOUBLE PRECISION,
24
- long_text TEXT,
25
- born_at_date DATE,
26
- born_at_time TIME,
27
- born_at_timestamp TIMESTAMP,
28
- uuid UUID UNIQUE,
29
- a_real real,
30
- a_smallint smallint,
31
- a_decimal decimal,
32
- an_enum simple_enum
33
- );`)
34
- }
35
- })
36
- teardown(app.close.bind(app))
37
-
38
- app.register(sqlGraphQL)
39
-
40
- await app.ready()
41
-
42
- const timestamp = new Date()
43
-
44
- {
45
- const res = await app.inject({
46
- method: 'POST',
47
- url: '/graphql',
48
- body: {
49
- query: `
50
- mutation {
51
- saveSimpleType(input: {
52
- published: true,
53
- current: 42,
54
- longText: "abc",
55
- bornAtDate: "2021-11-11",
56
- bornAtTime: "12:42:00Z",
57
- bornAtTimestamp: "${timestamp.toISOString()}",
58
- uuid: "12345678-1234-1234-1234-123456789012",
59
- aReal: 1.2,
60
- aSmallint: 42,
61
- aDecimal: "42",
62
- anEnum: value1
63
- }) {
64
- id
65
- published
66
- current
67
- longText
68
- bornAtDate
69
- bornAtTime
70
- bornAtTimestamp
71
- uuid
72
- aReal
73
- aSmallint
74
- aDecimal
75
- anEnum
76
- }
77
- }
78
- `
79
- }
80
- })
81
- equal(res.statusCode, 200, 'saveSimpleType status code')
82
- same(res.json(), {
83
- data: {
84
- saveSimpleType: {
85
- id: '1',
86
- published: true,
87
- current: 42,
88
- longText: 'abc',
89
- bornAtDate: '2021-11-11',
90
- bornAtTime: '12:42:00',
91
- bornAtTimestamp: timestamp.toISOString(),
92
- uuid: '12345678-1234-1234-1234-123456789012',
93
- aReal: 1.2,
94
- aSmallint: 42,
95
- aDecimal: '42',
96
- anEnum: 'value1'
97
- }
98
- }
99
- }, 'saveSimpleType response')
100
- }
101
-
102
- {
103
- const res = await app.inject({
104
- method: 'POST',
105
- url: '/graphql',
106
- body: {
107
- query: `
108
- query {
109
- getSimpleTypeById(id: 1) {
110
- id
111
- published
112
- current
113
- longText
114
- bornAtDate
115
- bornAtTime
116
- bornAtTimestamp
117
- uuid
118
- aReal
119
- aSmallint
120
- aDecimal
121
- anEnum
122
- }
123
- }
124
- `
125
- }
126
- })
127
- equal(res.statusCode, 200, 'getSimpleTypeById status code')
128
- same(res.json(), {
129
- data: {
130
- getSimpleTypeById: {
131
- id: 1,
132
- published: true,
133
- current: 42,
134
- longText: 'abc',
135
- bornAtDate: '2021-11-11',
136
- bornAtTime: '12:42:00',
137
- bornAtTimestamp: timestamp.toISOString(),
138
- uuid: '12345678-1234-1234-1234-123456789012',
139
- aReal: 1.2,
140
- aSmallint: 42,
141
- aDecimal: 42,
142
- anEnum: 'value1'
143
- }
144
- }
145
- }, 'getSimpleTypeById response')
146
- }
147
- })
148
-
149
- test('[PG] - UUID', { skip: !isPg }, async ({ pass, teardown, same, equal }) => {
150
- const app = fastify()
151
- app.register(sqlMapper, {
152
- ...connInfo,
153
- async onDatabaseLoad (db, sql) {
154
- pass('onDatabaseLoad called')
155
-
156
- await clear(db, sql)
157
-
158
- await db.query(sql`
159
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
160
- CREATE TABLE pages (
161
- id uuid PRIMARY KEY default uuid_generate_v1(),
162
- title VARCHAR(42)
163
- );`)
164
- }
165
- })
166
- app.register(sqlGraphQL)
167
- teardown(app.close.bind(app))
168
-
169
- await app.ready()
170
-
171
- let id
172
- {
173
- const res = await app.inject({
174
- method: 'POST',
175
- url: '/graphql',
176
- body: {
177
- query: `
178
- mutation {
179
- savePage(input: { title: "Hello" }) {
180
- id
181
- title
182
- }
183
- }
184
- `
185
- }
186
- })
187
- equal(res.statusCode, 200, 'savePage status code')
188
- id = res.json().data.savePage.id
189
- same(res.json(), {
190
- data: {
191
- savePage: {
192
- id,
193
- title: 'Hello'
194
- }
195
- }
196
- }, 'savePage response')
197
- }
198
-
199
- {
200
- const res = await app.inject({
201
- method: 'POST',
202
- url: '/graphql',
203
- body: {
204
- query: `
205
- query {
206
- getPageById(id: "${id}") {
207
- id
208
- title
209
- }
210
- }
211
- `
212
- }
213
- })
214
- equal(res.statusCode, 200, 'pages status code')
215
- same(res.json(), {
216
- data: {
217
- getPageById: {
218
- id,
219
- title: 'Hello'
220
- }
221
- }
222
- }, 'pages response')
223
- }
224
-
225
- {
226
- const res = await app.inject({
227
- method: 'POST',
228
- url: '/graphql',
229
- body: {
230
- query: `
231
- mutation {
232
- savePage(input: { id: "${id}", title: "Hello World" }) {
233
- id
234
- title
235
- }
236
- }
237
- `
238
- }
239
- })
240
- equal(res.statusCode, 200, 'savePage status code')
241
- same(res.json(), {
242
- data: {
243
- savePage: {
244
- id,
245
- title: 'Hello World'
246
- }
247
- }
248
- }, 'savePage response')
249
- }
250
- })
251
-
252
- test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pass, teardown, same, equal }) => {
253
- const app = fastify()
254
- app.register(sqlMapper, {
255
- ...connInfo,
256
- async onDatabaseLoad (db, sql) {
257
- pass('onDatabaseLoad called')
258
-
259
- await clear(db, sql)
260
-
261
- const uuidType = db.isMariaDB ? sql.__dangerous__rawValue('uuid') : sql.__dangerous__rawValue('varchar(200)')
262
-
263
- await db.query(sql`CREATE TABLE simple_types (
264
- id SERIAL PRIMARY KEY,
265
- published BOOL,
266
- current DOUBLE PRECISION,
267
- long_text TEXT,
268
- born_at_date DATE,
269
- born_at_time TIME,
270
- born_at_timestamp TIMESTAMP,
271
- uuid ${uuidType} UNIQUE,
272
- a_real real,
273
- a_smallint smallint,
274
- a_decimal decimal,
275
- an_enum enum ('value1', 'value2')
276
- );`)
277
- }
278
- })
279
- app.register(sqlGraphQL)
280
- teardown(app.close.bind(app))
281
-
282
- await app.ready()
283
-
284
- const isMariaDB = app.platformatic.db.isMariaDB
285
-
286
- const timestamp = new Date()
287
- const bornAtTimestamp = new Date(Math[isMariaDB ? 'floor' : 'round'](timestamp.getTime() / 1000) * 1000).toISOString()
288
-
289
- {
290
- const res = await app.inject({
291
- method: 'POST',
292
- url: '/graphql',
293
- body: {
294
- query: `
295
- mutation {
296
- saveSimpleType(input: {
297
- published: true,
298
- current: 42,
299
- longText: "abc",
300
- bornAtDate: "2021-11-11",
301
- bornAtTime: "12:42:00",
302
- bornAtTimestamp: "${timestamp.toISOString()}",
303
- uuid: "12345678-1234-1234-1234-123456789012",
304
- aReal: 1.2,
305
- aSmallint: 42,
306
- aDecimal: "42",
307
- anEnum: value1
308
- }) {
309
- id
310
- published
311
- current
312
- longText
313
- bornAtDate
314
- bornAtTime
315
- bornAtTimestamp
316
- uuid
317
- aReal
318
- aSmallint
319
- aDecimal
320
- anEnum
321
- }
322
- }
323
- `
324
- }
325
- })
326
- equal(res.statusCode, 200, 'saveSimpleType status code')
327
- same(res.json(), {
328
- data: {
329
- saveSimpleType: {
330
- id: '1',
331
- published: true,
332
- current: 42,
333
- longText: 'abc',
334
- bornAtDate: '2021-11-11',
335
- bornAtTime: '12:42:00',
336
- bornAtTimestamp,
337
- uuid: '12345678-1234-1234-1234-123456789012',
338
- aReal: 1.2,
339
- aSmallint: 42,
340
- aDecimal: '42',
341
- anEnum: 'value1'
342
- }
343
- }
344
- }, 'saveSimpleType response')
345
- }
346
-
347
- {
348
- const res = await app.inject({
349
- method: 'POST',
350
- url: '/graphql',
351
- body: {
352
- query: `
353
- query {
354
- getSimpleTypeById(id: 1) {
355
- id
356
- published
357
- current
358
- longText
359
- bornAtDate
360
- bornAtTime
361
- bornAtTimestamp
362
- uuid
363
- aReal
364
- aSmallint
365
- aDecimal
366
- anEnum
367
- }
368
- }
369
- `
370
- }
371
- })
372
- equal(res.statusCode, 200, 'getSimpleTypeById status code')
373
- same(res.json(), {
374
- data: {
375
- getSimpleTypeById: {
376
- id: 1,
377
- published: true,
378
- current: 42,
379
- longText: 'abc',
380
- bornAtDate: '2021-11-11',
381
- bornAtTime: '12:42:00',
382
- bornAtTimestamp,
383
- uuid: '12345678-1234-1234-1234-123456789012',
384
- aReal: 1.2,
385
- aSmallint: 42,
386
- aDecimal: '42',
387
- anEnum: 'value1'
388
- }
389
- }
390
- }, 'getSimpleTypeById response')
391
- }
392
- })
393
-
394
- test('[MySQL] - UUID', { skip: !isMysql }, async ({ pass, teardown, same, equal, skip }) => {
395
- const app = fastify()
396
- app.register(sqlMapper, {
397
- ...connInfo,
398
- async onDatabaseLoad (db, sql) {
399
- pass('onDatabaseLoad called')
400
- if (!db.isMariaDB) {
401
- return
402
- }
403
-
404
- await clear(db, sql)
405
-
406
- await db.query(sql`
407
- CREATE TABLE pages (
408
- id uuid PRIMARY KEY default UUID(),
409
- title VARCHAR(42)
410
- );`)
411
- }
412
- })
413
- app.register(sqlGraphQL)
414
- teardown(app.close.bind(app))
415
-
416
- await app.ready()
417
-
418
- if (!app.platformatic.db.isMariaDB) {
419
- skip('MySQL does not support UUID, only MariaDB does')
420
- return
421
- }
422
-
423
- let id
424
- {
425
- const res = await app.inject({
426
- method: 'POST',
427
- url: '/graphql',
428
- body: {
429
- query: `
430
- mutation {
431
- savePage(input: { title: "Hello" }) {
432
- id
433
- title
434
- }
435
- }
436
- `
437
- }
438
- })
439
- equal(res.statusCode, 200, 'savePage status code')
440
- id = res.json().data.savePage.id
441
- same(res.json(), {
442
- data: {
443
- savePage: {
444
- id,
445
- title: 'Hello'
446
- }
447
- }
448
- }, 'savePage response')
449
- }
450
-
451
- {
452
- const res = await app.inject({
453
- method: 'POST',
454
- url: '/graphql',
455
- body: {
456
- query: `
457
- query {
458
- getPageById(id: "${id}") {
459
- id
460
- title
461
- }
462
- }
463
- `
464
- }
465
- })
466
- equal(res.statusCode, 200, 'pages status code')
467
- same(res.json(), {
468
- data: {
469
- getPageById: {
470
- id,
471
- title: 'Hello'
472
- }
473
- }
474
- }, 'pages response')
475
- }
476
-
477
- {
478
- const res = await app.inject({
479
- method: 'POST',
480
- url: '/graphql',
481
- body: {
482
- query: `
483
- mutation {
484
- savePage(input: { id: "${id}", title: "Hello World" }) {
485
- id
486
- title
487
- }
488
- }
489
- `
490
- }
491
- })
492
- equal(res.statusCode, 200, 'savePage status code')
493
- same(res.json(), {
494
- data: {
495
- savePage: {
496
- id,
497
- title: 'Hello World'
498
- }
499
- }
500
- }, 'savePage response')
501
- }
502
- })
503
-
504
- test('[SQLite] simple db simple graphql schema', { skip: !isSQLite }, async ({ pass, teardown, same, equal }) => {
505
- const app = fastify()
506
- app.register(sqlMapper, {
507
- ...connInfo,
508
- async onDatabaseLoad (db, sql) {
509
- pass('onDatabaseLoad called')
510
-
511
- await clear(db, sql)
512
-
513
- await db.query(sql`CREATE TABLE simple_types (
514
- id INTEGER PRIMARY KEY,
515
- published BOOL,
516
- current DOUBLE PRECISION,
517
- long_text TEXT,
518
- born_at_timestamp TIMESTAMP,
519
- uuid UUID UNIQUE,
520
- a_real real,
521
- a_smallint smallint,
522
- a_decimal decimal
523
- );`)
524
- }
525
- })
526
- teardown(app.close.bind(app))
527
-
528
- app.register(sqlGraphQL)
529
-
530
- await app.ready()
531
-
532
- const timestamp = new Date()
533
-
534
- {
535
- const res = await app.inject({
536
- method: 'POST',
537
- url: '/graphql',
538
- body: {
539
- query: `
540
- mutation {
541
- saveSimpleType(input: {
542
- published: true,
543
- current: 42,
544
- longText: "abc",
545
- bornAtTimestamp: "${timestamp.toISOString()}",
546
- uuid: "12345678-1234-1234-1234-123456789012",
547
- aReal: 1.2,
548
- aSmallint: 42,
549
- aDecimal: "42" }) {
550
- id
551
- published
552
- current
553
- longText
554
- bornAtTimestamp
555
- uuid
556
- aReal
557
- aSmallint
558
- aDecimal
559
- }
560
- }
561
- `
562
- }
563
- })
564
- equal(res.statusCode, 200, 'saveSimpleType status code')
565
- same(res.json(), {
566
- data: {
567
- saveSimpleType: {
568
- id: '1',
569
- published: true,
570
- current: 42,
571
- longText: 'abc',
572
- bornAtTimestamp: timestamp.toISOString(),
573
- uuid: '12345678-1234-1234-1234-123456789012',
574
- aReal: 1.2,
575
- aSmallint: 42,
576
- aDecimal: '42'
577
- }
578
- }
579
- }, 'saveSimpleType response')
580
- }
581
-
582
- {
583
- const res = await app.inject({
584
- method: 'POST',
585
- url: '/graphql',
586
- body: {
587
- query: `
588
- query {
589
- getSimpleTypeById(id: 1) {
590
- id
591
- published
592
- current
593
- longText
594
- bornAtTimestamp
595
- uuid
596
- aReal
597
- aSmallint
598
- aDecimal
599
- }
600
- }
601
- `
602
- }
603
- })
604
- equal(res.statusCode, 200, 'getSimpleTypeById status code')
605
- same(res.json(), {
606
- data: {
607
- getSimpleTypeById: {
608
- id: 1,
609
- published: true,
610
- current: 42,
611
- longText: 'abc',
612
- bornAtTimestamp: timestamp.toISOString(),
613
- uuid: '12345678-1234-1234-1234-123456789012',
614
- aReal: 1.2,
615
- aSmallint: 42,
616
- aDecimal: '42'
617
- }
618
- }
619
- }, 'getSimpleTypeById response')
620
- }
621
- })
622
-
623
- test('[SQLite] - UUID', { skip: !isSQLite }, async ({ pass, teardown, same, equal }) => {
624
- const app = fastify()
625
- app.register(sqlMapper, {
626
- ...connInfo,
627
- async onDatabaseLoad (db, sql) {
628
- pass('onDatabaseLoad called')
629
-
630
- await clear(db, sql)
631
-
632
- await db.query(sql`
633
- CREATE TABLE pages (
634
- id uuid PRIMARY KEY,
635
- title VARCHAR(42)
636
- );`)
637
- }
638
- })
639
- app.register(sqlGraphQL)
640
-
641
- teardown(app.close.bind(app))
642
-
643
- await app.ready()
644
-
645
- let id
646
- {
647
- const res = await app.inject({
648
- method: 'POST',
649
- url: '/graphql',
650
- body: {
651
- query: `
652
- mutation {
653
- savePage(input: { title: "Hello" }) {
654
- id
655
- title
656
- }
657
- }
658
- `
659
- }
660
- })
661
- equal(res.statusCode, 200, 'savePage status code')
662
- id = res.json().data.savePage.id
663
- same(res.json(), {
664
- data: {
665
- savePage: {
666
- id,
667
- title: 'Hello'
668
- }
669
- }
670
- }, 'savePage response')
671
- }
672
-
673
- {
674
- const res = await app.inject({
675
- method: 'POST',
676
- url: '/graphql',
677
- body: {
678
- query: `
679
- query {
680
- getPageById(id: "${id}") {
681
- id
682
- title
683
- }
684
- }
685
- `
686
- }
687
- })
688
- equal(res.statusCode, 200, 'pages status code')
689
- same(res.json(), {
690
- data: {
691
- getPageById: {
692
- id,
693
- title: 'Hello'
694
- }
695
- }
696
- }, 'pages response')
697
- }
698
-
699
- {
700
- const res = await app.inject({
701
- method: 'POST',
702
- url: '/graphql',
703
- body: {
704
- query: `
705
- mutation {
706
- savePage(input: { id: "${id}", title: "Hello World" }) {
707
- id
708
- title
709
- }
710
- }
711
- `
712
- }
713
- })
714
- equal(res.statusCode, 200, 'savePage status code')
715
- same(res.json(), {
716
- data: {
717
- savePage: {
718
- id,
719
- title: 'Hello World'
720
- }
721
- }
722
- }, 'savePage response')
723
- }
724
- })
725
-
726
- test('BIGINT!', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
727
- const app = fastify()
728
- app.register(sqlMapper, {
729
- ...connInfo,
730
- async onDatabaseLoad (db, sql) {
731
- pass('onDatabaseLoad called')
732
-
733
- await clear(db, sql)
734
-
735
- await db.query(sql`
736
- CREATE TABLE simple_types (
737
- id SERIAL PRIMARY KEY,
738
- counter BIGINT
739
- );`)
740
- }
741
- })
742
- teardown(app.close.bind(app))
743
-
744
- app.register(sqlGraphQL)
745
-
746
- await app.ready()
747
-
748
- const counter = BigInt(Number.MAX_SAFE_INTEGER) + 1000n
749
-
750
- {
751
- const res = await app.inject({
752
- method: 'POST',
753
- url: '/graphql',
754
- body: {
755
- query: `
756
- mutation {
757
- saveSimpleType(input: {
758
- counter: "${counter}"
759
- }) {
760
- id
761
- counter
762
- }
763
- }
764
- `
765
- }
766
- })
767
- equal(res.statusCode, 200, 'saveSimpleType status code')
768
- same(res.json(), {
769
- data: {
770
- saveSimpleType: {
771
- id: '1',
772
- counter
773
- }
774
- }
775
- }, 'saveSimpleType response')
776
- }
777
-
778
- {
779
- const res = await app.inject({
780
- method: 'POST',
781
- url: '/graphql',
782
- body: {
783
- query: `
784
- query {
785
- getSimpleTypeById(id: 1) {
786
- id
787
- counter
788
- }
789
- }
790
- `
791
- }
792
- })
793
- equal(res.statusCode, 200, 'getSimpleTypeById status code')
794
- same(res.json(), {
795
- data: {
796
- getSimpleTypeById: {
797
- id: 1,
798
- counter
799
- }
800
- }
801
- }, 'getSimpleTypeById response')
802
- }
803
- })