@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,533 +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, isSQLite, isMysql } = require('./helper')
8
- const { setTimeout } = require('timers/promises')
9
-
10
- async function createBasicPages (db, sql) {
11
- if (isSQLite) {
12
- await db.query(sql`CREATE TABLE pages (
13
- id INTEGER PRIMARY KEY,
14
- title VARCHAR(42),
15
- inserted_at TIMESTAMP,
16
- updated_at TIMESTAMP
17
- );`)
18
- } else if (isMysql) {
19
- await db.query(sql`CREATE TABLE pages (
20
- id SERIAL PRIMARY KEY,
21
- title VARCHAR(42),
22
- inserted_at TIMESTAMP NULL DEFAULT NULL,
23
- updated_at TIMESTAMP NULL DEFAULT NULL
24
- );`)
25
- } else {
26
- await db.query(sql`CREATE TABLE pages (
27
- id SERIAL PRIMARY KEY,
28
- title VARCHAR(42),
29
- inserted_at TIMESTAMP,
30
- updated_at TIMESTAMP
31
- );`)
32
- }
33
- }
34
-
35
- test('inserted_at updated_at happy path', async ({ pass, teardown, same, equal, not, comment }) => {
36
- const app = fastify()
37
- app.register(sqlMapper, {
38
- ...connInfo,
39
- autoTimestamp: {
40
- createdAt: 'inserted_at',
41
- updatedAt: 'updated_at'
42
- },
43
- async onDatabaseLoad (db, sql) {
44
- pass('onDatabaseLoad called')
45
-
46
- await clear(db, sql)
47
- await createBasicPages(db, sql)
48
- }
49
- })
50
- app.register(sqlGraphQL)
51
- teardown(app.close.bind(app))
52
-
53
- await app.ready()
54
-
55
- let original
56
-
57
- {
58
- const res = await app.inject({
59
- method: 'POST',
60
- url: '/graphql',
61
- body: {
62
- query: `
63
- mutation {
64
- savePage(input: { title: "Hello" }) {
65
- id
66
- title
67
- insertedAt
68
- updatedAt
69
- }
70
- }
71
- `
72
- }
73
- })
74
- equal(res.statusCode, 200, 'savePage status code')
75
- const data = res.json().data
76
- not(data.savePage.insertedAt, null, 'insertedAt')
77
- not(data.savePage.updatedAt, null, 'updatedAt')
78
- comment(`insertedAt: ${data.savePage.insertedAt}`)
79
- comment(`updatedAt: ${data.savePage.updatedAt}`)
80
- original = data.savePage
81
- }
82
-
83
- {
84
- const res = await app.inject({
85
- method: 'POST',
86
- url: '/graphql',
87
- body: {
88
- query: `
89
- query {
90
- getPageById(id: 1) {
91
- id
92
- title
93
- insertedAt
94
- updatedAt
95
- }
96
- }
97
- `
98
- }
99
- })
100
- equal(res.statusCode, 200, 'pages status code')
101
- const data = res.json().data
102
- equal(data.getPageById.insertedAt, original.insertedAt, 'insertedAt')
103
- equal(data.getPageById.updatedAt, original.updatedAt, 'updatedAt')
104
- comment(`insertedAt: ${data.getPageById.insertedAt}`)
105
- comment(`updatedAt: ${data.getPageById.updatedAt}`)
106
- }
107
-
108
- await setTimeout(1000) // await 1s
109
-
110
- let updated
111
- {
112
- const res = await app.inject({
113
- method: 'POST',
114
- url: '/graphql',
115
- body: {
116
- query: `
117
- mutation {
118
- savePage(input: { id: 1, title: "Hello World" }) {
119
- id
120
- title
121
- insertedAt
122
- updatedAt
123
- }
124
- }
125
- `
126
- }
127
- })
128
- const data = res.json().data
129
- equal(data.savePage.insertedAt, original.insertedAt, 'insertedAt')
130
- not(data.savePage.updatedAt, original.updatedAt, 'updatedAt')
131
- updated = data.savePage
132
- comment(`insertedAt: ${data.savePage.insertedAt}`)
133
- comment(`updatedAt: ${data.savePage.updatedAt}`)
134
- }
135
-
136
- {
137
- const res = await app.inject({
138
- method: 'POST',
139
- url: '/graphql',
140
- body: {
141
- query: `
142
- query {
143
- getPageById(id: 1) {
144
- id
145
- title
146
- insertedAt
147
- updatedAt
148
- }
149
- }
150
- `
151
- }
152
- })
153
- equal(res.statusCode, 200, 'pages status code')
154
- const data = res.json().data
155
- equal(data.getPageById.insertedAt, updated.insertedAt, 'insertedAt')
156
- equal(data.getPageById.updatedAt, updated.updatedAt, 'updatedAt')
157
- comment(`insertedAt: ${data.getPageById.insertedAt}`)
158
- comment(`updatedAt: ${data.getPageById.updatedAt}`)
159
- }
160
- })
161
-
162
- test('cannot set inserted_at', async ({ pass, teardown, same, equal, not, comment }) => {
163
- const app = fastify()
164
- app.register(sqlMapper, {
165
- ...connInfo,
166
- autoTimestamp: {
167
- createdAt: 'inserted_at',
168
- updatedAt: 'updated_at'
169
- },
170
- async onDatabaseLoad (db, sql) {
171
- pass('onDatabaseLoad called')
172
-
173
- await clear(db, sql)
174
- await createBasicPages(db, sql)
175
- }
176
- })
177
- app.register(sqlGraphQL)
178
- teardown(app.close.bind(app))
179
-
180
- await app.ready()
181
-
182
- {
183
- const res = await app.inject({
184
- method: 'POST',
185
- url: '/graphql',
186
- body: {
187
- query: `
188
- mutation {
189
- savePage(input: { title: "Hello", insertedAt: "${new Date().toISOString()}" }) {
190
- id
191
- title
192
- insertedAt
193
- updatedAt
194
- }
195
- }
196
- `
197
- }
198
- })
199
- equal(res.statusCode, 400, 'savePage status code')
200
- const data = res.json()
201
- equal(data.errors[0].message, 'Field "insertedAt" is not defined by type "PageInput".')
202
- }
203
- })
204
-
205
- test('cannot set updated_at', async ({ pass, teardown, same, equal, not, comment }) => {
206
- const app = fastify()
207
- app.register(sqlMapper, {
208
- ...connInfo,
209
- autoTimestamp: {
210
- createdAt: 'inserted_at',
211
- updatedAt: 'updated_at'
212
- },
213
- async onDatabaseLoad (db, sql) {
214
- pass('onDatabaseLoad called')
215
-
216
- await clear(db, sql)
217
- await createBasicPages(db, sql)
218
- }
219
- })
220
- app.register(sqlGraphQL)
221
- teardown(app.close.bind(app))
222
-
223
- await app.ready()
224
-
225
- {
226
- const res = await app.inject({
227
- method: 'POST',
228
- url: '/graphql',
229
- body: {
230
- query: `
231
- mutation {
232
- savePage(input: { title: "Hello" }) {
233
- id
234
- title
235
- insertedAt
236
- updatedAt
237
- }
238
- }
239
- `
240
- }
241
- })
242
- equal(res.statusCode, 200, 'savePage status code')
243
- const data = res.json().data
244
- not(data.savePage.insertedAt, null, 'insertedAt')
245
- not(data.savePage.updatedAt, null, 'updatedAt')
246
- comment(`insertedAt: ${data.savePage.insertedAt}`)
247
- comment(`updatedAt: ${data.savePage.updatedAt}`)
248
- }
249
-
250
- {
251
- const res = await app.inject({
252
- method: 'POST',
253
- url: '/graphql',
254
- body: {
255
- query: `
256
- mutation {
257
- savePage(input: { id: 1, title: "Hello World", updatedAt: "${new Date().toISOString()}" }) {
258
- id
259
- title
260
- insertedAt
261
- updatedAt
262
- }
263
- }
264
- `
265
- }
266
- })
267
- equal(res.statusCode, 400, 'savePage status code')
268
- const data = res.json()
269
- equal(data.errors[0].message, 'Field "updatedAt" is not defined by type "PageInput".')
270
- }
271
- })
272
-
273
- test('do not assign inserted_at updated_at', async ({ pass, teardown, same, equal, not, comment }) => {
274
- const app = fastify()
275
- app.register(sqlMapper, {
276
- ...connInfo,
277
- autoTimestamp: false,
278
- async onDatabaseLoad (db, sql) {
279
- pass('onDatabaseLoad called')
280
-
281
- await clear(db, sql)
282
- await createBasicPages(db, sql)
283
- }
284
- })
285
- app.register(sqlGraphQL)
286
- teardown(app.close.bind(app))
287
-
288
- await app.ready()
289
-
290
- {
291
- const res = await app.inject({
292
- method: 'POST',
293
- url: '/graphql',
294
- body: {
295
- query: `
296
- mutation {
297
- savePage(input: { title: "Hello" }) {
298
- id
299
- title
300
- insertedAt
301
- updatedAt
302
- }
303
- }
304
- `
305
- }
306
- })
307
- equal(res.statusCode, 200, 'savePage status code')
308
- const data = res.json().data
309
- equal(data.savePage.insertedAt, null, 'insertedAt')
310
- equal(data.savePage.updatedAt, null, 'updatedAt')
311
- }
312
-
313
- {
314
- const res = await app.inject({
315
- method: 'POST',
316
- url: '/graphql',
317
- body: {
318
- query: `
319
- query {
320
- getPageById(id: 1) {
321
- id
322
- title
323
- insertedAt
324
- updatedAt
325
- }
326
- }
327
- `
328
- }
329
- })
330
- equal(res.statusCode, 200, 'pages status code')
331
- const data = res.json().data
332
- equal(data.getPageById.insertedAt, null, 'insertedAt')
333
- equal(data.getPageById.updatedAt, null, 'updatedAt')
334
- }
335
-
336
- {
337
- const res = await app.inject({
338
- method: 'POST',
339
- url: '/graphql',
340
- body: {
341
- query: `
342
- mutation {
343
- savePage(input: { id: 1, title: "Hello World" }) {
344
- id
345
- title
346
- insertedAt
347
- updatedAt
348
- }
349
- }
350
- `
351
- }
352
- })
353
- const data = res.json().data
354
- equal(data.savePage.insertedAt, null, 'insertedAt')
355
- equal(data.savePage.updatedAt, null, 'updatedAt')
356
- }
357
-
358
- {
359
- const res = await app.inject({
360
- method: 'POST',
361
- url: '/graphql',
362
- body: {
363
- query: `
364
- query {
365
- getPageById(id: 1) {
366
- id
367
- title
368
- insertedAt
369
- updatedAt
370
- }
371
- }
372
- `
373
- }
374
- })
375
- equal(res.statusCode, 200, 'pages status code')
376
- const data = res.json().data
377
- equal(data.getPageById.insertedAt, null, 'insertedAt')
378
- equal(data.getPageById.updatedAt, null, 'updatedAt')
379
- }
380
- })
381
-
382
- test('bulk insert adds inserted_at updated_at', async ({ pass, teardown, same, equal, not, comment }) => {
383
- const app = fastify()
384
- app.register(sqlMapper, {
385
- ...connInfo,
386
- autoTimestamp: {
387
- createdAt: 'inserted_at',
388
- updatedAt: 'updated_at'
389
- },
390
- async onDatabaseLoad (db, sql) {
391
- pass('onDatabaseLoad called')
392
-
393
- await clear(db, sql)
394
- await createBasicPages(db, sql)
395
- }
396
- })
397
- app.register(sqlGraphQL)
398
- teardown(app.close.bind(app))
399
-
400
- await app.ready()
401
-
402
- {
403
- const res = await app.inject({
404
- method: 'POST',
405
- url: '/graphql',
406
- body: {
407
- query: `
408
- mutation {
409
- savePage(input: { title: "Hello" }) {
410
- id
411
- title
412
- insertedAt
413
- updatedAt
414
- }
415
- }
416
- `
417
- }
418
- })
419
- equal(res.statusCode, 200, 'savePage status code')
420
- const data = res.json().data
421
- not(data.savePage.insertedAt, null, 'insertedAt')
422
- not(data.savePage.updatedAt, null, 'updatedAt')
423
- }
424
-
425
- {
426
- const res = await app.inject({
427
- method: 'POST',
428
- url: '/graphql',
429
- body: {
430
- query: `
431
- mutation batch($inputs : [PageInput]!) {
432
- insertPages (inputs: $inputs) {
433
- id
434
- title
435
- insertedAt
436
- updatedAt
437
- }
438
- }
439
- `,
440
- variables: {
441
- inputs: [
442
- { title: 'Page 1' },
443
- { title: 'Page 2' },
444
- { title: 'Page 3' }
445
- ]
446
- }
447
- }
448
- })
449
- equal(res.statusCode, 200, 'savePage status code')
450
- const pages = res.json().data.insertPages
451
- for (const page of pages) {
452
- not(page.insertedAt, null, 'insertedAt')
453
- not(page.updatedAt, null, 'updatedAt')
454
- equal(page.insertedAt, page.updatedAt, 'insertedAt === updatedAt')
455
- }
456
- }
457
-
458
- {
459
- const res = await app.inject({
460
- method: 'POST',
461
- url: '/graphql',
462
- body: {
463
- query: `
464
- query {
465
- pages {
466
- id
467
- title
468
- insertedAt
469
- updatedAt
470
- }
471
- }
472
- `
473
- }
474
- })
475
- equal(res.statusCode, 200, 'pages status code')
476
- const pages = res.json().data.pages
477
- for (const page of pages) {
478
- not(page.insertedAt, null, 'insertedAt')
479
- not(page.updatedAt, null, 'updatedAt')
480
- equal(page.insertedAt, page.updatedAt, 'insertedAt === updatedAt')
481
- }
482
- }
483
- })
484
-
485
- test('bulk insert with autoTimestamp=false do not had inserted_at updated_at', async ({ pass, teardown, same, equal, not, comment }) => {
486
- const app = fastify()
487
- app.register(sqlMapper, {
488
- ...connInfo,
489
- autoTimestamp: false,
490
- async onDatabaseLoad (db, sql) {
491
- pass('onDatabaseLoad called')
492
-
493
- await clear(db, sql)
494
- await createBasicPages(db, sql)
495
- }
496
- })
497
- app.register(sqlGraphQL)
498
- teardown(app.close.bind(app))
499
-
500
- await app.ready()
501
-
502
- {
503
- const res = await app.inject({
504
- method: 'POST',
505
- url: '/graphql',
506
- body: {
507
- query: `
508
- mutation batch($inputs : [PageInput]!) {
509
- insertPages (inputs: $inputs) {
510
- id
511
- title
512
- insertedAt
513
- updatedAt
514
- }
515
- }
516
- `,
517
- variables: {
518
- inputs: [
519
- { title: 'Page 1' },
520
- { title: 'Page 2' },
521
- { title: 'Page 3' }
522
- ]
523
- }
524
- }
525
- })
526
- equal(res.statusCode, 200, 'savePage status code')
527
- const pages = res.json().data.insertPages
528
- for (const page of pages) {
529
- equal(page.insertedAt, null, 'insertedAt')
530
- equal(page.updatedAt, null, 'updatedAt')
531
- }
532
- }
533
- })