@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,312 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const Fastify = require('fastify')
5
- const WebSocket = require('ws')
6
- const { once } = require('events')
7
- const sqlGraphQL = require('..')
8
- const sqlMapper = require('@platformatic/sql-mapper')
9
- const sqlEvents = require('@platformatic/sql-events')
10
- const { clear, connInfo, isSQLite } = require('./helper')
11
- const stream = require('stream')
12
-
13
- async function createBasicPages (db, sql) {
14
- if (isSQLite) {
15
- await db.query(sql`CREATE TABLE pages (
16
- page_id INTEGER PRIMARY KEY,
17
- title VARCHAR(42)
18
- );`)
19
- } else {
20
- await db.query(sql`CREATE TABLE pages (
21
- page_id SERIAL PRIMARY KEY,
22
- title VARCHAR(42)
23
- );`)
24
- }
25
- }
26
-
27
- function createWebSocketClient (t, app) {
28
- const ws = new WebSocket('ws://localhost:' + (app.server.address()).port + '/graphql', 'graphql-ws')
29
- const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8', objectMode: true })
30
- t.teardown(client.destroy.bind(client))
31
- client.setEncoding('utf8')
32
- return { client, ws }
33
- }
34
-
35
- test('subscription - crud when there is a primary key to be camelised', async t => {
36
- const app = Fastify()
37
- t.teardown(() => app.close())
38
-
39
- app.register(sqlMapper, {
40
- ...connInfo,
41
- async onDatabaseLoad (db, sql) {
42
- await clear(db, sql)
43
- await createBasicPages(db, sql)
44
- }
45
- })
46
- app.register(sqlEvents)
47
- app.register(sqlGraphQL)
48
-
49
- await app.listen({ port: 0 })
50
-
51
- const { client } = createWebSocketClient(t, app)
52
-
53
- client.write(JSON.stringify({
54
- type: 'connection_init'
55
- }))
56
-
57
- {
58
- const query = `subscription {
59
- pageSaved {
60
- pageId
61
- title
62
- }
63
- }`
64
- client.write(JSON.stringify({
65
- pageId: 1,
66
- type: 'start',
67
- payload: {
68
- query
69
- }
70
- }))
71
- }
72
-
73
- {
74
- const query = `subscription {
75
- pageDeleted {
76
- pageId
77
- }
78
- }`
79
- client.write(JSON.stringify({
80
- pageId: 1,
81
- type: 'start',
82
- payload: {
83
- query
84
- }
85
- }))
86
- }
87
-
88
- {
89
- const [chunk] = await once(client, 'data')
90
- const data = JSON.parse(chunk)
91
- t.equal(data.type, 'connection_ack')
92
- }
93
-
94
- t.comment('sending mutation')
95
-
96
- await app.inject({
97
- method: 'POST',
98
- url: '/graphql',
99
- body: {
100
- query: `
101
- mutation {
102
- savePage(input: { title: "Hello World" }) {
103
- pageId
104
- }
105
- }
106
- `
107
- }
108
- })
109
-
110
- t.comment('mutation sent')
111
-
112
- {
113
- const [chunk] = await once(client, 'data')
114
- const data = JSON.parse(chunk)
115
- t.same(data, {
116
- type: 'data',
117
- payload: {
118
- data: {
119
- pageSaved: {
120
- pageId: '1',
121
- title: 'Hello World'
122
- }
123
- }
124
- }
125
- })
126
- }
127
-
128
- t.comment('updating entity')
129
-
130
- await app.inject({
131
- method: 'POST',
132
- url: '/graphql',
133
- body: {
134
- query: `
135
- mutation {
136
- savePage(input: { pageId: 1, title: "Il libraccio" }) {
137
- pageId
138
- }
139
- }
140
- `
141
- }
142
- })
143
-
144
- t.comment('entity updated')
145
-
146
- {
147
- const [chunk] = await once(client, 'data')
148
- const data = JSON.parse(chunk)
149
- t.same(data, {
150
- type: 'data',
151
- payload: {
152
- data: {
153
- pageSaved: {
154
- pageId: '1',
155
- title: 'Il libraccio'
156
- }
157
- }
158
- }
159
- })
160
- }
161
-
162
- t.comment('deleting entity')
163
-
164
- await app.inject({
165
- method: 'POST',
166
- url: '/graphql',
167
- body: {
168
- query: `
169
- mutation {
170
- deletePages(where: { pageId: { eq: 1 } }) {
171
- pageId
172
- }
173
- }
174
- `
175
- }
176
- })
177
-
178
- t.comment('entity deleted')
179
-
180
- {
181
- const [chunk] = await once(client, 'data')
182
- const data = JSON.parse(chunk)
183
- t.same(data, {
184
- type: 'data',
185
- payload: {
186
- data: {
187
- pageDeleted: {
188
- pageId: '1'
189
- }
190
- }
191
- }
192
- })
193
- }
194
-
195
- t.comment('sending mutation')
196
-
197
- {
198
- const [
199
- received,
200
- stored
201
- ] = await Promise.all(([
202
- (async function () {
203
- const res = await app.inject({
204
- method: 'POST',
205
- url: '/graphql',
206
- body: {
207
- query: `
208
- mutation batch($inputs : [PageInput]!) {
209
- insertPages (inputs: $inputs) {
210
- pageId
211
- title
212
- }
213
- }
214
- `,
215
- variables: {
216
- inputs: [
217
- { title: 'Page 1' },
218
- { title: 'Page 2' },
219
- { title: 'Page 3' }
220
- ]
221
- }
222
- }
223
- })
224
- t.comment('mutation sent')
225
- const pages = res.json().data.insertPages
226
- t.comment(JSON.stringify(pages, null, 2))
227
- return pages
228
- })(),
229
- (async function () {
230
- const pages = []
231
- const second = new stream.PassThrough({ objectMode: true })
232
- client.pipe(second)
233
- for await (const chunk of second) {
234
- const data = JSON.parse(chunk)
235
- pages.push(data.payload.data.pageSaved)
236
- if (pages.length === 3) {
237
- break
238
- }
239
- }
240
- t.comment('received all pages', JSON.stringify(pages, null, 2))
241
- return pages
242
- })()
243
- ]))
244
-
245
- t.same(received, stored)
246
- }
247
- })
248
-
249
- test('subscription - crud with a primary key to be camelised, two schemas and a ignore', async t => {
250
- const app = Fastify()
251
- t.teardown(() => app.close())
252
-
253
- app.register(sqlMapper, {
254
- ...connInfo,
255
- async onDatabaseLoad (db, sql) {
256
- await clear(db, sql)
257
- await createBasicPages(db, sql)
258
-
259
- if (isSQLite) {
260
- await db.query(sql`CREATE TABLE categories (
261
- category_id INTEGER PRIMARY KEY,
262
- name VARCHAR(42)
263
- );`)
264
- } else {
265
- await db.query(sql`CREATE TABLE categories (
266
- category_id SERIAL PRIMARY KEY,
267
- name VARCHAR(42)
268
- );`)
269
- }
270
- }
271
- })
272
- app.register(sqlEvents)
273
- app.register(sqlGraphQL, {
274
- subscriptionIgnore: ['category']
275
- })
276
-
277
- await app.listen({ port: 0 })
278
-
279
- const { client } = createWebSocketClient(t, app)
280
- t.teardown(() => client.destroy())
281
-
282
- client.write(JSON.stringify({
283
- type: 'connection_init'
284
- }))
285
-
286
- {
287
- const [chunk] = await once(client, 'data')
288
- const data = JSON.parse(chunk)
289
- t.equal(data.type, 'connection_ack')
290
- }
291
-
292
- {
293
- const query = `subscription {
294
- categorySaved {
295
- categoryId
296
- }
297
- }`
298
- client.write(JSON.stringify({
299
- categoryId: 1,
300
- type: 'start',
301
- payload: {
302
- query
303
- }
304
- }))
305
- }
306
-
307
- {
308
- const [chunk] = await once(client, 'data')
309
- const data = JSON.parse(chunk)
310
- t.equal(data.payload, 'The subscription field "categorySaved" is not defined.')
311
- }
312
- })