@platformatic/sql-graphql 0.0.23

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.
Files changed (54) hide show
  1. package/.nyc_output/0681b2b7-587a-40f2-8242-257bc4c72456.json +1 -0
  2. package/.nyc_output/191054d4-6a1e-4a1b-87bb-f801ed78a223.json +1 -0
  3. package/.nyc_output/22ead0f6-e5a6-4647-a2ea-db2f42498081.json +1 -0
  4. package/.nyc_output/4e847e26-05cd-4063-9277-48aff86d6822.json +1 -0
  5. package/.nyc_output/5acfd416-02d1-497b-8dbd-69c0af167c62.json +1 -0
  6. package/.nyc_output/5ec04f76-9bcb-475d-b79e-f9304cc0026c.json +1 -0
  7. package/.nyc_output/5f3bb47a-0d2d-440a-a466-8ffa9ab3efaa.json +1 -0
  8. package/.nyc_output/71c2bd84-9a72-4e86-83c6-d587ce525fe4.json +1 -0
  9. package/.nyc_output/74cc1dab-7479-48e7-80b0-c97351d8856e.json +1 -0
  10. package/.nyc_output/9f5d12ba-bc3d-461c-a0e2-2a95d2020c19.json +1 -0
  11. package/.nyc_output/ac343b71-6f76-421c-a287-46d19bb48f89.json +1 -0
  12. package/.nyc_output/c1c0eecb-63a0-4258-81b6-50e7488bd67c.json +1 -0
  13. package/.nyc_output/c201a1cd-3105-4d18-bbbb-8d7fa674d439.json +1 -0
  14. package/.nyc_output/d6da0270-e9ed-41e2-bbf3-9262d1df683c.json +1 -0
  15. package/.nyc_output/processinfo/0681b2b7-587a-40f2-8242-257bc4c72456.json +1 -0
  16. package/.nyc_output/processinfo/191054d4-6a1e-4a1b-87bb-f801ed78a223.json +1 -0
  17. package/.nyc_output/processinfo/22ead0f6-e5a6-4647-a2ea-db2f42498081.json +1 -0
  18. package/.nyc_output/processinfo/4e847e26-05cd-4063-9277-48aff86d6822.json +1 -0
  19. package/.nyc_output/processinfo/5acfd416-02d1-497b-8dbd-69c0af167c62.json +1 -0
  20. package/.nyc_output/processinfo/5ec04f76-9bcb-475d-b79e-f9304cc0026c.json +1 -0
  21. package/.nyc_output/processinfo/5f3bb47a-0d2d-440a-a466-8ffa9ab3efaa.json +1 -0
  22. package/.nyc_output/processinfo/71c2bd84-9a72-4e86-83c6-d587ce525fe4.json +1 -0
  23. package/.nyc_output/processinfo/74cc1dab-7479-48e7-80b0-c97351d8856e.json +1 -0
  24. package/.nyc_output/processinfo/9f5d12ba-bc3d-461c-a0e2-2a95d2020c19.json +1 -0
  25. package/.nyc_output/processinfo/ac343b71-6f76-421c-a287-46d19bb48f89.json +1 -0
  26. package/.nyc_output/processinfo/c1c0eecb-63a0-4258-81b6-50e7488bd67c.json +1 -0
  27. package/.nyc_output/processinfo/c201a1cd-3105-4d18-bbbb-8d7fa674d439.json +1 -0
  28. package/.nyc_output/processinfo/d6da0270-e9ed-41e2-bbf3-9262d1df683c.json +1 -0
  29. package/.nyc_output/processinfo/index.json +1 -0
  30. package/.taprc +1 -0
  31. package/LICENSE +201 -0
  32. package/NOTICE +13 -0
  33. package/README.md +13 -0
  34. package/index.d.ts +40 -0
  35. package/index.js +117 -0
  36. package/lib/entity-to-type.js +248 -0
  37. package/lib/relationship.js +67 -0
  38. package/lib/utils.js +77 -0
  39. package/package.json +44 -0
  40. package/test/datatypes.test.js +708 -0
  41. package/test/federation.test.js +312 -0
  42. package/test/helper.js +70 -0
  43. package/test/hooks.test.js +660 -0
  44. package/test/ignore.test.js +117 -0
  45. package/test/insert.test.js +181 -0
  46. package/test/inserted_updated_at.test.js +517 -0
  47. package/test/nested.test.js +862 -0
  48. package/test/no-modification-of-entity.test.js +113 -0
  49. package/test/order_by.test.js +397 -0
  50. package/test/relations.test.js +60 -0
  51. package/test/simple.test.js +708 -0
  52. package/test/sqlite.test.js +276 -0
  53. package/test/types/index.test-d.ts +33 -0
  54. package/test/where.test.js +814 -0
@@ -0,0 +1,276 @@
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
+
109
+ await db.query(sql`
110
+ CREATE TABLE pages (
111
+ id INTEGER PRIMARY KEY,
112
+ title TEXT,
113
+ body TEXT,
114
+ category_id INTEGER,
115
+ FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
116
+ );
117
+ );`)
118
+ }
119
+ })
120
+ app.register(sqlGraphQL)
121
+ teardown(app.close.bind(app))
122
+
123
+ {
124
+ const res = await app.inject({
125
+ method: 'POST',
126
+ url: '/graphql',
127
+ body: {
128
+ query: `
129
+ mutation {
130
+ saveCategory(input: { name: "pets" }) {
131
+ id
132
+ name
133
+ }
134
+ }
135
+ `
136
+ }
137
+ })
138
+ equal(res.statusCode, 200, 'saveCategory status code')
139
+ same(res.json(), {
140
+ data: {
141
+ saveCategory: {
142
+ id: 1,
143
+ name: 'pets'
144
+ }
145
+ }
146
+ }, 'saveCategory response')
147
+ }
148
+
149
+ {
150
+ const res = await app.inject({
151
+ method: 'POST',
152
+ url: '/graphql',
153
+ body: {
154
+ query: `
155
+ mutation {
156
+ savePage(input: { title: "Dogs", body: "Dogs are cool", categoryId: "1" }) {
157
+ id
158
+ title
159
+ category {
160
+ id
161
+ name
162
+ }
163
+ }
164
+ }
165
+ `
166
+ }
167
+ })
168
+ equal(res.statusCode, 200, 'savePage status code')
169
+ same(res.json(), {
170
+ data: {
171
+ savePage: {
172
+ id: 1,
173
+ title: 'Dogs',
174
+ category: {
175
+ id: 1,
176
+ name: 'pets'
177
+ }
178
+ }
179
+ }
180
+ }, 'savePage response')
181
+ }
182
+
183
+ {
184
+ const res = await app.inject({
185
+ method: 'POST',
186
+ url: '/graphql',
187
+ body: {
188
+ query: `
189
+ query {
190
+ getPageById (id: "1") {
191
+ id
192
+ title
193
+ category {
194
+ id
195
+ name
196
+ }
197
+ }
198
+ }
199
+ `
200
+ }
201
+ })
202
+ equal(res.statusCode, 200, 'getPageById status code')
203
+ same(res.json(), {
204
+ data: {
205
+ getPageById: {
206
+ id: 1,
207
+ title: 'Dogs',
208
+ category: {
209
+ id: 1,
210
+ name: 'pets'
211
+ }
212
+ }
213
+ }
214
+ }, 'saveCategory response')
215
+ }
216
+
217
+ {
218
+ const res = await app.inject({
219
+ method: 'POST',
220
+ url: '/graphql',
221
+ body: {
222
+ query: `
223
+ query allPages {
224
+ pages {
225
+ title
226
+ category {
227
+ name
228
+ }
229
+ }
230
+ }
231
+ `
232
+ }
233
+ })
234
+ equal(res.statusCode, 200, 'pages status code')
235
+ same(res.json(), {
236
+ data: {
237
+ pages: [{
238
+ title: 'Dogs',
239
+ category: {
240
+ name: 'pets'
241
+ }
242
+ }]
243
+ }
244
+ }, 'pages response')
245
+ }
246
+
247
+ {
248
+ const res = await app.inject({
249
+ method: 'POST',
250
+ url: '/graphql',
251
+ body: {
252
+ query: `
253
+ query allCategories {
254
+ categories {
255
+ name
256
+ pages {
257
+ title
258
+ }
259
+ }
260
+ }
261
+ `
262
+ }
263
+ })
264
+ equal(res.statusCode, 200, 'categories status code')
265
+ same(res.json(), {
266
+ data: {
267
+ categories: [{
268
+ name: 'pets',
269
+ pages: [{
270
+ title: 'Dogs'
271
+ }]
272
+ }]
273
+ }
274
+ }, 'categories response')
275
+ }
276
+ })
@@ -0,0 +1,33 @@
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
+ autoTimestamp: true,
9
+ federationMetadata: true,
10
+ resolvers: {
11
+ Mutation: {
12
+ savePage: false,
13
+ deletePages: false,
14
+ insertPages: false
15
+ },
16
+ Query: {
17
+ async getCategory (root, args, context, info) {
18
+ return { id: 1, name: 'Hello' }
19
+ }
20
+ },
21
+ },
22
+ schema: `
23
+ extend type Query {
24
+ add(a: Int!, b: Int!): Int
25
+ }
26
+ `
27
+ }
28
+
29
+ const instance: FastifyInstance = fastify()
30
+ instance.register(plugin, pluginOptions)
31
+ instance.register(async (instance) => {
32
+ expectType<MercuriusPlugin>(instance.graphql)
33
+ })