@platformatic/sql-graphql 0.19.1 → 0.19.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.
- package/lib/entity-to-type.js +7 -16
- package/lib/relationship.js +5 -1
- package/package.json +3 -3
- package/test/relations.test.js +205 -0
package/lib/entity-to-type.js
CHANGED
|
@@ -318,33 +318,24 @@ function constructGraph (app, entity, opts, ignore) {
|
|
|
318
318
|
return acc
|
|
319
319
|
}, {})
|
|
320
320
|
|
|
321
|
-
const
|
|
321
|
+
const rows = await entity.find({
|
|
322
322
|
where,
|
|
323
323
|
fields,
|
|
324
324
|
limit,
|
|
325
325
|
ctx
|
|
326
326
|
})
|
|
327
327
|
|
|
328
|
-
const
|
|
328
|
+
const matchedRaws = []
|
|
329
329
|
// TODO this is extremely inefficient
|
|
330
330
|
// we need a better data structure
|
|
331
331
|
for (const pair of keys) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
target = null
|
|
337
|
-
break
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (target) {
|
|
342
|
-
output.push(target)
|
|
343
|
-
}
|
|
344
|
-
}
|
|
332
|
+
const matchedRaw = rows.find((raw) =>
|
|
333
|
+
pair.every(({ key, value }) => raw[key] === value)
|
|
334
|
+
) || null
|
|
335
|
+
matchedRaws.push(matchedRaw)
|
|
345
336
|
}
|
|
346
337
|
|
|
347
|
-
return
|
|
338
|
+
return matchedRaws
|
|
348
339
|
}
|
|
349
340
|
|
|
350
341
|
function getFields (queries) {
|
package/lib/relationship.js
CHANGED
|
@@ -40,7 +40,11 @@ module.exports = function establishRelations (app, relations, resolvers, loaders
|
|
|
40
40
|
loader (queries, ctx) {
|
|
41
41
|
const keys = []
|
|
42
42
|
for (const { obj } of queries) {
|
|
43
|
-
|
|
43
|
+
const value = obj[originalField] != null
|
|
44
|
+
? obj[originalField].toString()
|
|
45
|
+
: null
|
|
46
|
+
|
|
47
|
+
keys.push([{ key, value }])
|
|
44
48
|
}
|
|
45
49
|
return foreign.loadMany(keys, queries, ctx)
|
|
46
50
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-graphql",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.2",
|
|
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.4",
|
|
22
22
|
"tsd": "^0.28.0",
|
|
23
23
|
"ws": "^8.12.1",
|
|
24
|
-
"@platformatic/sql-events": "0.19.
|
|
25
|
-
"@platformatic/sql-mapper": "0.19.
|
|
24
|
+
"@platformatic/sql-events": "0.19.2",
|
|
25
|
+
"@platformatic/sql-mapper": "0.19.2"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@mercuriusjs/federation": "^1.0.1",
|
package/test/relations.test.js
CHANGED
|
@@ -581,3 +581,208 @@ test('should handle reads from save', { only: true }, async ({ pass, teardown, s
|
|
|
581
581
|
})
|
|
582
582
|
}
|
|
583
583
|
})
|
|
584
|
+
|
|
585
|
+
test('should handle nullable relation', async ({ pass, teardown, same, equal }) => {
|
|
586
|
+
const app = fastify()
|
|
587
|
+
app.register(sqlMapper, {
|
|
588
|
+
...connInfo,
|
|
589
|
+
async onDatabaseLoad (db, sql) {
|
|
590
|
+
pass('onDatabaseLoad called')
|
|
591
|
+
|
|
592
|
+
await clear(db, sql)
|
|
593
|
+
|
|
594
|
+
if (isMysql) {
|
|
595
|
+
await db.query(sql`
|
|
596
|
+
CREATE TABLE authors (
|
|
597
|
+
id SERIAL PRIMARY KEY,
|
|
598
|
+
name VARCHAR(255)
|
|
599
|
+
);
|
|
600
|
+
CREATE TABLE books (
|
|
601
|
+
id SERIAL PRIMARY KEY,
|
|
602
|
+
title VARCHAR(42),
|
|
603
|
+
author_id BIGINT UNSIGNED,
|
|
604
|
+
FOREIGN KEY (author_id) REFERENCES authors(id)
|
|
605
|
+
);
|
|
606
|
+
`)
|
|
607
|
+
} else if (isSQLite) {
|
|
608
|
+
await db.query(sql`
|
|
609
|
+
CREATE TABLE authors (
|
|
610
|
+
id INTEGER PRIMARY KEY,
|
|
611
|
+
name VARCHAR(255)
|
|
612
|
+
);
|
|
613
|
+
`)
|
|
614
|
+
await db.query(sql`
|
|
615
|
+
CREATE TABLE books (
|
|
616
|
+
id INTEGER PRIMARY KEY,
|
|
617
|
+
title VARCHAR(42),
|
|
618
|
+
author_id BIGINT UNSIGNED,
|
|
619
|
+
FOREIGN KEY (author_id) REFERENCES authors(id)
|
|
620
|
+
);
|
|
621
|
+
`)
|
|
622
|
+
} else {
|
|
623
|
+
await db.query(sql`
|
|
624
|
+
CREATE TABLE authors (
|
|
625
|
+
id SERIAL PRIMARY KEY,
|
|
626
|
+
name VARCHAR(42)
|
|
627
|
+
);
|
|
628
|
+
CREATE TABLE books (
|
|
629
|
+
id SERIAL PRIMARY KEY,
|
|
630
|
+
title VARCHAR(42),
|
|
631
|
+
author_id INTEGER REFERENCES authors(id)
|
|
632
|
+
);
|
|
633
|
+
`)
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
})
|
|
637
|
+
app.register(sqlGraphQL)
|
|
638
|
+
teardown(app.close.bind(app))
|
|
639
|
+
|
|
640
|
+
await app.ready()
|
|
641
|
+
|
|
642
|
+
const authors = [
|
|
643
|
+
{
|
|
644
|
+
id: 1,
|
|
645
|
+
name: 'Mark 1'
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
id: 2,
|
|
649
|
+
name: 'Mark 2'
|
|
650
|
+
}
|
|
651
|
+
]
|
|
652
|
+
|
|
653
|
+
const books = [
|
|
654
|
+
{
|
|
655
|
+
id: 1,
|
|
656
|
+
title: 'Harry 1',
|
|
657
|
+
authorId: 1
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
id: 2,
|
|
661
|
+
title: 'Harry 2',
|
|
662
|
+
authorId: null
|
|
663
|
+
},
|
|
664
|
+
{
|
|
665
|
+
id: 3,
|
|
666
|
+
title: 'Harry 3',
|
|
667
|
+
authorId: 2
|
|
668
|
+
}
|
|
669
|
+
]
|
|
670
|
+
|
|
671
|
+
{
|
|
672
|
+
const res = await app.inject({
|
|
673
|
+
method: 'POST',
|
|
674
|
+
url: '/graphql',
|
|
675
|
+
body: {
|
|
676
|
+
query: `
|
|
677
|
+
mutation batch($inputs : [AuthorInput]!) {
|
|
678
|
+
insertAuthors(inputs: $inputs) {
|
|
679
|
+
id
|
|
680
|
+
name
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
`,
|
|
684
|
+
variables: {
|
|
685
|
+
inputs: authors
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
})
|
|
689
|
+
equal(res.statusCode, 200, 'authors status code')
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
{
|
|
693
|
+
const res = await app.inject({
|
|
694
|
+
method: 'POST',
|
|
695
|
+
url: '/graphql',
|
|
696
|
+
body: {
|
|
697
|
+
query: `
|
|
698
|
+
mutation batch($inputs : [BookInput]!) {
|
|
699
|
+
insertBooks(inputs: $inputs) {
|
|
700
|
+
id
|
|
701
|
+
title
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
`,
|
|
705
|
+
variables: {
|
|
706
|
+
inputs: books
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
})
|
|
710
|
+
equal(res.statusCode, 200, 'books status code')
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
{
|
|
714
|
+
const res = await app.inject({
|
|
715
|
+
method: 'POST',
|
|
716
|
+
url: '/graphql',
|
|
717
|
+
body: {
|
|
718
|
+
query: `
|
|
719
|
+
query {
|
|
720
|
+
books (orderBy: { field: id, direction: ASC }) {
|
|
721
|
+
id
|
|
722
|
+
author {
|
|
723
|
+
id
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
`
|
|
728
|
+
}
|
|
729
|
+
})
|
|
730
|
+
equal(res.statusCode, 200, 'query books')
|
|
731
|
+
same(res.json(), {
|
|
732
|
+
data: {
|
|
733
|
+
books: [
|
|
734
|
+
{
|
|
735
|
+
id: 1,
|
|
736
|
+
author: {
|
|
737
|
+
id: 1
|
|
738
|
+
}
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
id: 2,
|
|
742
|
+
author: null
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
id: 3,
|
|
746
|
+
author: {
|
|
747
|
+
id: 2
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
]
|
|
751
|
+
}
|
|
752
|
+
}, 'query book response')
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
{
|
|
756
|
+
const res = await app.inject({
|
|
757
|
+
method: 'POST',
|
|
758
|
+
url: '/graphql',
|
|
759
|
+
body: {
|
|
760
|
+
query: `
|
|
761
|
+
query {
|
|
762
|
+
authors {
|
|
763
|
+
id
|
|
764
|
+
books {
|
|
765
|
+
id
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
`
|
|
770
|
+
}
|
|
771
|
+
})
|
|
772
|
+
equal(res.statusCode, 200, 'query authors')
|
|
773
|
+
same(res.json(), {
|
|
774
|
+
data: {
|
|
775
|
+
authors: [
|
|
776
|
+
{
|
|
777
|
+
id: 1,
|
|
778
|
+
books: [{ id: 1 }]
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
id: 2,
|
|
782
|
+
books: [{ id: 3 }]
|
|
783
|
+
}
|
|
784
|
+
]
|
|
785
|
+
}
|
|
786
|
+
}, 'query authors response')
|
|
787
|
+
}
|
|
788
|
+
})
|