@platformatic/sql-graphql 0.45.1 → 0.46.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.
@@ -1,474 +0,0 @@
1
- 'use strict'
2
-
3
- const { clear, connInfo, isSQLite, isMysql, isPg } = require('./helper')
4
- const { test } = require('tap')
5
- const fastify = require('fastify')
6
- const sqlMapper = require('@platformatic/sql-mapper')
7
- const sqlEvents = require('@platformatic/sql-events')
8
- const sqlGraphQL = require('..')
9
-
10
- test('composite primary keys', async ({ equal, same, teardown, rejects }) => {
11
- /* https://github.com/platformatic/platformatic/issues/299 */
12
- async function onDatabaseLoad (db, sql) {
13
- await clear(db, sql)
14
-
15
- if (isSQLite) {
16
- await db.query(sql`CREATE TABLE pages (
17
- id INTEGER PRIMARY KEY,
18
- the_title VARCHAR(42)
19
- );`)
20
-
21
- await db.query(sql`CREATE TABLE users (
22
- id INTEGER PRIMARY KEY,
23
- username VARCHAR(255) NOT NULL
24
- );`)
25
-
26
- await db.query(sql`CREATE TABLE editors (
27
- page_id INTEGER NOT NULL,
28
- user_id INTEGER NOT NULL,
29
- role VARCHAR(255) NOT NULL,
30
- CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
31
- CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
32
- PRIMARY KEY (page_id, user_id)
33
- );`)
34
- } else if (isPg) {
35
- await db.query(sql`CREATE TABLE pages (
36
- id SERIAL PRIMARY KEY,
37
- the_title VARCHAR(255) NOT NULL
38
- );`)
39
-
40
- await db.query(sql`CREATE TABLE users (
41
- id SERIAL PRIMARY KEY,
42
- username VARCHAR(255) NOT NULL
43
- );`)
44
-
45
- await db.query(sql`CREATE TABLE editors (
46
- page_id INTEGER NOT NULL,
47
- user_id INTEGER NOT NULL,
48
- role VARCHAR(255) NOT NULL,
49
- CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
50
- CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
51
- PRIMARY KEY (page_id, user_id)
52
- );`)
53
- } else if (isMysql) {
54
- await db.query(sql`CREATE TABLE pages (
55
- id INTEGER PRIMARY KEY AUTO_INCREMENT,
56
- the_title VARCHAR(255) NOT NULL
57
- );`)
58
-
59
- await db.query(sql`CREATE TABLE users (
60
- id INTEGER PRIMARY KEY AUTO_INCREMENT,
61
- username VARCHAR(255) NOT NULL
62
- );`)
63
-
64
- await db.query(sql`CREATE TABLE editors (
65
- page_id INTEGER NOT NULL,
66
- user_id INTEGER NOT NULL,
67
- role VARCHAR(255) NOT NULL,
68
- CONSTRAINT \`fk_editor_pages\` FOREIGN KEY (page_id) REFERENCES pages (id) ON DELETE CASCADE ON UPDATE RESTRICT,
69
- CONSTRAINT \`fk_editor_users\` FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,
70
- PRIMARY KEY (page_id, user_id)
71
- );`)
72
- }
73
- }
74
-
75
- const app = fastify()
76
- app.register(sqlMapper, {
77
- ...connInfo,
78
- onDatabaseLoad
79
- })
80
- app.register(sqlEvents) // needed as if it's present it might throw
81
- app.register(sqlGraphQL)
82
- teardown(app.close.bind(app))
83
-
84
- await app.ready()
85
-
86
- {
87
- const res = await app.inject({
88
- method: 'POST',
89
- url: '/graphql',
90
- body: {
91
- query: `
92
- mutation {
93
- savePage(input: { theTitle: "foobar" }) {
94
- id
95
- theTitle
96
- }
97
- }
98
- `
99
- }
100
- })
101
- equal(res.statusCode, 200, 'savePage status code')
102
- same(res.json(), {
103
- data: {
104
- savePage: {
105
- id: 1,
106
- theTitle: 'foobar'
107
- }
108
- }
109
- }, 'savePage response')
110
- }
111
-
112
- {
113
- const res = await app.inject({
114
- method: 'POST',
115
- url: '/graphql',
116
- body: {
117
- query: `
118
- mutation {
119
- saveUser(input: { username: "mcollina" }) {
120
- id
121
- username
122
- }
123
- }
124
- `
125
- }
126
- })
127
- equal(res.statusCode, 200, 'saveUser status code')
128
- same(res.json(), {
129
- data: {
130
- saveUser: {
131
- id: 1,
132
- username: 'mcollina'
133
- }
134
- }
135
- }, 'saveUser response')
136
- }
137
-
138
- {
139
- const res = await app.inject({
140
- method: 'POST',
141
- url: '/graphql',
142
- body: {
143
- query: `
144
- mutation {
145
- saveUser(input: { username: "lucamaraschi" }) {
146
- id
147
- username
148
- }
149
- }
150
- `
151
- }
152
- })
153
- equal(res.statusCode, 200, 'saveUser status code')
154
- same(res.json(), {
155
- data: {
156
- saveUser: {
157
- id: 2,
158
- username: 'lucamaraschi'
159
- }
160
- }
161
- }, 'saveUser response')
162
- }
163
-
164
- {
165
- const res = await app.inject({
166
- method: 'POST',
167
- url: '/graphql',
168
- body: {
169
- query: `
170
- mutation {
171
- saveEditor(input: { userId: "1", pageId: "1", role: "admin" }) {
172
- user {
173
- id
174
- username
175
- }
176
- page {
177
- id
178
- theTitle
179
- }
180
- role
181
- }
182
- }
183
- `
184
- }
185
- })
186
- equal(res.statusCode, 200, 'saveEditor status code')
187
- same(res.json(), {
188
- data: {
189
- saveEditor: {
190
- user: {
191
- id: 1,
192
- username: 'mcollina'
193
- },
194
- page: {
195
- id: 1,
196
- theTitle: 'foobar'
197
- },
198
- role: 'admin'
199
- }
200
- }
201
- }, 'saveEditor response')
202
- }
203
-
204
- {
205
- const res = await app.inject({
206
- method: 'POST',
207
- url: '/graphql',
208
- body: {
209
- query: `
210
- mutation {
211
- saveEditor(input: { userId: "2", pageId: "1", role: "author" }) {
212
- user {
213
- id
214
- username
215
- }
216
- page {
217
- id
218
- theTitle
219
- }
220
- role
221
- }
222
- }
223
- `
224
- }
225
- })
226
- equal(res.statusCode, 200, 'saveEditor status code')
227
- same(res.json(), {
228
- data: {
229
- saveEditor: {
230
- user: {
231
- id: 2,
232
- username: 'lucamaraschi'
233
- },
234
- page: {
235
- id: 1,
236
- theTitle: 'foobar'
237
- },
238
- role: 'author'
239
- }
240
- }
241
- }, 'saveEditor response')
242
- }
243
-
244
- {
245
- const res = await app.inject({
246
- method: 'POST',
247
- url: '/graphql',
248
- body: {
249
- query: `
250
- mutation {
251
- saveEditor(input: { userId: "1", pageId: "1", role: "captain" }) {
252
- user {
253
- id
254
- username
255
- }
256
- page {
257
- id
258
- theTitle
259
- }
260
- role
261
- }
262
- }
263
- `
264
- }
265
- })
266
- equal(res.statusCode, 200, 'saveEditor status code')
267
- same(res.json(), {
268
- data: {
269
- saveEditor: {
270
- user: {
271
- id: 1,
272
- username: 'mcollina'
273
- },
274
- page: {
275
- id: 1,
276
- theTitle: 'foobar'
277
- },
278
- role: 'captain'
279
- }
280
- }
281
- }, 'saveEditor response')
282
- }
283
-
284
- {
285
- const res = await app.inject({
286
- method: 'post',
287
- url: '/graphql',
288
- body: {
289
- query: `
290
- query {
291
- editors(orderBy: { field: role, direction: DESC }) {
292
- user {
293
- id
294
- username
295
- }
296
- page {
297
- id
298
- theTitle
299
- }
300
- role
301
- }
302
- }
303
- `
304
- }
305
- })
306
- equal(res.statusCode, 200, 'editors status code')
307
- same(res.json(), {
308
- data: {
309
- editors: [{
310
- user: {
311
- id: '1',
312
- username: 'mcollina'
313
- },
314
- page: {
315
- id: '1',
316
- theTitle: 'foobar'
317
- },
318
- role: 'captain'
319
- }, {
320
- user: {
321
- id: '2',
322
- username: 'lucamaraschi'
323
- },
324
- page: {
325
- id: '1',
326
- theTitle: 'foobar'
327
- },
328
- role: 'author'
329
- }]
330
- }
331
- }, 'editor response')
332
- }
333
- })
334
-
335
- test('composite primary keys with no foreign keys', async ({ equal, same, teardown, rejects }) => {
336
- /* https://github.com/platformatic/platformatic/issues/299 */
337
- async function onDatabaseLoad (db, sql) {
338
- await clear(db, sql)
339
-
340
- await db.query(sql`CREATE TABLE editors (
341
- page_id INTEGER NOT NULL,
342
- user_id INTEGER NOT NULL,
343
- role VARCHAR(255) NOT NULL,
344
- PRIMARY KEY (page_id, user_id)
345
- );`)
346
- }
347
-
348
- const app = fastify()
349
- app.register(sqlMapper, {
350
- ...connInfo,
351
- onDatabaseLoad
352
- })
353
- app.register(sqlEvents) // needed as if it's present it will throw
354
- app.register(sqlGraphQL)
355
- teardown(app.close.bind(app))
356
-
357
- await app.ready()
358
-
359
- {
360
- const res = await app.inject({
361
- method: 'POST',
362
- url: '/graphql',
363
- body: {
364
- query: `
365
- mutation {
366
- saveEditor(input: { userId: "1", pageId: "1", role: "admin" }) {
367
- userId
368
- pageId
369
- role
370
- }
371
- }
372
- `
373
- }
374
- })
375
- equal(res.statusCode, 200, 'saveEditor status code')
376
- same(res.json(), {
377
- data: {
378
- saveEditor: {
379
- userId: '1',
380
- pageId: '1',
381
- role: 'admin'
382
- }
383
- }
384
- }, 'saveEditor response')
385
- }
386
-
387
- {
388
- const res = await app.inject({
389
- method: 'POST',
390
- url: '/graphql',
391
- body: {
392
- query: `
393
- mutation {
394
- saveEditor(input: { userId: "2", pageId: "1", role: "author" }) {
395
- userId
396
- pageId
397
- role
398
- }
399
- }
400
- `
401
- }
402
- })
403
- equal(res.statusCode, 200, 'saveEditor status code')
404
- same(res.json(), {
405
- data: {
406
- saveEditor: {
407
- userId: '2',
408
- pageId: '1',
409
- role: 'author'
410
- }
411
- }
412
- }, 'saveEditor response')
413
- }
414
-
415
- {
416
- const res = await app.inject({
417
- method: 'POST',
418
- url: '/graphql',
419
- body: {
420
- query: `
421
- mutation {
422
- saveEditor(input: { userId: "1", pageId: "1", role: "captain" }) {
423
- userId
424
- pageId
425
- role
426
- }
427
- }
428
- `
429
- }
430
- })
431
- equal(res.statusCode, 200, 'saveEditor status code')
432
- same(res.json(), {
433
- data: {
434
- saveEditor: {
435
- userId: 1,
436
- pageId: 1,
437
- role: 'captain'
438
- }
439
- }
440
- }, 'saveEditor response')
441
- }
442
-
443
- {
444
- const res = await app.inject({
445
- method: 'post',
446
- url: '/graphql',
447
- body: {
448
- query: `
449
- query {
450
- editors(orderBy: { field: role, direction: DESC }) {
451
- userId
452
- pageId
453
- role
454
- }
455
- }
456
- `
457
- }
458
- })
459
- equal(res.statusCode, 200, 'editors status code')
460
- same(res.json(), {
461
- data: {
462
- editors: [{
463
- userId: '1',
464
- pageId: '1',
465
- role: 'captain'
466
- }, {
467
- userId: '2',
468
- pageId: '1',
469
- role: 'author'
470
- }]
471
- }
472
- }, 'editor response')
473
- }
474
- })
@@ -1,126 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const sqlGraphQL = require('..')
5
- const sqlMapper = require('@platformatic/sql-mapper')
6
- const fastify = require('fastify')
7
- const { clear, connInfo, isSQLite } = require('./helper')
8
-
9
- test('count', async ({ pass, teardown, same, equal }) => {
10
- const app = fastify()
11
- app.register(sqlMapper, {
12
- ...connInfo,
13
- async onDatabaseLoad (db, sql) {
14
- pass('onDatabaseLoad called')
15
-
16
- await clear(db, sql)
17
-
18
- if (isSQLite) {
19
- await db.query(sql`CREATE TABLE posts (
20
- id INTEGER PRIMARY KEY,
21
- title VARCHAR(42),
22
- long_text TEXT,
23
- counter INTEGER
24
- );`)
25
- } else {
26
- await db.query(sql`CREATE TABLE posts (
27
- id SERIAL PRIMARY KEY,
28
- title VARCHAR(42),
29
- long_text TEXT,
30
- counter INTEGER
31
- );`)
32
- }
33
- }
34
- })
35
- app.register(sqlGraphQL)
36
- teardown(app.close.bind(app))
37
-
38
- await app.ready()
39
-
40
- const posts = [{
41
- title: 'Dog',
42
- longText: 'Foo',
43
- counter: 10
44
- }, {
45
- title: 'Cat',
46
- longText: 'Bar',
47
- counter: 20
48
- }, {
49
- title: 'Mouse',
50
- longText: 'Baz',
51
- counter: 30
52
- }, {
53
- title: 'Duck',
54
- longText: 'A duck tale',
55
- counter: 40
56
- }]
57
-
58
- {
59
- const res = await app.inject({
60
- method: 'POST',
61
- url: '/graphql',
62
- body: {
63
- query: `
64
- mutation batch($inputs : [PostInput]!) {
65
- insertPosts(inputs: $inputs) {
66
- id
67
- title
68
- }
69
- }
70
- `,
71
- variables: {
72
- inputs: posts
73
- }
74
- }
75
- })
76
- equal(res.statusCode, 200, 'posts status code')
77
- }
78
-
79
- {
80
- const res = await app.inject({
81
- method: 'POST',
82
- url: '/graphql',
83
- body: {
84
- query: `
85
- query {
86
- countPosts {
87
- total
88
- }
89
- }
90
- `
91
- }
92
- })
93
- equal(res.statusCode, 200, 'posts status code')
94
- same(res.json(), {
95
- data: {
96
- countPosts: {
97
- total: 4
98
- }
99
- }
100
- }, 'posts response')
101
- }
102
-
103
- {
104
- const res = await app.inject({
105
- method: 'POST',
106
- url: '/graphql',
107
- body: {
108
- query: `
109
- query {
110
- countPosts(where: { counter: { gt: 20 } }) {
111
- total
112
- }
113
- }
114
- `
115
- }
116
- })
117
- equal(res.statusCode, 200, 'posts status code')
118
- same(res.json(), {
119
- data: {
120
- countPosts: {
121
- total: 2
122
- }
123
- }
124
- }, 'posts response')
125
- }
126
- })