@platformatic/sql-graphql 0.46.1 → 0.47.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.
@@ -1,397 +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, isMysql } = require('./helper')
8
-
9
- test('one-level order by', 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 pages (
20
- id INTEGER PRIMARY KEY,
21
- title VARCHAR(42),
22
- counter INTEGER
23
- );`)
24
- } else {
25
- await db.query(sql`CREATE TABLE pages (
26
- id SERIAL PRIMARY KEY,
27
- title VARCHAR(42),
28
- counter INTEGER
29
- );`)
30
- }
31
- }
32
- })
33
- app.register(sqlGraphQL)
34
- teardown(app.close.bind(app))
35
-
36
- await app.ready()
37
-
38
- {
39
- const res = await app.inject({
40
- method: 'POST',
41
- url: '/graphql',
42
- body: {
43
- query: `
44
- mutation batch($inputs : [PageInput]!) {
45
- insertPages (inputs: $inputs) {
46
- id
47
- title
48
- counter
49
- }
50
- }
51
- `,
52
- variables: {
53
- inputs: [
54
- { title: 'Page 1', counter: 3 },
55
- { title: 'Page 2', counter: 2 },
56
- { title: 'Page 3', counter: 1 }
57
- ]
58
- }
59
- }
60
- })
61
- equal(res.statusCode, 200, 'savePage status code')
62
- same(res.json(), {
63
- data: {
64
- insertPages: [
65
- { id: 1, title: 'Page 1', counter: 3 },
66
- { id: 2, title: 'Page 2', counter: 2 },
67
- { id: 3, title: 'Page 3', counter: 1 }
68
- ]
69
- }
70
- }, 'savePage response')
71
- }
72
-
73
- {
74
- const res = await app.inject({
75
- method: 'POST',
76
- url: '/graphql',
77
- body: {
78
- query: `
79
- query {
80
- pages (orderBy: { field: counter, direction: ASC }) {
81
- id
82
- title
83
- counter
84
- }
85
- }
86
- `
87
- }
88
- })
89
- equal(res.statusCode, 200, 'pages status code')
90
- same(res.json(), {
91
- data: {
92
- pages: [
93
- { id: 3, title: 'Page 3', counter: 1 },
94
- { id: 2, title: 'Page 2', counter: 2 },
95
- { id: 1, title: 'Page 1', counter: 3 }
96
- ]
97
- }
98
- }, 'pages response')
99
- }
100
-
101
- {
102
- const res = await app.inject({
103
- method: 'POST',
104
- url: '/graphql',
105
- body: {
106
- query: `
107
- query {
108
- pages (orderBy: { field: counter, direction: DESC }) {
109
- id
110
- title
111
- counter
112
- }
113
- }
114
- `
115
- }
116
- })
117
- equal(res.statusCode, 200, 'pages status code')
118
- same(res.json(), {
119
- data: {
120
- pages: [
121
- { id: 1, title: 'Page 1', counter: 3 },
122
- { id: 2, title: 'Page 2', counter: 2 },
123
- { id: 3, title: 'Page 3', counter: 1 }
124
- ]
125
- }
126
- }, 'pages response')
127
- }
128
-
129
- {
130
- const res = await app.inject({
131
- method: 'POST',
132
- url: '/graphql',
133
- body: {
134
- query: `
135
- query {
136
- pages (orderBy: { field: counter}) {
137
- id
138
- title
139
- counter
140
- }
141
- }
142
- `
143
- }
144
- })
145
- equal(res.statusCode, 400, 'pages status code')
146
- same(res.json(), {
147
- data: null,
148
- errors: [{
149
- message: 'Field "PageOrderByArguments.direction" of required type "OrderByDirection!" was not provided.',
150
- locations: [{
151
- line: 3,
152
- column: 29
153
- }
154
- ]
155
- }]
156
- })
157
- }
158
- })
159
-
160
- test('list order by', async ({ pass, teardown, same, equal }) => {
161
- const app = fastify()
162
- app.register(sqlMapper, {
163
- ...connInfo,
164
- async onDatabaseLoad (db, sql) {
165
- pass('onDatabaseLoad called')
166
-
167
- await clear(db, sql)
168
-
169
- if (isSQLite) {
170
- await db.query(sql`CREATE TABLE pages (
171
- id INTEGER PRIMARY KEY,
172
- counter INTEGER,
173
- counter2 INTEGER
174
- );`)
175
- } else {
176
- await db.query(sql`CREATE TABLE pages (
177
- id SERIAL PRIMARY KEY,
178
- counter INTEGER,
179
- counter2 INTEGER
180
- );`)
181
- }
182
- }
183
- })
184
- app.register(sqlGraphQL)
185
- teardown(app.close.bind(app))
186
-
187
- await app.ready()
188
-
189
- {
190
- const res = await app.inject({
191
- method: 'POST',
192
- url: '/graphql',
193
- body: {
194
- query: `
195
- mutation batch($inputs : [PageInput]!) {
196
- insertPages (inputs: $inputs) {
197
- id
198
- counter
199
- counter2
200
- }
201
- }
202
- `,
203
- variables: {
204
- inputs: [
205
- { counter: 3, counter2: 3 },
206
- { counter: 3, counter2: 2 },
207
- { counter: 1, counter2: 1 }
208
- ]
209
- }
210
- }
211
- })
212
- equal(res.statusCode, 200, 'savePage status code')
213
- same(res.json(), {
214
- data: {
215
- insertPages: [
216
- { id: 1, counter: 3, counter2: 3 },
217
- { id: 2, counter: 3, counter2: 2 },
218
- { id: 3, counter: 1, counter2: 1 }
219
- ]
220
- }
221
- }, 'savePage response')
222
- }
223
-
224
- {
225
- const res = await app.inject({
226
- method: 'POST',
227
- url: '/graphql',
228
- body: {
229
- query: `
230
- query {
231
- pages (orderBy: [{ field: counter, direction: ASC }, { field: counter2, direction: DESC }]) {
232
- id
233
- counter,
234
- counter2
235
- }
236
- }
237
- `
238
- }
239
- })
240
- equal(res.statusCode, 200, 'pages status code')
241
- same(res.json(), {
242
- data: {
243
- pages: [
244
- { id: 3, counter: 1, counter2: 1 },
245
- { id: 1, counter: 3, counter2: 3 },
246
- { id: 2, counter: 3, counter2: 2 }
247
- ]
248
- }
249
- }, 'pages response')
250
- }
251
- })
252
-
253
- test('nested order by', async ({ pass, teardown, same, equal }) => {
254
- const app = fastify()
255
- app.register(sqlMapper, {
256
- ...connInfo,
257
- async onDatabaseLoad (db, sql) {
258
- pass('onDatabaseLoad called')
259
-
260
- await clear(db, sql)
261
-
262
- if (isMysql) {
263
- await db.query(sql`
264
- CREATE TABLE categories (
265
- id SERIAL PRIMARY KEY,
266
- name VARCHAR(255)
267
- );
268
- CREATE TABLE pages (
269
- id SERIAL PRIMARY KEY,
270
- title VARCHAR(42),
271
- category_id BIGINT UNSIGNED,
272
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
273
- );
274
- `)
275
- } else if (isSQLite) {
276
- await db.query(sql`
277
- CREATE TABLE categories (
278
- id INTEGER PRIMARY KEY,
279
- name VARCHAR(42)
280
- );
281
- `)
282
- await db.query(sql`
283
- CREATE TABLE pages (
284
- id INTEGER PRIMARY KEY,
285
- title VARCHAR(42),
286
- category_id INTEGER REFERENCES categories(id)
287
- );
288
- `)
289
- } else {
290
- await db.query(sql`
291
- CREATE TABLE categories (
292
- id SERIAL PRIMARY KEY,
293
- name VARCHAR(42)
294
- );
295
- CREATE TABLE pages (
296
- id SERIAL PRIMARY KEY,
297
- title VARCHAR(42),
298
- category_id INTEGER REFERENCES categories(id)
299
- );
300
- `)
301
- }
302
- }
303
- })
304
- app.register(sqlGraphQL)
305
- teardown(app.close.bind(app))
306
-
307
- const categories = [{
308
- name: 'Pets'
309
- }, {
310
- name: 'Food'
311
- }]
312
-
313
- await app.inject({
314
- method: 'POST',
315
- url: '/graphql',
316
- body: {
317
- query: `
318
- mutation batch($inputs: [CategoryInput]!) {
319
- insertCategories(inputs: $inputs) {
320
- id
321
- name
322
- }
323
- }
324
- `,
325
- variables: {
326
- inputs: categories
327
- }
328
- }
329
- })
330
-
331
- const pages = [{
332
- title: 'foo',
333
- categoryId: 1
334
- }, {
335
- title: 'bar',
336
- categoryId: 1
337
- }]
338
-
339
- await app.inject({
340
- method: 'POST',
341
- url: '/graphql',
342
- body: {
343
- query: `
344
- mutation batch($inputs: [PageInput]!) {
345
- insertPages(inputs: $inputs) {
346
- id
347
- title
348
- }
349
- }
350
- `,
351
- variables: {
352
- inputs: pages
353
- }
354
- }
355
- })
356
-
357
- {
358
- const res = await app.inject({
359
- method: 'POST',
360
- url: '/graphql',
361
- body: {
362
- query: `
363
- query {
364
- categories {
365
- id
366
- name
367
- pages(orderBy: { field: title, direction: ASC }) {
368
- id
369
- title
370
- }
371
- }
372
- }
373
- `
374
- }
375
- })
376
- equal(res.statusCode, 200, 'categories.posts status code')
377
- same(res.json(), {
378
- data: {
379
- categories: [{
380
- id: 1,
381
- name: 'Pets',
382
- pages: [{
383
- id: 2,
384
- title: 'bar'
385
- }, {
386
- id: 1,
387
- title: 'foo'
388
- }]
389
- }, {
390
- id: 2,
391
- name: 'Food',
392
- pages: []
393
- }]
394
- }
395
- }, 'categories.posts response')
396
- }
397
- })