@platformatic/sql-graphql 0.12.1 → 0.13.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 +11 -5
- package/package.json +3 -3
- package/test/enum.test.js +125 -0
package/lib/entity-to-type.js
CHANGED
|
@@ -49,13 +49,19 @@ function constructGraph (app, entity, opts, ignore) {
|
|
|
49
49
|
// sqlite doesn't support enums
|
|
50
50
|
/* istanbul ignore next */
|
|
51
51
|
if (field.enum) {
|
|
52
|
-
const enumValues = field.enum.reduce((acc, enumValue) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
const enumValues = field.enum.reduce((acc, enumValue, index) => {
|
|
53
|
+
let key = enumValue.replace(/[^\w\s]/g, '_')
|
|
54
|
+
|
|
55
|
+
const keyStartsWithLetterOrUnderscore = !!key.match(/^[_a-zA-Z]/g)
|
|
56
|
+
if (!keyStartsWithLetterOrUnderscore) {
|
|
57
|
+
key = `_${key}`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (key.startsWith('__')) {
|
|
61
|
+
key = key.replace('__', `enum${++index}`)
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
acc[
|
|
64
|
+
acc[key] = { value: enumValue }
|
|
59
65
|
return acc
|
|
60
66
|
}, {})
|
|
61
67
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Map SQL dbs to GraphQL",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"tap": "^16.3.2",
|
|
21
21
|
"tsd": "^0.25.0",
|
|
22
22
|
"ws": "^8.11.0",
|
|
23
|
-
"@platformatic/sql-
|
|
24
|
-
"@platformatic/sql-
|
|
23
|
+
"@platformatic/sql-mapper": "0.13.0",
|
|
24
|
+
"@platformatic/sql-events": "0.13.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"camelcase": "^6.3.0",
|
package/test/enum.test.js
CHANGED
|
@@ -98,3 +98,128 @@ test('should not fail if tables have duplicate enum names', { skip: isSQLite },
|
|
|
98
98
|
same(true, false, 'Previous call should never fail')
|
|
99
99
|
}
|
|
100
100
|
})
|
|
101
|
+
|
|
102
|
+
test('should not fail if tables have enum with special characters', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
|
|
103
|
+
const app = fastify()
|
|
104
|
+
app.register(sqlMapper, {
|
|
105
|
+
...connInfo,
|
|
106
|
+
async onDatabaseLoad (db, sql) {
|
|
107
|
+
pass('onDatabaseLoad called')
|
|
108
|
+
|
|
109
|
+
await clear(db, sql)
|
|
110
|
+
|
|
111
|
+
if (isPg) {
|
|
112
|
+
await db.query(sql`
|
|
113
|
+
CREATE TYPE custom_enum AS ENUM('.', ',');
|
|
114
|
+
|
|
115
|
+
CREATE TABLE enum_tests (
|
|
116
|
+
id INTEGER NOT NULL,
|
|
117
|
+
test_enum custom_enum,
|
|
118
|
+
PRIMARY KEY (id)
|
|
119
|
+
);
|
|
120
|
+
`)
|
|
121
|
+
} else {
|
|
122
|
+
await db.query(sql`
|
|
123
|
+
CREATE TABLE enum_tests (
|
|
124
|
+
id INTEGER NOT NULL,
|
|
125
|
+
test_enum ENUM ('.', ',') DEFAULT NULL,
|
|
126
|
+
PRIMARY KEY (id)
|
|
127
|
+
);
|
|
128
|
+
`)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
app.register(sqlGraphQL)
|
|
134
|
+
teardown(app.close.bind(app))
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await app.ready()
|
|
138
|
+
same(true, true, 'custom_enum have special chars, but app does not throws')
|
|
139
|
+
} catch (err) {
|
|
140
|
+
console.log(err)
|
|
141
|
+
same(true, false, 'Previous call should never fail')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
{
|
|
145
|
+
const res = await app.inject({
|
|
146
|
+
method: 'POST',
|
|
147
|
+
url: '/graphql',
|
|
148
|
+
body: {
|
|
149
|
+
query: `
|
|
150
|
+
mutation ($input: EnumTestInput!) {
|
|
151
|
+
saveEnumTest(input: $input) {
|
|
152
|
+
id
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
`,
|
|
156
|
+
variables: { input: { id: 1 } }
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
equal(res.statusCode, 200)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
{
|
|
163
|
+
const res = await app.inject({
|
|
164
|
+
method: 'POST',
|
|
165
|
+
url: '/graphql',
|
|
166
|
+
body: {
|
|
167
|
+
query: `
|
|
168
|
+
mutation ($input: EnumTestInput!) {
|
|
169
|
+
saveEnumTest(input: $input) {
|
|
170
|
+
id
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
`,
|
|
174
|
+
variables: { input: { id: 2 } }
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
equal(res.statusCode, 200)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
{
|
|
181
|
+
const res = await app.inject({
|
|
182
|
+
method: 'POST',
|
|
183
|
+
url: '/graphql',
|
|
184
|
+
body: {
|
|
185
|
+
query: `
|
|
186
|
+
query { enumTests { id } }
|
|
187
|
+
`
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
equal(res.statusCode, 200)
|
|
191
|
+
same(res.json(), { data: { enumTests: [{ id: 1 }, { id: 2 }] } })
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
{
|
|
195
|
+
const res = await app.inject({
|
|
196
|
+
method: 'POST',
|
|
197
|
+
url: '/graphql',
|
|
198
|
+
body: {
|
|
199
|
+
query: `
|
|
200
|
+
mutation ($input: EnumTestWhereArgumentsid!) {
|
|
201
|
+
deleteEnumTests(where: {id: $input}) {
|
|
202
|
+
id
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
`,
|
|
206
|
+
variables: { input: { eq: 1 } }
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
equal(res.statusCode, 200)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
{
|
|
213
|
+
const res = await app.inject({
|
|
214
|
+
method: 'POST',
|
|
215
|
+
url: '/graphql',
|
|
216
|
+
body: {
|
|
217
|
+
query: `
|
|
218
|
+
query { enumTests { id } }
|
|
219
|
+
`
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
equal(res.statusCode, 200)
|
|
223
|
+
same(res.json(), { data: { enumTests: [{ id: 2 }] } })
|
|
224
|
+
}
|
|
225
|
+
})
|