@platformatic/sql-graphql 0.14.0 → 0.15.0
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.
- package/lib/relationship.js +1 -1
- package/package.json +4 -4
- package/test/helper.js +10 -10
- package/test/relations.test.js +197 -0
package/lib/relationship.js
CHANGED
|
@@ -34,7 +34,7 @@ module.exports = function establishRelations (app, relations, resolvers, loaders
|
|
|
34
34
|
const originalField = camelcase(column_name)
|
|
35
35
|
delete current.fields[originalField]
|
|
36
36
|
current.fields[lowered] = { type: foreign.type }
|
|
37
|
-
loaders[current.type] = loaders[current.type] ||
|
|
37
|
+
loaders[current.type] = loaders[current.type] || {}
|
|
38
38
|
const key = camelcase(foreign_column_name)
|
|
39
39
|
loaders[current.type][lowered] = {
|
|
40
40
|
loader (queries, ctx) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Map SQL dbs to GraphQL",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"tap": "^16.3.2",
|
|
22
22
|
"tsd": "^0.25.0",
|
|
23
23
|
"ws": "^8.11.0",
|
|
24
|
-
"@platformatic/sql-events": "0.
|
|
25
|
-
"@platformatic/sql-mapper": "0.
|
|
24
|
+
"@platformatic/sql-events": "0.15.0",
|
|
25
|
+
"@platformatic/sql-mapper": "0.15.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@mercuriusjs/federation": "^1.0.0",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"directory": "test/types"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
|
-
"lint": "standard",
|
|
41
|
+
"lint": "standard | snazzy",
|
|
42
42
|
"test": "standard | snazzy && npm run test:typescript && npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
|
|
43
43
|
"test:postgresql": "DB=postgresql tap test/*.test.js",
|
|
44
44
|
"test:mariadb": "DB=mariadb tap test/*.test.js",
|
package/test/helper.js
CHANGED
|
@@ -40,9 +40,18 @@ module.exports.clear = async function (db, sql) {
|
|
|
40
40
|
} catch (err) {
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
try {
|
|
44
|
+
await db.query(sql`DROP TABLE books`)
|
|
45
|
+
} catch (err) {
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
await db.query(sql`DROP TABLE authors`)
|
|
49
|
+
} catch (err) {
|
|
50
|
+
}
|
|
51
|
+
|
|
43
52
|
try {
|
|
44
53
|
await db.query(sql`DROP TABLE categories`)
|
|
45
|
-
} catch {
|
|
54
|
+
} catch (err) {
|
|
46
55
|
}
|
|
47
56
|
|
|
48
57
|
try {
|
|
@@ -74,15 +83,6 @@ module.exports.clear = async function (db, sql) {
|
|
|
74
83
|
} catch {
|
|
75
84
|
}
|
|
76
85
|
|
|
77
|
-
try {
|
|
78
|
-
await db.query(sql`DROP TABLE books`)
|
|
79
|
-
} catch (err) {
|
|
80
|
-
}
|
|
81
|
-
try {
|
|
82
|
-
await db.query(sql`DROP TABLE authors`)
|
|
83
|
-
} catch (err) {
|
|
84
|
-
}
|
|
85
|
-
|
|
86
86
|
try {
|
|
87
87
|
await db.query(sql`DROP TABLE organizations`)
|
|
88
88
|
} catch (err) {
|
package/test/relations.test.js
CHANGED
|
@@ -384,3 +384,200 @@ test('cut out id exactly from ending when forming a name of relation', async ({
|
|
|
384
384
|
}, 'query organization response')
|
|
385
385
|
}
|
|
386
386
|
})
|
|
387
|
+
|
|
388
|
+
test('should handle reads from save', { only: true }, async ({ pass, teardown, same, equal }) => {
|
|
389
|
+
const app = fastify({ logger: false })
|
|
390
|
+
app.register(sqlMapper, {
|
|
391
|
+
...connInfo,
|
|
392
|
+
async onDatabaseLoad (db, sql) {
|
|
393
|
+
pass('onDatabaseLoad called')
|
|
394
|
+
|
|
395
|
+
await clear(db, sql)
|
|
396
|
+
|
|
397
|
+
if (isMysql) {
|
|
398
|
+
await db.query(sql`
|
|
399
|
+
CREATE TABLE categories (
|
|
400
|
+
id SERIAL PRIMARY KEY,
|
|
401
|
+
name VARCHAR(255)
|
|
402
|
+
);
|
|
403
|
+
CREATE TABLE authors (
|
|
404
|
+
id SERIAL PRIMARY KEY,
|
|
405
|
+
name VARCHAR(255),
|
|
406
|
+
category_id BIGINT UNSIGNED,
|
|
407
|
+
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
|
|
408
|
+
);
|
|
409
|
+
CREATE TABLE books (
|
|
410
|
+
id SERIAL PRIMARY KEY,
|
|
411
|
+
title VARCHAR(42),
|
|
412
|
+
author_id BIGINT UNSIGNED,
|
|
413
|
+
category_id BIGINT UNSIGNED,
|
|
414
|
+
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
|
|
415
|
+
FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
|
|
416
|
+
);
|
|
417
|
+
`)
|
|
418
|
+
} else if (isSQLite) {
|
|
419
|
+
await db.query(sql`
|
|
420
|
+
CREATE TABLE categories (
|
|
421
|
+
id INTEGER PRIMARY KEY,
|
|
422
|
+
name VARCHAR(255)
|
|
423
|
+
);
|
|
424
|
+
`)
|
|
425
|
+
await db.query(sql`
|
|
426
|
+
CREATE TABLE authors (
|
|
427
|
+
id INTEGER PRIMARY KEY,
|
|
428
|
+
name VARCHAR(255),
|
|
429
|
+
category_id BIGINT UNSIGNED,
|
|
430
|
+
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
|
|
431
|
+
);
|
|
432
|
+
`)
|
|
433
|
+
await db.query(sql`
|
|
434
|
+
CREATE TABLE books (
|
|
435
|
+
id INTEGER PRIMARY KEY,
|
|
436
|
+
title VARCHAR(42),
|
|
437
|
+
author_id BIGINT UNSIGNED,
|
|
438
|
+
category_id BIGINT UNSIGNED,
|
|
439
|
+
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
|
|
440
|
+
FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
|
|
441
|
+
);
|
|
442
|
+
`)
|
|
443
|
+
} else {
|
|
444
|
+
await db.query(sql`
|
|
445
|
+
CREATE TABLE categories (
|
|
446
|
+
id SERIAL PRIMARY KEY,
|
|
447
|
+
name VARCHAR(42)
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
CREATE TABLE authors (
|
|
451
|
+
id SERIAL PRIMARY KEY,
|
|
452
|
+
name VARCHAR(42),
|
|
453
|
+
category_id INTEGER REFERENCES categories(id)
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
CREATE TABLE books (
|
|
457
|
+
id SERIAL PRIMARY KEY,
|
|
458
|
+
title VARCHAR(42),
|
|
459
|
+
category_id INTEGER REFERENCES categories(id),
|
|
460
|
+
author_id INTEGER REFERENCES authors(id)
|
|
461
|
+
);
|
|
462
|
+
`)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
})
|
|
466
|
+
app.register(sqlGraphQL)
|
|
467
|
+
teardown(app.close.bind(app))
|
|
468
|
+
|
|
469
|
+
await app.ready()
|
|
470
|
+
|
|
471
|
+
const categories = [{
|
|
472
|
+
name: 'Sci-Fi'
|
|
473
|
+
}]
|
|
474
|
+
|
|
475
|
+
const authors = [{
|
|
476
|
+
name: 'Mark',
|
|
477
|
+
categoryId: 1
|
|
478
|
+
}]
|
|
479
|
+
|
|
480
|
+
const books = [{
|
|
481
|
+
title: 'Harry',
|
|
482
|
+
authorId: 1,
|
|
483
|
+
categoryId: 1
|
|
484
|
+
}]
|
|
485
|
+
|
|
486
|
+
{
|
|
487
|
+
const res = await app.inject({
|
|
488
|
+
method: 'POST',
|
|
489
|
+
url: '/graphql',
|
|
490
|
+
body: {
|
|
491
|
+
query: `
|
|
492
|
+
mutation batch($inputs : [CategoryInput]!) {
|
|
493
|
+
insertCategories(inputs: $inputs) {
|
|
494
|
+
id
|
|
495
|
+
name
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
`,
|
|
499
|
+
variables: {
|
|
500
|
+
inputs: categories
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
})
|
|
504
|
+
equal(res.statusCode, 200, 'categories status code')
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
{
|
|
508
|
+
const res = await app.inject({
|
|
509
|
+
method: 'POST',
|
|
510
|
+
url: '/graphql',
|
|
511
|
+
body: {
|
|
512
|
+
query: `
|
|
513
|
+
mutation batch($inputs : [AuthorInput]!) {
|
|
514
|
+
insertAuthors(inputs: $inputs) {
|
|
515
|
+
id
|
|
516
|
+
name
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
`,
|
|
520
|
+
variables: {
|
|
521
|
+
inputs: authors
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
})
|
|
525
|
+
equal(res.statusCode, 200, 'authors status code')
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
{
|
|
529
|
+
const res = await app.inject({
|
|
530
|
+
method: 'POST',
|
|
531
|
+
url: '/graphql',
|
|
532
|
+
body: {
|
|
533
|
+
query: `
|
|
534
|
+
mutation batch($inputs : [BookInput]!) {
|
|
535
|
+
insertBooks(inputs: $inputs) {
|
|
536
|
+
id
|
|
537
|
+
title
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
`,
|
|
541
|
+
variables: {
|
|
542
|
+
inputs: books
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
})
|
|
546
|
+
equal(res.statusCode, 200, 'books status code')
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
{
|
|
550
|
+
const res = await app.inject({
|
|
551
|
+
method: 'POST',
|
|
552
|
+
url: '/graphql',
|
|
553
|
+
body: {
|
|
554
|
+
query: `
|
|
555
|
+
mutation {
|
|
556
|
+
saveAuthor(input: { id: 1, name: "Mark II" }) {
|
|
557
|
+
id
|
|
558
|
+
books {
|
|
559
|
+
id
|
|
560
|
+
title
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
`,
|
|
565
|
+
variables: {
|
|
566
|
+
inputs: books
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
})
|
|
570
|
+
equal(res.statusCode, 200, 'books status code')
|
|
571
|
+
same(res.json(), {
|
|
572
|
+
data: {
|
|
573
|
+
saveAuthor: {
|
|
574
|
+
id: 1,
|
|
575
|
+
books: [{
|
|
576
|
+
id: 1,
|
|
577
|
+
title: 'Harry'
|
|
578
|
+
}]
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
})
|
|
582
|
+
}
|
|
583
|
+
})
|