@platformatic/sql-graphql 0.6.0 → 0.7.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/entity-to-type.js +12 -1
- package/lib/relationship.js +15 -11
- package/package.json +6 -6
- package/test/datatypes.test.js +25 -9
- package/test/helper.js +5 -0
package/lib/entity-to-type.js
CHANGED
|
@@ -36,7 +36,18 @@ function constructGraph (app, entity, opts) {
|
|
|
36
36
|
for (const key of Object.keys(entity.fields)) {
|
|
37
37
|
const field = entity.fields[key]
|
|
38
38
|
const meta = { field }
|
|
39
|
-
|
|
39
|
+
|
|
40
|
+
// sqlite doesn't support enums
|
|
41
|
+
/* istanbul ignore next */
|
|
42
|
+
if (field.enum) {
|
|
43
|
+
const enumValues = field.enum.reduce((acc, enumValue) => {
|
|
44
|
+
acc[enumValue] = { value: enumValue }
|
|
45
|
+
return acc
|
|
46
|
+
}, {})
|
|
47
|
+
meta.type = new graphql.GraphQLEnumType({ name: key, values: enumValues })
|
|
48
|
+
} else {
|
|
49
|
+
meta.type = sqlTypeToGraphQL(field.sqlType)
|
|
50
|
+
}
|
|
40
51
|
if (field.primaryKey) {
|
|
41
52
|
meta.primaryKeyType = field.type
|
|
42
53
|
meta.type = graphql.GraphQLID
|
package/lib/relationship.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
/* eslint camelcase: 0 */
|
|
4
|
+
|
|
3
5
|
const camelcase = require('camelcase')
|
|
4
6
|
const {
|
|
5
7
|
fromSelectionSet
|
|
@@ -13,21 +15,23 @@ module.exports = function establishRelations (app, relations, resolvers, loaders
|
|
|
13
15
|
const entity = entities[key]
|
|
14
16
|
tablesTypeMap[entity.table] = metaMap.get(entity)
|
|
15
17
|
}
|
|
16
|
-
for (const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
assert(
|
|
18
|
+
for (const { table_name, foreign_table_name, column_name } of relations) {
|
|
19
|
+
const enhanceAssertLogMsg = `(table: "${table_name}", foreign table: "${foreign_table_name}", column: "${column_name}")`
|
|
20
|
+
|
|
21
|
+
assert(table_name, `table_name is required ${enhanceAssertLogMsg}`)
|
|
22
|
+
assert(foreign_table_name, `foreign_table_name is required ${enhanceAssertLogMsg}`)
|
|
23
|
+
assert(column_name.toLowerCase().endsWith('id'), `The column with id reference must be ended with "id" postfix ${enhanceAssertLogMsg}`)
|
|
20
24
|
|
|
21
|
-
const current = tablesTypeMap[
|
|
22
|
-
const foreign = tablesTypeMap[
|
|
23
|
-
assert(foreign !== undefined, `No foreign table named "${
|
|
25
|
+
const current = tablesTypeMap[table_name]
|
|
26
|
+
const foreign = tablesTypeMap[foreign_table_name]
|
|
27
|
+
assert(foreign !== undefined, `No foreign table named "${foreign_table_name}" was found ${enhanceAssertLogMsg}`)
|
|
24
28
|
|
|
25
29
|
// current to foreign
|
|
26
30
|
{
|
|
27
|
-
const lowered = lowerCaseFirst(camelcase(cutOutIdEnding(
|
|
31
|
+
const lowered = lowerCaseFirst(camelcase(cutOutIdEnding(column_name)))
|
|
28
32
|
if (!relationships[current.type] || relationships[current.type][lowered] !== false) {
|
|
29
33
|
current.fields[lowered] = { type: foreign.type }
|
|
30
|
-
const originalField = camelcase(
|
|
34
|
+
const originalField = camelcase(column_name)
|
|
31
35
|
delete current.fields[originalField]
|
|
32
36
|
loaders[current.type] = loaders[current.type] || resolvers[current.type] || {}
|
|
33
37
|
loaders[current.type][lowered] = {
|
|
@@ -52,9 +56,9 @@ module.exports = function establishRelations (app, relations, resolvers, loaders
|
|
|
52
56
|
resolvers[foreign.type] = resolvers[foreign.type] || {}
|
|
53
57
|
const resolveRelation = async function (obj, args, ctx, info) {
|
|
54
58
|
const fields = fromSelectionSet(info.fieldNodes[0].selectionSet, new Set())
|
|
55
|
-
const toSearch = { ...args, fields: [...fields,
|
|
59
|
+
const toSearch = { ...args, fields: [...fields, column_name], ctx }
|
|
56
60
|
toSearch.where = toSearch.where || {}
|
|
57
|
-
toSearch.where[camelcase(
|
|
61
|
+
toSearch.where[camelcase(column_name)] = { eq: obj.id }
|
|
58
62
|
return current.entity.find(toSearch)
|
|
59
63
|
}
|
|
60
64
|
if (resolvers[foreign.type][lowered] === undefined) {
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Map SQL dbs to GraphQL",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/
|
|
8
|
+
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
9
9
|
},
|
|
10
10
|
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
11
11
|
"license": "Apache-2.0",
|
|
12
12
|
"bugs": {
|
|
13
|
-
"url": "https://github.com/
|
|
13
|
+
"url": "https://github.com/platformatic/platformatic/issues"
|
|
14
14
|
},
|
|
15
|
-
"homepage": "https://github.com/
|
|
15
|
+
"homepage": "https://github.com/platformatic/platformatic#readme",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@platformatic/sql-events": "0.
|
|
18
|
-
"@platformatic/sql-mapper": "0.
|
|
17
|
+
"@platformatic/sql-events": "0.7.0",
|
|
18
|
+
"@platformatic/sql-mapper": "0.7.0",
|
|
19
19
|
"fastify": "^4.6.0",
|
|
20
20
|
"snazzy": "^9.0.0",
|
|
21
21
|
"standard": "^17.0.0",
|
package/test/datatypes.test.js
CHANGED
|
@@ -15,7 +15,9 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
15
15
|
|
|
16
16
|
await clear(db, sql)
|
|
17
17
|
|
|
18
|
-
await db.query(sql`
|
|
18
|
+
await db.query(sql`
|
|
19
|
+
CREATE TYPE simple_enum as ENUM ('value1', 'value2');
|
|
20
|
+
CREATE TABLE simple_types (
|
|
19
21
|
id SERIAL8 PRIMARY KEY,
|
|
20
22
|
published BOOL,
|
|
21
23
|
current DOUBLE PRECISION,
|
|
@@ -26,7 +28,8 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
26
28
|
uuid UUID UNIQUE,
|
|
27
29
|
a_real real,
|
|
28
30
|
a_smallint smallint,
|
|
29
|
-
a_decimal decimal
|
|
31
|
+
a_decimal decimal,
|
|
32
|
+
an_enum simple_enum
|
|
30
33
|
);`)
|
|
31
34
|
}
|
|
32
35
|
})
|
|
@@ -55,7 +58,9 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
55
58
|
uuid: "12345678-1234-1234-1234-123456789012",
|
|
56
59
|
aReal: 1.2,
|
|
57
60
|
aSmallint: 42,
|
|
58
|
-
aDecimal: 42
|
|
61
|
+
aDecimal: 42,
|
|
62
|
+
anEnum: value1
|
|
63
|
+
}) {
|
|
59
64
|
id
|
|
60
65
|
published
|
|
61
66
|
current
|
|
@@ -67,6 +72,7 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
67
72
|
aReal
|
|
68
73
|
aSmallint
|
|
69
74
|
aDecimal
|
|
75
|
+
anEnum
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
`
|
|
@@ -86,7 +92,8 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
86
92
|
uuid: '12345678-1234-1234-1234-123456789012',
|
|
87
93
|
aReal: 1.2,
|
|
88
94
|
aSmallint: 42,
|
|
89
|
-
aDecimal: 42
|
|
95
|
+
aDecimal: 42,
|
|
96
|
+
anEnum: 'value1'
|
|
90
97
|
}
|
|
91
98
|
}
|
|
92
99
|
}, 'saveSimpleType response')
|
|
@@ -111,6 +118,7 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
111
118
|
aReal
|
|
112
119
|
aSmallint
|
|
113
120
|
aDecimal
|
|
121
|
+
anEnum
|
|
114
122
|
}
|
|
115
123
|
}
|
|
116
124
|
`
|
|
@@ -130,7 +138,8 @@ test('[PG] simple db simple graphql schema', { skip: !isPg }, async ({ pass, tea
|
|
|
130
138
|
uuid: '12345678-1234-1234-1234-123456789012',
|
|
131
139
|
aReal: 1.2,
|
|
132
140
|
aSmallint: 42,
|
|
133
|
-
aDecimal: 42
|
|
141
|
+
aDecimal: 42,
|
|
142
|
+
anEnum: 'value1'
|
|
134
143
|
}
|
|
135
144
|
}
|
|
136
145
|
}, 'getSimpleTypeById response')
|
|
@@ -262,7 +271,8 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
262
271
|
uuid ${uuidType} UNIQUE,
|
|
263
272
|
a_real real,
|
|
264
273
|
a_smallint smallint,
|
|
265
|
-
a_decimal decimal
|
|
274
|
+
a_decimal decimal,
|
|
275
|
+
an_enum enum ('value1', 'value2')
|
|
266
276
|
);`)
|
|
267
277
|
}
|
|
268
278
|
})
|
|
@@ -293,7 +303,9 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
293
303
|
uuid: "12345678-1234-1234-1234-123456789012",
|
|
294
304
|
aReal: 1.2,
|
|
295
305
|
aSmallint: 42,
|
|
296
|
-
aDecimal: 42
|
|
306
|
+
aDecimal: 42,
|
|
307
|
+
anEnum: value1
|
|
308
|
+
}) {
|
|
297
309
|
id
|
|
298
310
|
published
|
|
299
311
|
current
|
|
@@ -305,6 +317,7 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
305
317
|
aReal
|
|
306
318
|
aSmallint
|
|
307
319
|
aDecimal
|
|
320
|
+
anEnum
|
|
308
321
|
}
|
|
309
322
|
}
|
|
310
323
|
`
|
|
@@ -324,7 +337,8 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
324
337
|
uuid: '12345678-1234-1234-1234-123456789012',
|
|
325
338
|
aReal: 1.2,
|
|
326
339
|
aSmallint: 42,
|
|
327
|
-
aDecimal: 42
|
|
340
|
+
aDecimal: 42,
|
|
341
|
+
anEnum: 'value1'
|
|
328
342
|
}
|
|
329
343
|
}
|
|
330
344
|
}, 'saveSimpleType response')
|
|
@@ -349,6 +363,7 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
349
363
|
aReal
|
|
350
364
|
aSmallint
|
|
351
365
|
aDecimal
|
|
366
|
+
anEnum
|
|
352
367
|
}
|
|
353
368
|
}
|
|
354
369
|
`
|
|
@@ -368,7 +383,8 @@ test('[MySQL] simple db simple graphql schema', { skip: !isMysql }, async ({ pas
|
|
|
368
383
|
uuid: '12345678-1234-1234-1234-123456789012',
|
|
369
384
|
aReal: 1.2,
|
|
370
385
|
aSmallint: 42,
|
|
371
|
-
aDecimal: 42
|
|
386
|
+
aDecimal: 42,
|
|
387
|
+
anEnum: 'value1'
|
|
372
388
|
}
|
|
373
389
|
}
|
|
374
390
|
}, 'getSimpleTypeById response')
|