@platformatic/sql-graphql 0.13.0 → 0.14.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 +7 -1
- package/lib/relationship.js +1 -1
- package/package.json +6 -4
- package/test/datatypes.test.js +1 -1
- package/test/federation.test.js +5 -5
- package/test/helper.js +20 -0
- package/test/schema.test.js +111 -1
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
|
-
|
|
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,
|
package/lib/relationship.js
CHANGED
|
@@ -23,7 +23,7 @@ module.exports = function establishRelations (app, relations, resolvers, loaders
|
|
|
23
23
|
|
|
24
24
|
const current = entitiesTypeMap[entityName]
|
|
25
25
|
const foreign = entitiesTypeMap[foreignEntityName]
|
|
26
|
-
assert(foreign !== undefined, `No foreign table named "${foreign_table_name}" was found ${enhanceAssertLogMsg}`)
|
|
26
|
+
assert(foreign !== undefined, `No foreign table named "${foreign_table_name}" was found ${enhanceAssertLogMsg}. Relation: ${JSON.stringify(relation, null, 2)}`)
|
|
27
27
|
|
|
28
28
|
// current to foreign, we skip this if the foreign table has a composite primary key
|
|
29
29
|
// TODO implement support for this case
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Map SQL dbs to GraphQL",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -14,23 +14,25 @@
|
|
|
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-
|
|
24
|
-
"@platformatic/sql-
|
|
24
|
+
"@platformatic/sql-events": "0.14.0",
|
|
25
|
+
"@platformatic/sql-mapper": "0.14.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": "^
|
|
35
|
+
"mercurius": "^12.0.0"
|
|
34
36
|
},
|
|
35
37
|
"tsd": {
|
|
36
38
|
"directory": "test/types"
|
package/test/datatypes.test.js
CHANGED
|
@@ -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
|
|
726
|
+
test('BIGINT!', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
|
|
727
727
|
const app = fastify()
|
|
728
728
|
app.register(sqlMapper, {
|
|
729
729
|
...connInfo,
|
package/test/federation.test.js
CHANGED
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
const { test } = require('tap')
|
|
4
4
|
const Fastify = require('fastify')
|
|
5
|
-
const
|
|
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(
|
|
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(
|
|
158
|
+
gateway.register(Gateway, {
|
|
159
159
|
gateway: {
|
|
160
160
|
services: [{
|
|
161
161
|
name: 'category',
|
package/test/helper.js
CHANGED
|
@@ -127,6 +127,16 @@ module.exports.clear = async function (db, sql) {
|
|
|
127
127
|
} catch (err) {
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
try {
|
|
131
|
+
await db.query(sql`DROP TABLE test4.books`)
|
|
132
|
+
} catch (err) {
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
await db.query(sql`DROP TABLE test3.authors`)
|
|
137
|
+
} catch (err) {
|
|
138
|
+
}
|
|
139
|
+
|
|
130
140
|
try {
|
|
131
141
|
await db.query(sql`DROP TABLE test2.books`)
|
|
132
142
|
} catch (err) {
|
|
@@ -137,6 +147,16 @@ module.exports.clear = async function (db, sql) {
|
|
|
137
147
|
} catch (err) {
|
|
138
148
|
}
|
|
139
149
|
|
|
150
|
+
try {
|
|
151
|
+
await db.query(sql`DROP SCHEMA test4`)
|
|
152
|
+
} catch (err) {
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
await db.query(sql`DROP SCHEMA test3`)
|
|
157
|
+
} catch (err) {
|
|
158
|
+
}
|
|
159
|
+
|
|
140
160
|
try {
|
|
141
161
|
await db.query(sql`DROP SCHEMA test2`)
|
|
142
162
|
} catch (err) {
|
package/test/schema.test.js
CHANGED
|
@@ -4,7 +4,7 @@ const { test } = require('tap')
|
|
|
4
4
|
const sqlGraphQL = require('..')
|
|
5
5
|
const sqlMapper = require('@platformatic/sql-mapper')
|
|
6
6
|
const fastify = require('fastify')
|
|
7
|
-
const { isSQLite, connInfo, isMysql, clear } = require('./helper')
|
|
7
|
+
const { isSQLite, isPg, connInfo, isMysql, clear } = require('./helper')
|
|
8
8
|
|
|
9
9
|
test('should handle relationships with different schemas', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
|
|
10
10
|
const app = fastify()
|
|
@@ -165,3 +165,113 @@ test('should handle relationships with different schemas', { skip: isSQLite }, a
|
|
|
165
165
|
}, 'query authors response')
|
|
166
166
|
}
|
|
167
167
|
})
|
|
168
|
+
|
|
169
|
+
test('should not throw if all of the schema with contraint references are loaded on the config', { skip: !isPg }, async ({ pass, teardown, same }) => {
|
|
170
|
+
const app = fastify()
|
|
171
|
+
app.register(sqlMapper, {
|
|
172
|
+
...connInfo,
|
|
173
|
+
schema: ['test1', 'test2', 'test3', 'test4'],
|
|
174
|
+
async onDatabaseLoad (db, sql) {
|
|
175
|
+
pass('onDatabaseLoad called')
|
|
176
|
+
|
|
177
|
+
await clear(db, sql)
|
|
178
|
+
|
|
179
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
|
|
180
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
|
|
181
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test3;`)
|
|
182
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test4;`)
|
|
183
|
+
await db.query(sql`
|
|
184
|
+
CREATE TABLE test1.authors (
|
|
185
|
+
id INTEGER PRIMARY KEY,
|
|
186
|
+
name VARCHAR(42)
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
CREATE TABLE test2.books (
|
|
190
|
+
id INTEGER PRIMARY KEY,
|
|
191
|
+
title VARCHAR(42),
|
|
192
|
+
author_id INTEGER
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
CREATE TABLE test3.authors (
|
|
196
|
+
id INTEGER PRIMARY KEY,
|
|
197
|
+
name VARCHAR(42)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
CREATE TABLE test4.books (
|
|
201
|
+
id INTEGER PRIMARY KEY,
|
|
202
|
+
title VARCHAR(42),
|
|
203
|
+
author_id INTEGER
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
ALTER TABLE ONLY test2.books
|
|
207
|
+
ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test1.authors(id);
|
|
208
|
+
|
|
209
|
+
ALTER TABLE ONLY test4.books
|
|
210
|
+
ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test3.authors(id);
|
|
211
|
+
`)
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
app.register(sqlGraphQL)
|
|
215
|
+
teardown(app.close.bind(app))
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
await app.ready()
|
|
219
|
+
} catch (error) {
|
|
220
|
+
same(true, false, 'app should not throw')
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('should not throw if some of the schema with contraint references are not passed to the config', { skip: !isPg }, async ({ pass, teardown, same }) => {
|
|
225
|
+
const app = fastify()
|
|
226
|
+
app.register(sqlMapper, {
|
|
227
|
+
...connInfo,
|
|
228
|
+
schema: ['test1', 'test2'],
|
|
229
|
+
async onDatabaseLoad (db, sql) {
|
|
230
|
+
pass('onDatabaseLoad called')
|
|
231
|
+
|
|
232
|
+
await clear(db, sql)
|
|
233
|
+
|
|
234
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
|
|
235
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
|
|
236
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test3;`)
|
|
237
|
+
await db.query(sql`CREATE SCHEMA IF NOT EXISTS test4;`)
|
|
238
|
+
await db.query(sql`
|
|
239
|
+
CREATE TABLE test1.authors (
|
|
240
|
+
id INTEGER PRIMARY KEY,
|
|
241
|
+
name VARCHAR(42)
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
CREATE TABLE test2.books (
|
|
245
|
+
id INTEGER PRIMARY KEY,
|
|
246
|
+
title VARCHAR(42),
|
|
247
|
+
author_id INTEGER
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
CREATE TABLE test3.authors (
|
|
251
|
+
id INTEGER PRIMARY KEY,
|
|
252
|
+
name VARCHAR(42)
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
CREATE TABLE test4.books (
|
|
256
|
+
id INTEGER PRIMARY KEY,
|
|
257
|
+
title VARCHAR(42),
|
|
258
|
+
author_id INTEGER
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
ALTER TABLE ONLY test2.books
|
|
262
|
+
ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test1.authors(id);
|
|
263
|
+
|
|
264
|
+
ALTER TABLE ONLY test4.books
|
|
265
|
+
ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test3.authors(id);
|
|
266
|
+
`)
|
|
267
|
+
}
|
|
268
|
+
})
|
|
269
|
+
app.register(sqlGraphQL)
|
|
270
|
+
teardown(app.close.bind(app))
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
await app.ready()
|
|
274
|
+
} catch (error) {
|
|
275
|
+
same(true, false, 'we expect the app to not throw')
|
|
276
|
+
}
|
|
277
|
+
})
|