@platformatic/sql-graphql 0.46.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,660 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const sqlMapper = require('@platformatic/sql-mapper')
5
- const sqlGraphQL = require('..')
6
- const fastify = require('fastify')
7
- const { clear, connInfo, isMysql, isSQLite } = require('./helper')
8
-
9
- test('basic hooks', async ({ pass, teardown, same, equal, plan }) => {
10
- plan(22)
11
- const app = fastify()
12
- app.register(sqlMapper, {
13
- ...connInfo,
14
- async onDatabaseLoad (db, sql) {
15
- pass('onDatabaseLoad called')
16
-
17
- await clear(db, sql)
18
-
19
- if (isSQLite) {
20
- await db.query(sql`CREATE TABLE pages (
21
- id INTEGER PRIMARY KEY,
22
- title VARCHAR(42)
23
- );`)
24
- } else {
25
- await db.query(sql`CREATE TABLE pages (
26
- id SERIAL PRIMARY KEY,
27
- title VARCHAR(42)
28
- );`)
29
- }
30
- },
31
- hooks: {
32
- Page: {
33
- noKey () {
34
- // This should never be called
35
- },
36
- async save (original, { input, ctx, fields }) {
37
- pass('save called')
38
-
39
- equal(ctx.app, app)
40
- if (!input.id) {
41
- same(input, {
42
- title: 'Hello'
43
- })
44
-
45
- return original({
46
- input: {
47
- title: 'Hello from hook'
48
- },
49
- fields
50
- })
51
- } else {
52
- same(input, {
53
- id: 1,
54
- title: 'Hello World'
55
- })
56
-
57
- return original({
58
- input: {
59
- id: 1,
60
- title: 'Hello from hook 2'
61
- },
62
- fields
63
- })
64
- }
65
- },
66
- async find (original, args) {
67
- pass('find called')
68
-
69
- equal(args.ctx.app, app)
70
- same(args.where, {
71
- id: {
72
- in: ['1']
73
- }
74
- })
75
- args.where = {
76
- id: {
77
- eq: ['2']
78
- }
79
- }
80
- same(args.fields, ['id', 'title'])
81
- return original(args)
82
- },
83
- async insert (original, args) {
84
- pass('insert called')
85
-
86
- equal(args.ctx.app, app)
87
- same(args.inputs, [{
88
- title: 'Hello'
89
- }, {
90
- title: 'Hello World'
91
- }])
92
- same(args.fields, ['id', 'title'])
93
- return original(args)
94
- }
95
- }
96
- }
97
- })
98
- app.register(sqlGraphQL)
99
- teardown(app.close.bind(app))
100
-
101
- await app.ready()
102
-
103
- {
104
- const res = await app.inject({
105
- method: 'POST',
106
- url: '/graphql',
107
- body: {
108
- query: `
109
- mutation {
110
- savePage(input: { title: "Hello" }) {
111
- id
112
- title
113
- }
114
- }
115
- `
116
- }
117
- })
118
- equal(res.statusCode, 200, 'savePage status code')
119
- same(res.json(), {
120
- data: {
121
- savePage: {
122
- id: 1,
123
- title: 'Hello from hook'
124
- }
125
- }
126
- }, 'savePage response')
127
- }
128
-
129
- {
130
- const res = await app.inject({
131
- method: 'POST',
132
- url: '/graphql',
133
- body: {
134
- query: `
135
- query {
136
- getPageById(id: 1) {
137
- id
138
- title
139
- }
140
- }
141
- `
142
- }
143
- })
144
- equal(res.statusCode, 200, 'pages status code')
145
- same(res.json(), {
146
- data: {
147
- getPageById: null
148
- }
149
- }, 'pages response')
150
- }
151
-
152
- {
153
- const res = await app.inject({
154
- method: 'POST',
155
- url: '/graphql',
156
- body: {
157
- query: `
158
- mutation {
159
- savePage(input: { id: 1, title: "Hello World" }) {
160
- id
161
- title
162
- }
163
- }
164
- `
165
- }
166
- })
167
- equal(res.statusCode, 200, 'savePage status code')
168
- same(res.json(), {
169
- data: {
170
- savePage: {
171
- id: 1,
172
- title: 'Hello from hook 2'
173
- }
174
- }
175
- }, 'savePage response')
176
- }
177
-
178
- {
179
- const res = await app.inject({
180
- method: 'POST',
181
- url: '/graphql',
182
- body: {
183
- query: `
184
- mutation batch($inputs: [PageInput]!) {
185
- insertPages(inputs: $inputs) {
186
- id
187
- title
188
- }
189
- }
190
- `,
191
- variables: {
192
- inputs: [
193
- {
194
- title: 'Hello'
195
- },
196
- {
197
- title: 'Hello World'
198
- }
199
- ]
200
- }
201
- }
202
- })
203
-
204
- equal(res.statusCode, 200, 'insertPages status code')
205
- }
206
- })
207
-
208
- test('hooks with relationships', async ({ pass, teardown, same, equal, plan }) => {
209
- plan(17)
210
- const app = fastify()
211
- app.register(sqlMapper, {
212
- ...connInfo,
213
- async onDatabaseLoad (db, sql) {
214
- pass('onDatabaseLoad called')
215
-
216
- await clear(db, sql)
217
-
218
- if (isMysql) {
219
- await db.query(sql`
220
- CREATE TABLE categories (
221
- id SERIAL PRIMARY KEY,
222
- name VARCHAR(255)
223
- );
224
- CREATE TABLE pages (
225
- id SERIAL PRIMARY KEY,
226
- title VARCHAR(42),
227
- category_id BIGINT UNSIGNED,
228
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
229
- );
230
- `)
231
- } else if (isSQLite) {
232
- await db.query(sql`
233
- CREATE TABLE categories (
234
- id INTEGER PRIMARY KEY,
235
- name VARCHAR(255)
236
- );
237
- `)
238
- await db.query(sql`
239
- CREATE TABLE pages (
240
- id INTEGER PRIMARY KEY,
241
- title VARCHAR(42),
242
- category_id BIGINT UNSIGNED,
243
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
244
- );
245
- `)
246
- } else {
247
- await db.query(sql`
248
- CREATE TABLE categories (
249
- id SERIAL PRIMARY KEY,
250
- name VARCHAR(42)
251
- );
252
- CREATE TABLE pages (
253
- id SERIAL PRIMARY KEY,
254
- title VARCHAR(42),
255
- category_id INTEGER REFERENCES categories(id)
256
- );
257
- `)
258
- }
259
- },
260
- hooks: {
261
- Page: {
262
- async save (original, { input, ctx, fields }) {
263
- pass('save called')
264
- equal(ctx.app, app)
265
-
266
- return original({
267
- input,
268
- fields
269
- })
270
- },
271
- async find (original, opts) {
272
- pass('find called')
273
- equal(opts.ctx.app, app)
274
- return original(opts)
275
- }
276
- }
277
- }
278
- })
279
- app.register(sqlGraphQL)
280
- teardown(app.close.bind(app))
281
-
282
- const categories = [{
283
- name: 'Pets'
284
- }, {
285
- name: 'Food'
286
- }]
287
-
288
- await app.inject({
289
- method: 'POST',
290
- url: '/graphql',
291
- body: {
292
- query: `
293
- mutation batch($inputs: [CategoryInput]!) {
294
- insertCategories(inputs: $inputs) {
295
- id
296
- name
297
- }
298
- }
299
- `,
300
- variables: {
301
- inputs: categories
302
- }
303
- }
304
- })
305
-
306
- {
307
- const res = await app.inject({
308
- method: 'POST',
309
- url: '/graphql',
310
- body: {
311
- query: `
312
- mutation {
313
- savePage(input: { title: "Hello", categoryId: 1 }) {
314
- id
315
- title
316
- category {
317
- id
318
- name
319
- }
320
- }
321
- }
322
- `
323
- }
324
- })
325
- equal(res.statusCode, 200, 'savePage status code')
326
- same(res.json(), {
327
- data: {
328
- savePage: {
329
- id: 1,
330
- title: 'Hello',
331
- category: {
332
- id: 1,
333
- name: 'Pets'
334
- }
335
- }
336
- }
337
- }, 'savePage response')
338
- }
339
-
340
- {
341
- const res = await app.inject({
342
- method: 'POST',
343
- url: '/graphql',
344
- body: {
345
- query: `
346
- query {
347
- getPageById(id: 1) {
348
- id
349
- title
350
- category {
351
- id
352
- name
353
- pages {
354
- id
355
- title
356
- category {
357
- id
358
- name
359
- }
360
- }
361
- }
362
- }
363
- }
364
- `
365
- }
366
- })
367
- equal(res.statusCode, 200, 'pages status code')
368
- same(res.json(), {
369
- data: {
370
- getPageById: {
371
- id: 1,
372
- title: 'Hello',
373
- category: {
374
- id: 1,
375
- name: 'Pets',
376
- pages: [{
377
- id: 1,
378
- title: 'Hello',
379
- category: {
380
- id: 1,
381
- name: 'Pets'
382
- }
383
- }]
384
- }
385
- }
386
- }
387
- }, 'pages response')
388
- }
389
-
390
- {
391
- const res = await app.inject({
392
- method: 'POST',
393
- url: '/graphql',
394
- body: {
395
- query: `
396
- query {
397
- categories {
398
- id
399
- name
400
- pages {
401
- id
402
- title
403
- category {
404
- id
405
- name
406
- }
407
- }
408
- }
409
- }
410
- `
411
- }
412
- })
413
- equal(res.statusCode, 200, 'categories.posts status code')
414
- same(res.json(), {
415
- data: {
416
- categories: [{
417
- id: 1,
418
- name: 'Pets',
419
- pages: [{
420
- id: 1,
421
- title: 'Hello',
422
- category: {
423
- id: 1,
424
- name: 'Pets'
425
- }
426
- }]
427
- }, {
428
- id: 2,
429
- name: 'Food',
430
- pages: []
431
- }]
432
- }
433
- }, 'categories.posts response')
434
- }
435
- })
436
-
437
- test('delete hook', async ({ pass, teardown, same, equal, plan }) => {
438
- plan(10)
439
- const app = fastify()
440
- app.register(sqlMapper, {
441
- ...connInfo,
442
- async onDatabaseLoad (db, sql) {
443
- await clear(db, sql)
444
-
445
- if (isSQLite) {
446
- await db.query(sql`CREATE TABLE pages (
447
- id INTEGER PRIMARY KEY,
448
- title VARCHAR(42)
449
- );`)
450
- } else {
451
- await db.query(sql`CREATE TABLE pages (
452
- id SERIAL PRIMARY KEY,
453
- title VARCHAR(42)
454
- );`)
455
- }
456
- },
457
- hooks: {
458
- Page: {
459
- async delete (original, args) {
460
- pass('delete called')
461
-
462
- equal(args.ctx.app, app)
463
- same(args.where, {
464
- id: {
465
- eq: '1'
466
- }
467
- })
468
- same(args.fields, ['id', 'title'])
469
- return original(args)
470
- }
471
- }
472
- }
473
- })
474
- app.register(sqlGraphQL)
475
- teardown(app.close.bind(app))
476
-
477
- await app.ready()
478
-
479
- {
480
- const res = await app.inject({
481
- method: 'POST',
482
- url: '/graphql',
483
- body: {
484
- query: `
485
- mutation {
486
- savePage(input: { title: "Hello" }) {
487
- id
488
- title
489
- }
490
- }
491
- `
492
- }
493
- })
494
- equal(res.statusCode, 200, 'savePage status code')
495
- same(res.json(), {
496
- data: {
497
- savePage: {
498
- id: 1,
499
- title: 'Hello'
500
- }
501
- }
502
- }, 'savePage response')
503
- }
504
-
505
- {
506
- const res = await app.inject({
507
- method: 'POST',
508
- url: '/graphql',
509
- body: {
510
- query: `
511
- mutation {
512
- deletePages(where: { id: { eq: "1" } }) {
513
- id
514
- title
515
- }
516
- }
517
- `
518
- }
519
- })
520
- equal(res.statusCode, 200, 'deletePage status code')
521
- same(res.json(), {
522
- data: {
523
- deletePages: [{
524
- id: 1,
525
- title: 'Hello'
526
- }]
527
- }
528
- }, 'deletePage response')
529
- }
530
-
531
- {
532
- const res = await app.inject({
533
- method: 'POST',
534
- url: '/graphql',
535
- body: {
536
- query: `
537
- query {
538
- getPageById(id: 1) {
539
- id
540
- title
541
- }
542
- }
543
- `
544
- }
545
- })
546
- equal(res.statusCode, 200, 'pages status code')
547
- same(res.json(), {
548
- data: {
549
- getPageById: null
550
- }
551
- }, 'pages response')
552
- }
553
- })
554
-
555
- test('false resolver no schema', async ({ pass, teardown, same, equal, plan, match }) => {
556
- plan(5)
557
- const app = fastify()
558
- app.register(sqlMapper, {
559
- ...connInfo,
560
- async onDatabaseLoad (db, sql) {
561
- pass('onDatabaseLoad called')
562
-
563
- await clear(db, sql)
564
-
565
- if (isSQLite) {
566
- await db.query(sql`CREATE TABLE pages (
567
- id INTEGER PRIMARY KEY,
568
- title VARCHAR(42)
569
- );`)
570
- } else {
571
- await db.query(sql`CREATE TABLE pages (
572
- id SERIAL PRIMARY KEY,
573
- title VARCHAR(42)
574
- );`)
575
- }
576
- }
577
- })
578
- app.register(sqlGraphQL, {
579
- schema: `
580
- extend type Query {
581
- add(a: Int!, b: Int!): Int
582
- }
583
- `,
584
- resolvers: {
585
- Mutation: {
586
- savePage: false,
587
- deletePages: false,
588
- insertPages: false
589
- },
590
- Query: {
591
- pages: false,
592
- getPageById: false
593
- }
594
- }
595
- })
596
- teardown(app.close.bind(app))
597
-
598
- await app.ready()
599
-
600
- {
601
- const res = await app.inject({
602
- method: 'POST',
603
- url: '/graphql',
604
- body: {
605
- query: `
606
- mutation {
607
- savePage(input: { title: "Hello" }) {
608
- id
609
- title
610
- }
611
- }
612
- `
613
- }
614
- })
615
- equal(res.statusCode, 200, 'savePage status code')
616
- }
617
-
618
- {
619
- const res = await app.inject({
620
- method: 'POST',
621
- url: '/graphql',
622
- body: {
623
- query: `
624
- query {
625
- getPageById(id: 1) {
626
- id
627
- title
628
- }
629
- }
630
- `
631
- }
632
- })
633
- equal(res.statusCode, 400, 'pages status code')
634
- match(res.json(), {
635
- data: null,
636
- errors: [{
637
- message: 'Cannot query field "getPageById" on type "Query".',
638
- locations: [{ line: 3, column: 13 }]
639
- }]
640
- })
641
- }
642
-
643
- {
644
- const res = await app.inject({
645
- method: 'POST',
646
- url: '/graphql',
647
- body: {
648
- query: `
649
- mutation {
650
- deletePages(where: { id: { eq: "1" } }) {
651
- id
652
- title
653
- }
654
- }
655
- `
656
- }
657
- })
658
- equal(res.statusCode, 200, 'deletePages status code')
659
- }
660
- })