@platformatic/sql-graphql 0.13.1 → 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/index.js CHANGED
@@ -4,6 +4,7 @@ const fp = require('fastify-plugin')
4
4
  const constructGraph = require('./lib/entity-to-type')
5
5
  const mercurius = require('mercurius')
6
6
  const graphql = require('graphql')
7
+ const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
7
8
  const establishRelations = require('./lib/relationship')
8
9
  const setupSubscriptions = require('./lib/subscriptions')
9
10
  const scalars = require('graphql-scalars')
@@ -156,7 +157,12 @@ async function mapperToGraphql (app, opts) {
156
157
 
157
158
  opts.graphiql = opts.graphiql !== false
158
159
 
159
- await app.register(mercurius, {
160
+ let plugin = mercurius
161
+ if (opts.federationMetadata) {
162
+ plugin = mercuriusFederationPlugin
163
+ }
164
+
165
+ await app.register(plugin, {
160
166
  ...opts,
161
167
  schema: sdl,
162
168
  loaders,
@@ -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] || resolvers[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.13.1",
3
+ "version": "0.15.0",
4
4
  "description": "Map SQL dbs to GraphQL",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -14,29 +14,31 @@
14
14
  },
15
15
  "homepage": "https://github.com/platformatic/platformatic#readme",
16
16
  "devDependencies": {
17
+ "@mercuriusjs/gateway": "^1.0.0",
17
18
  "fastify": "^4.10.2",
18
19
  "snazzy": "^9.0.0",
19
20
  "standard": "^17.0.0",
20
21
  "tap": "^16.3.2",
21
22
  "tsd": "^0.25.0",
22
23
  "ws": "^8.11.0",
23
- "@platformatic/sql-events": "0.13.1",
24
- "@platformatic/sql-mapper": "0.13.1"
24
+ "@platformatic/sql-events": "0.15.0",
25
+ "@platformatic/sql-mapper": "0.15.0"
25
26
  },
26
27
  "dependencies": {
28
+ "@mercuriusjs/federation": "^1.0.0",
27
29
  "camelcase": "^6.3.0",
28
30
  "fastify-plugin": "^4.4.0",
29
31
  "graphql": "^16.6.0",
30
32
  "graphql-scalars": "^1.20.1",
31
33
  "graphql-type-json": "^0.3.2",
32
34
  "inflected": "^2.1.0",
33
- "mercurius": "^11.4.0"
35
+ "mercurius": "^12.0.0"
34
36
  },
35
37
  "tsd": {
36
38
  "directory": "test/types"
37
39
  },
38
40
  "scripts": {
39
- "lint": "standard",
41
+ "lint": "standard | snazzy",
40
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",
41
43
  "test:postgresql": "DB=postgresql tap test/*.test.js",
42
44
  "test:mariadb": "DB=mariadb tap test/*.test.js",
@@ -723,7 +723,7 @@ test('[SQLite] - UUID', { skip: !isSQLite }, async ({ pass, teardown, same, equa
723
723
  }
724
724
  })
725
725
 
726
- test('BIGINT!', { skip: isSQLite, only: true }, async ({ pass, teardown, same, equal }) => {
726
+ test('BIGINT!', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
727
727
  const app = fastify()
728
728
  app.register(sqlMapper, {
729
729
  ...connInfo,
@@ -2,17 +2,17 @@
2
2
 
3
3
  const { test } = require('tap')
4
4
  const Fastify = require('fastify')
5
- const mercurius = require('mercurius')
5
+ const Gateway = require('@mercuriusjs/gateway')
6
+ const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
6
7
  const sqlGraphQL = require('..')
7
8
  const sqlMapper = require('@platformatic/sql-mapper')
8
9
  const { clear, connInfo, isSQLite } = require('./helper')
9
10
 
10
11
  async function createTestService (t, schema, resolvers = {}) {
11
12
  const service = Fastify({ logger: { level: 'error' } })
12
- service.register(mercurius, {
13
+ service.register(mercuriusFederationPlugin, {
13
14
  schema,
14
- resolvers,
15
- federationMetadata: true
15
+ resolvers
16
16
  })
17
17
  await service.listen({ port: 0 })
18
18
  return [service, service.server.address().port]
@@ -155,7 +155,7 @@ async function createTestGatewayServer (t, cacheOpts) {
155
155
  await categoryService.close()
156
156
  await postService.close()
157
157
  })
158
- gateway.register(mercurius, {
158
+ gateway.register(Gateway, {
159
159
  gateway: {
160
160
  services: [{
161
161
  name: 'category',
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) {
@@ -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
+ })