@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,47 +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, isPg } = require('./helper')
8
-
9
- test('should correctly get the special characters', { skip: isSQLite }, async ({ pass, teardown, same }) => {
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 (isPg) {
19
- await db.query(sql`
20
- CREATE TYPE custom_enum AS ENUM ('application/pdf', 'image/jpeg', 'image/png');
21
- CREATE TABLE enum_tests (
22
- id INTEGER NOT NULL,
23
- test_enum custom_enum,
24
- PRIMARY KEY (id)
25
- );`)
26
- } else {
27
- await db.query(sql`
28
- CREATE TABLE enum_tests (
29
- id INTEGER NOT NULL,
30
- test_enum ENUM ('application/pdf', 'image/jpeg', 'image/png'),
31
- PRIMARY KEY (id)
32
- );`)
33
- }
34
- }
35
- })
36
-
37
- app.register(sqlGraphQL)
38
- teardown(app.close.bind(app))
39
-
40
- try {
41
- await app.ready()
42
- same(true, true, 'Special characters are properly configured')
43
- } catch (err) {
44
- console.log(err)
45
- same(true, false, 'Previous call should never fail')
46
- }
47
- })
@@ -1,274 +0,0 @@
1
- 'use strict'
2
-
3
- const fastify = require('fastify')
4
- const sqlGraphQL = require('..')
5
- const sqlMapper = require('@platformatic/sql-mapper')
6
- const { isSQLite } = require('./helper')
7
- const { test, skip } = require('tap')
8
- const { tmpdir } = require('os')
9
- const { join } = require('path')
10
- const { randomUUID } = require('crypto')
11
-
12
- if (!isSQLite) {
13
- skip('The db is not SQLite')
14
- process.exit(0)
15
- }
16
-
17
- test('store, close and load', async ({ pass, same, equal }) => {
18
- const file = join(tmpdir(), randomUUID())
19
- {
20
- const app = fastify()
21
- app.register(sqlMapper, {
22
- connectionString: `sqlite://${file}`,
23
- async onDatabaseLoad (db, sql) {
24
- pass('onDatabaseLoad called')
25
-
26
- await db.query(sql`CREATE TABLE pages (
27
- id INTEGER PRIMARY KEY,
28
- title VARCHAR(42)
29
- );`)
30
- }
31
- })
32
- app.register(sqlGraphQL)
33
-
34
- const res = await app.inject({
35
- method: 'POST',
36
- url: '/graphql',
37
- body: {
38
- query: `
39
- mutation {
40
- savePage(input: { title: "Hello" }) {
41
- id
42
- title
43
- }
44
- }
45
- `
46
- }
47
- })
48
- equal(res.statusCode, 200, 'savePage status code')
49
- same(res.json(), {
50
- data: {
51
- savePage: {
52
- id: 1,
53
- title: 'Hello'
54
- }
55
- }
56
- }, 'savePage response')
57
-
58
- await app.close()
59
- }
60
-
61
- {
62
- const app = fastify()
63
- app.register(sqlMapper, {
64
- connectionString: `sqlite://${file}`
65
- })
66
- app.register(sqlGraphQL)
67
- const res = await app.inject({
68
- method: 'POST',
69
- url: '/graphql',
70
- body: {
71
- query: `
72
- query {
73
- getPageById(id: 1) {
74
- id
75
- title
76
- }
77
- }
78
- `
79
- }
80
- })
81
- equal(res.statusCode, 200, 'pages status code')
82
- same(res.json(), {
83
- data: {
84
- getPageById: {
85
- id: 1,
86
- title: 'Hello'
87
- }
88
- }
89
- }, 'pages response')
90
- await app.close()
91
- }
92
- })
93
-
94
- test('demo', async ({ pass, same, equal, teardown }) => {
95
- const file = join(tmpdir(), randomUUID())
96
- const app = fastify()
97
- app.register(sqlMapper, {
98
- connectionString: `sqlite://${file}`,
99
- async onDatabaseLoad (db, sql) {
100
- pass('onDatabaseLoad called')
101
-
102
- await db.query(sql`
103
- CREATE TABLE categories (
104
- id INTEGER PRIMARY KEY,
105
- name TEXT NOT NULL
106
- );`)
107
-
108
- await db.query(sql`
109
- CREATE TABLE pages (
110
- id INTEGER PRIMARY KEY,
111
- title TEXT,
112
- body TEXT,
113
- category_id INTEGER,
114
- FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
115
- );`)
116
- }
117
- })
118
- app.register(sqlGraphQL)
119
- teardown(app.close.bind(app))
120
-
121
- {
122
- const res = await app.inject({
123
- method: 'POST',
124
- url: '/graphql',
125
- body: {
126
- query: `
127
- mutation {
128
- saveCategory(input: { name: "pets" }) {
129
- id
130
- name
131
- }
132
- }
133
- `
134
- }
135
- })
136
- equal(res.statusCode, 200, 'saveCategory status code')
137
- same(res.json(), {
138
- data: {
139
- saveCategory: {
140
- id: 1,
141
- name: 'pets'
142
- }
143
- }
144
- }, 'saveCategory response')
145
- }
146
-
147
- {
148
- const res = await app.inject({
149
- method: 'POST',
150
- url: '/graphql',
151
- body: {
152
- query: `
153
- mutation {
154
- savePage(input: { title: "Dogs", body: "Dogs are cool", categoryId: "1" }) {
155
- id
156
- title
157
- category {
158
- id
159
- name
160
- }
161
- }
162
- }
163
- `
164
- }
165
- })
166
- equal(res.statusCode, 200, 'savePage status code')
167
- same(res.json(), {
168
- data: {
169
- savePage: {
170
- id: 1,
171
- title: 'Dogs',
172
- category: {
173
- id: 1,
174
- name: 'pets'
175
- }
176
- }
177
- }
178
- }, 'savePage response')
179
- }
180
-
181
- {
182
- const res = await app.inject({
183
- method: 'POST',
184
- url: '/graphql',
185
- body: {
186
- query: `
187
- query {
188
- getPageById (id: "1") {
189
- id
190
- title
191
- category {
192
- id
193
- name
194
- }
195
- }
196
- }
197
- `
198
- }
199
- })
200
- equal(res.statusCode, 200, 'getPageById status code')
201
- same(res.json(), {
202
- data: {
203
- getPageById: {
204
- id: 1,
205
- title: 'Dogs',
206
- category: {
207
- id: 1,
208
- name: 'pets'
209
- }
210
- }
211
- }
212
- }, 'saveCategory response')
213
- }
214
-
215
- {
216
- const res = await app.inject({
217
- method: 'POST',
218
- url: '/graphql',
219
- body: {
220
- query: `
221
- query allPages {
222
- pages {
223
- title
224
- category {
225
- name
226
- }
227
- }
228
- }
229
- `
230
- }
231
- })
232
- equal(res.statusCode, 200, 'pages status code')
233
- same(res.json(), {
234
- data: {
235
- pages: [{
236
- title: 'Dogs',
237
- category: {
238
- name: 'pets'
239
- }
240
- }]
241
- }
242
- }, 'pages response')
243
- }
244
-
245
- {
246
- const res = await app.inject({
247
- method: 'POST',
248
- url: '/graphql',
249
- body: {
250
- query: `
251
- query allCategories {
252
- categories {
253
- name
254
- pages {
255
- title
256
- }
257
- }
258
- }
259
- `
260
- }
261
- })
262
- equal(res.statusCode, 200, 'categories status code')
263
- same(res.json(), {
264
- data: {
265
- categories: [{
266
- name: 'pets',
267
- pages: [{
268
- title: 'Dogs'
269
- }]
270
- }]
271
- }
272
- }, 'categories response')
273
- }
274
- })
@@ -1,336 +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
- const graphql = require('graphql')
13
-
14
- async function createBasicPages (db, sql) {
15
- if (isSQLite) {
16
- await db.query(sql`CREATE TABLE pages (
17
- id INTEGER PRIMARY KEY,
18
- title VARCHAR(42)
19
- );`)
20
- } else {
21
- await db.query(sql`CREATE TABLE pages (
22
- id SERIAL PRIMARY KEY,
23
- title VARCHAR(42)
24
- );`)
25
- }
26
- }
27
-
28
- function createWebSocketClient (t, app) {
29
- const ws = new WebSocket('ws://localhost:' + (app.server.address()).port + '/graphql', 'graphql-ws')
30
- const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8', objectMode: true })
31
- t.teardown(client.destroy.bind(client))
32
- client.setEncoding('utf8')
33
- return { client, ws }
34
- }
35
-
36
- test('subscription - crud', async t => {
37
- const app = Fastify()
38
- t.teardown(() => app.close())
39
-
40
- app.register(sqlMapper, {
41
- ...connInfo,
42
- async onDatabaseLoad (db, sql) {
43
- await clear(db, sql)
44
- await createBasicPages(db, sql)
45
- }
46
- })
47
- app.register(sqlEvents)
48
- app.register(sqlGraphQL)
49
-
50
- await app.listen({ port: 0 })
51
-
52
- const { client } = createWebSocketClient(t, app)
53
-
54
- client.write(JSON.stringify({
55
- type: 'connection_init'
56
- }))
57
-
58
- {
59
- const query = `subscription {
60
- pageSaved {
61
- id
62
- title
63
- }
64
- }`
65
- client.write(JSON.stringify({
66
- id: 1,
67
- type: 'start',
68
- payload: {
69
- query
70
- }
71
- }))
72
- }
73
-
74
- {
75
- const query = `subscription {
76
- pageDeleted {
77
- id
78
- }
79
- }`
80
- client.write(JSON.stringify({
81
- id: 1,
82
- type: 'start',
83
- payload: {
84
- query
85
- }
86
- }))
87
- }
88
-
89
- {
90
- const [chunk] = await once(client, 'data')
91
- const data = JSON.parse(chunk)
92
- t.equal(data.type, 'connection_ack')
93
- }
94
-
95
- t.comment('sending mutation')
96
-
97
- await app.inject({
98
- method: 'POST',
99
- url: '/graphql',
100
- body: {
101
- query: `
102
- mutation {
103
- savePage(input: { title: "Hello World" }) {
104
- id
105
- }
106
- }
107
- `
108
- }
109
- })
110
-
111
- t.comment('mutation sent')
112
-
113
- {
114
- const [chunk] = await once(client, 'data')
115
- const data = JSON.parse(chunk)
116
- t.same(data, {
117
- id: 1,
118
- type: 'data',
119
- payload: {
120
- data: {
121
- pageSaved: {
122
- id: '1',
123
- title: 'Hello World'
124
- }
125
- }
126
- }
127
- })
128
- }
129
-
130
- t.comment('updating entity')
131
-
132
- await app.inject({
133
- method: 'POST',
134
- url: '/graphql',
135
- body: {
136
- query: `
137
- mutation {
138
- savePage(input: { id: 1, title: "Harry Potter" }) {
139
- id
140
- }
141
- }
142
- `
143
- }
144
- })
145
-
146
- t.comment('entity updated')
147
-
148
- {
149
- const [chunk] = await once(client, 'data')
150
- const data = JSON.parse(chunk)
151
- t.same(data, {
152
- id: 1,
153
- type: 'data',
154
- payload: {
155
- data: {
156
- pageSaved: {
157
- id: '1',
158
- title: 'Harry Potter'
159
- }
160
- }
161
- }
162
- })
163
- }
164
-
165
- t.comment('deleting entity')
166
-
167
- await app.inject({
168
- method: 'POST',
169
- url: '/graphql',
170
- body: {
171
- query: `
172
- mutation {
173
- deletePages(where: { id: { eq: 1 } }) {
174
- id
175
- }
176
- }
177
- `
178
- }
179
- })
180
-
181
- t.comment('entity deleted')
182
-
183
- {
184
- const [chunk] = await once(client, 'data')
185
- const data = JSON.parse(chunk)
186
- t.same(data, {
187
- id: '1',
188
- type: 'data',
189
- payload: {
190
- data: {
191
- pageDeleted: {
192
- id: '1'
193
- }
194
- }
195
- }
196
- })
197
- }
198
-
199
- t.comment('sending mutation')
200
-
201
- {
202
- const [
203
- received,
204
- stored
205
- ] = await Promise.all(([
206
- (async function () {
207
- const res = await app.inject({
208
- method: 'POST',
209
- url: '/graphql',
210
- body: {
211
- query: `
212
- mutation batch($inputs : [PageInput]!) {
213
- insertPages (inputs: $inputs) {
214
- id
215
- title
216
- }
217
- }
218
- `,
219
- variables: {
220
- inputs: [
221
- { title: 'Page 1' },
222
- { title: 'Page 2' },
223
- { title: 'Page 3' }
224
- ]
225
- }
226
- }
227
- })
228
- t.comment('mutation sent')
229
- const pages = res.json().data.insertPages
230
- t.comment(JSON.stringify(pages, null, 2))
231
- return pages
232
- })(),
233
- (async function () {
234
- const pages = []
235
- const second = new stream.PassThrough({ objectMode: true })
236
- client.pipe(second)
237
- for await (const chunk of second) {
238
- const data = JSON.parse(chunk)
239
- pages.push(data.payload.data.pageSaved)
240
- if (pages.length === 3) {
241
- break
242
- }
243
- }
244
- t.comment('received all pages', JSON.stringify(pages, null, 2))
245
- return pages
246
- })()
247
- ]))
248
-
249
- t.same(received, stored)
250
- }
251
- })
252
-
253
- test('subscription - ignore', async t => {
254
- const app = Fastify()
255
- t.teardown(() => app.close())
256
-
257
- app.register(sqlMapper, {
258
- ...connInfo,
259
- async onDatabaseLoad (db, sql) {
260
- await clear(db, sql)
261
- await createBasicPages(db, sql)
262
- }
263
- })
264
- app.register(sqlEvents)
265
- app.register(sqlGraphQL, {
266
- subscriptionIgnore: ['page']
267
- })
268
-
269
- await app.ready()
270
- t.equal(graphql.printSchema(app.graphql.schema).indexOf('type Subscription'), -1)
271
- })
272
-
273
- test('subscription - crud with two schemas and a ignore', async t => {
274
- const app = Fastify()
275
- t.teardown(() => app.close())
276
-
277
- app.register(sqlMapper, {
278
- ...connInfo,
279
- async onDatabaseLoad (db, sql) {
280
- await clear(db, sql)
281
- await createBasicPages(db, sql)
282
-
283
- if (isSQLite) {
284
- await db.query(sql`CREATE TABLE categories (
285
- id INTEGER PRIMARY KEY,
286
- name VARCHAR(42)
287
- );`)
288
- } else {
289
- await db.query(sql`CREATE TABLE categories (
290
- id SERIAL PRIMARY KEY,
291
- name VARCHAR(42)
292
- );`)
293
- }
294
- }
295
- })
296
- app.register(sqlEvents)
297
- app.register(sqlGraphQL, {
298
- subscriptionIgnore: ['category']
299
- })
300
-
301
- await app.listen({ port: 0 })
302
-
303
- const { client } = createWebSocketClient(t, app)
304
- t.teardown(() => client.destroy())
305
-
306
- client.write(JSON.stringify({
307
- type: 'connection_init'
308
- }))
309
-
310
- {
311
- const [chunk] = await once(client, 'data')
312
- const data = JSON.parse(chunk)
313
- t.equal(data.type, 'connection_ack')
314
- }
315
-
316
- {
317
- const query = `subscription {
318
- categorySaved {
319
- id
320
- }
321
- }`
322
- client.write(JSON.stringify({
323
- id: 1,
324
- type: 'start',
325
- payload: {
326
- query
327
- }
328
- }))
329
- }
330
-
331
- {
332
- const [chunk] = await once(client, 'data')
333
- const data = JSON.parse(chunk)
334
- t.equal(data.payload, 'The subscription field "categorySaved" is not defined.')
335
- }
336
- })
@@ -1,15 +0,0 @@
1
- import { expectType } from 'tsd'
2
- import { FastifyError } from '@fastify/error'
3
- import {
4
- errors
5
- } from '../../index'
6
-
7
- // Errors
8
- type ErrorWithNoParams = () => FastifyError
9
- type ErrorWithOneParam = (param: string) => FastifyError
10
-
11
- expectType<ErrorWithNoParams>(errors.UnableToGenerateGraphQLEnumTypeError)
12
- expectType<ErrorWithOneParam>(errors.UnsupportedKindError)
13
- expectType<ErrorWithNoParams>(errors.ErrorPrintingGraphQLSchema)
14
-
15
-
@@ -1,32 +0,0 @@
1
- import { expectType } from 'tsd'
2
- import { fastify, FastifyInstance } from 'fastify'
3
- import { MercuriusPlugin } from 'mercurius'
4
- import plugin, { SQLGraphQLPluginOptions } from '../../index'
5
-
6
- const pluginOptions: SQLGraphQLPluginOptions = {
7
- graphiql: true,
8
- federationMetadata: true,
9
- resolvers: {
10
- Mutation: {
11
- savePage: false,
12
- deletePages: false,
13
- insertPages: false
14
- },
15
- Query: {
16
- async getCategory (root, args, context, info) {
17
- return { id: 1, name: 'Hello' }
18
- }
19
- },
20
- },
21
- schema: `
22
- extend type Query {
23
- add(a: Int!, b: Int!): Int
24
- }
25
- `
26
- }
27
-
28
- const instance: FastifyInstance = fastify()
29
- instance.register(plugin, pluginOptions)
30
- instance.register(async (instance) => {
31
- expectType<MercuriusPlugin>(instance.graphql)
32
- })