@platformatic/sql-graphql 0.46.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.
package/test/enum.test.js DELETED
@@ -1,225 +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 properly setup the enum types', { 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('1', '2', '3');
21
-
22
- CREATE TABLE enum_tests (
23
- id INTEGER NOT NULL,
24
- test_enum custom_enum,
25
- PRIMARY KEY (id)
26
- );`)
27
- } else {
28
- await db.query(sql`
29
- CREATE TABLE enum_tests (
30
- id INTEGER NOT NULL,
31
- test_enum ENUM ('1', '2', '3') DEFAULT NULL,
32
- PRIMARY KEY (id)
33
- );`)
34
- }
35
- }
36
- })
37
-
38
- app.register(sqlGraphQL)
39
- teardown(app.close.bind(app))
40
-
41
- try {
42
- await app.ready()
43
- same(true, true, 'Enum values are properly interpreted')
44
- } catch (err) {
45
- console.log(err)
46
- same(true, false, 'Previous call should never fail')
47
- }
48
- })
49
-
50
- test('should not fail if tables have duplicate enum names', { skip: isSQLite }, async ({ pass, teardown, same }) => {
51
- const app = fastify()
52
- app.register(sqlMapper, {
53
- ...connInfo,
54
- async onDatabaseLoad (db, sql) {
55
- pass('onDatabaseLoad called')
56
-
57
- await clear(db, sql)
58
-
59
- if (isPg) {
60
- await db.query(sql`
61
- CREATE TYPE custom_enum AS ENUM('1', '2', '3');
62
- CREATE TYPE simple_enum AS ENUM('4', '5', '6');
63
-
64
- CREATE TABLE enum_tests (
65
- id INTEGER NOT NULL,
66
- test_enum custom_enum,
67
- PRIMARY KEY (id)
68
- );
69
- CREATE TABLE simple_types (
70
- pk INTEGER NOT NULL,
71
- test_enum simple_enum,
72
- PRIMARY KEY (pk)
73
- );`)
74
- } else {
75
- await db.query(sql`
76
- CREATE TABLE enum_tests (
77
- id INTEGER NOT NULL,
78
- test_enum ENUM ('1', '2', '3') DEFAULT NULL,
79
- PRIMARY KEY (id)
80
- );
81
- CREATE TABLE simple_types (
82
- pk INTEGER NOT NULL,
83
- test_enum ENUM ('4', '5', '6') DEFAULT NULL,
84
- PRIMARY KEY (pk)
85
- );`)
86
- }
87
- }
88
- })
89
-
90
- app.register(sqlGraphQL)
91
- teardown(app.close.bind(app))
92
-
93
- try {
94
- await app.ready()
95
- same(true, true, 'test_enum is used twice but app not throws')
96
- } catch (err) {
97
- console.log(err)
98
- same(true, false, 'Previous call should never fail')
99
- }
100
- })
101
-
102
- test('should not fail if tables have enum with special characters', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
103
- const app = fastify()
104
- app.register(sqlMapper, {
105
- ...connInfo,
106
- async onDatabaseLoad (db, sql) {
107
- pass('onDatabaseLoad called')
108
-
109
- await clear(db, sql)
110
-
111
- if (isPg) {
112
- await db.query(sql`
113
- CREATE TYPE custom_enum AS ENUM('.', ',');
114
-
115
- CREATE TABLE enum_tests (
116
- id INTEGER NOT NULL,
117
- test_enum custom_enum,
118
- PRIMARY KEY (id)
119
- );
120
- `)
121
- } else {
122
- await db.query(sql`
123
- CREATE TABLE enum_tests (
124
- id INTEGER NOT NULL,
125
- test_enum ENUM ('.', ',') DEFAULT NULL,
126
- PRIMARY KEY (id)
127
- );
128
- `)
129
- }
130
- }
131
- })
132
-
133
- app.register(sqlGraphQL)
134
- teardown(app.close.bind(app))
135
-
136
- try {
137
- await app.ready()
138
- same(true, true, 'custom_enum have special chars, but app does not throws')
139
- } catch (err) {
140
- console.log(err)
141
- same(true, false, 'Previous call should never fail')
142
- }
143
-
144
- {
145
- const res = await app.inject({
146
- method: 'POST',
147
- url: '/graphql',
148
- body: {
149
- query: `
150
- mutation ($input: EnumTestInput!) {
151
- saveEnumTest(input: $input) {
152
- id
153
- }
154
- }
155
- `,
156
- variables: { input: { id: 1 } }
157
- }
158
- })
159
- equal(res.statusCode, 200)
160
- }
161
-
162
- {
163
- const res = await app.inject({
164
- method: 'POST',
165
- url: '/graphql',
166
- body: {
167
- query: `
168
- mutation ($input: EnumTestInput!) {
169
- saveEnumTest(input: $input) {
170
- id
171
- }
172
- }
173
- `,
174
- variables: { input: { id: 2 } }
175
- }
176
- })
177
- equal(res.statusCode, 200)
178
- }
179
-
180
- {
181
- const res = await app.inject({
182
- method: 'POST',
183
- url: '/graphql',
184
- body: {
185
- query: `
186
- query { enumTests { id } }
187
- `
188
- }
189
- })
190
- equal(res.statusCode, 200)
191
- same(res.json(), { data: { enumTests: [{ id: 1 }, { id: 2 }] } })
192
- }
193
-
194
- {
195
- const res = await app.inject({
196
- method: 'POST',
197
- url: '/graphql',
198
- body: {
199
- query: `
200
- mutation ($input: EnumTestWhereArgumentsid!) {
201
- deleteEnumTests(where: {id: $input}) {
202
- id
203
- }
204
- }
205
- `,
206
- variables: { input: { eq: 1 } }
207
- }
208
- })
209
- equal(res.statusCode, 200)
210
- }
211
-
212
- {
213
- const res = await app.inject({
214
- method: 'POST',
215
- url: '/graphql',
216
- body: {
217
- query: `
218
- query { enumTests { id } }
219
- `
220
- }
221
- })
222
- equal(res.statusCode, 200)
223
- same(res.json(), { data: { enumTests: [{ id: 2 }] } })
224
- }
225
- })
@@ -1,312 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const Fastify = require('fastify')
5
- const Gateway = require('@mercuriusjs/gateway')
6
- const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
7
- const sqlGraphQL = require('..')
8
- const sqlMapper = require('@platformatic/sql-mapper')
9
- const { clear, connInfo, isSQLite } = require('./helper')
10
-
11
- async function createTestService (t, schema, resolvers = {}) {
12
- const service = Fastify({ logger: { level: 'error' } })
13
- service.register(mercuriusFederationPlugin, {
14
- schema,
15
- resolvers
16
- })
17
- await service.listen({ port: 0 })
18
- return [service, service.server.address().port]
19
- }
20
-
21
- const categories = {
22
- c1: {
23
- id: 'c1',
24
- name: 'Food'
25
- },
26
- c2: {
27
- id: 'c2',
28
- name: 'Places'
29
- }
30
- }
31
-
32
- // This works because the database is reset between tests
33
- const postCategory = {
34
- 1: 'c1',
35
- 2: 'c2',
36
- 3: 'c1',
37
- 4: 'c1'
38
- }
39
-
40
- const categoryPost = Object.keys(postCategory).reduce((acc, key) => {
41
- acc[postCategory[key]] = acc[postCategory[key]] || []
42
- acc[postCategory[key]].push(key)
43
- return acc
44
- }, {})
45
-
46
- const posts = [{
47
- title: 'Post 1',
48
- longText: 'This is a long text 1'
49
- }, {
50
- title: 'Post 2',
51
- longText: 'This is a long text 2'
52
- }, {
53
- title: 'Post 3',
54
- longText: 'This is a long text 3'
55
- }, {
56
- title: 'Post 4',
57
- longText: 'This is a long text 4'
58
- }]
59
-
60
- async function createTestGatewayServer (t, cacheOpts) {
61
- const categoryServiceSchema = `
62
- type Query @extends {
63
- categories: [Category]
64
- }
65
-
66
- type Category @key(fields: "id") {
67
- id: ID!
68
- name: String
69
- posts: [Post]
70
- }
71
-
72
- type Post @key(fields: "id") @extends {
73
- id: ID! @external
74
- category: Category
75
- }
76
- `
77
- const categoryServiceResolvers = {
78
- Query: {
79
- categories: (root, args, context, info) => {
80
- t.pass('Query.categories resolved')
81
- return Object.values(categories)
82
- }
83
- },
84
- Category: {
85
- posts: (root, args, context, info) => {
86
- t.pass('Category.posts resolved')
87
- return categoryPost[root.id]
88
- ? categoryPost[root.id].map(id => ({ id }))
89
- : []
90
- },
91
- __resolveReference: (category, args, context, info) => {
92
- t.pass('Category.__resolveReference')
93
- return categories[category.id]
94
- }
95
- },
96
- Post: {
97
- category: (root, args, context, info) => {
98
- t.pass('Post.category resolved')
99
- return categories[postCategory[root.id]]
100
- }
101
- }
102
- }
103
- const [categoryService, categoryServicePort] = await createTestService(t, categoryServiceSchema, categoryServiceResolvers)
104
-
105
- const postService = Fastify()
106
- postService.register(sqlMapper, {
107
- ...connInfo,
108
- async onDatabaseLoad (db, sql) {
109
- t.pass('onDatabaseLoad called')
110
-
111
- await clear(db, sql)
112
-
113
- if (isSQLite) {
114
- await db.query(sql`CREATE TABLE posts (
115
- id INTEGER PRIMARY KEY,
116
- title VARCHAR(42),
117
- long_text TEXT
118
- );`)
119
- } else {
120
- await db.query(sql`CREATE TABLE posts (
121
- id SERIAL PRIMARY KEY,
122
- title VARCHAR(42),
123
- long_text TEXT
124
- );`)
125
- }
126
- }
127
- })
128
- postService.register(sqlGraphQL, {
129
- federationMetadata: true
130
- })
131
- await postService.listen({ port: 0 })
132
- const postServicePort = postService.server.address().port
133
-
134
- await postService.inject({
135
- method: 'POST',
136
- url: '/graphql',
137
- body: {
138
- query: `
139
- mutation batch($inputs : [PostInput]!) {
140
- insertPosts(inputs: $inputs) {
141
- id
142
- title
143
- }
144
- }
145
- `,
146
- variables: {
147
- inputs: posts
148
- }
149
- }
150
- })
151
-
152
- const gateway = Fastify()
153
- t.teardown(async () => {
154
- await gateway.close()
155
- await categoryService.close()
156
- await postService.close()
157
- })
158
- gateway.register(Gateway, {
159
- gateway: {
160
- services: [{
161
- name: 'category',
162
- url: `http://localhost:${categoryServicePort}/graphql`
163
- }, {
164
- name: 'post',
165
- url: `http://localhost:${postServicePort}/graphql`
166
- }]
167
- }
168
- })
169
-
170
- return gateway
171
- }
172
-
173
- test('extendable', async (t) => {
174
- const app = await createTestGatewayServer(t)
175
-
176
- const query = `query {
177
- categories {
178
- id
179
- name
180
- posts {
181
- id
182
- title
183
- longText
184
- category {
185
- id
186
- name
187
- }
188
- }
189
- }
190
- posts {
191
- id
192
- title
193
- category {
194
- id
195
- name
196
- }
197
- }
198
- }`
199
-
200
- const expected = {
201
- data: {
202
- categories: [
203
- {
204
- id: 'c1',
205
- name: 'Food',
206
- posts: [
207
- {
208
- id: '1',
209
- title: 'Post 1',
210
- longText: 'This is a long text 1',
211
- category: {
212
- id: 'c1',
213
- name: 'Food'
214
- }
215
- },
216
- {
217
- id: '3',
218
- title: 'Post 3',
219
- longText: 'This is a long text 3',
220
- category: {
221
- id: 'c1',
222
- name: 'Food'
223
- }
224
- },
225
- {
226
- id: '4',
227
- title: 'Post 4',
228
- longText: 'This is a long text 4',
229
- category: {
230
- id: 'c1',
231
- name: 'Food'
232
- }
233
- }
234
- ]
235
- },
236
- {
237
- id: 'c2',
238
- name: 'Places',
239
- posts: [
240
- {
241
- id: '2',
242
- title: 'Post 2',
243
- longText: 'This is a long text 2',
244
- category: {
245
- id: 'c2',
246
- name: 'Places'
247
- }
248
- }
249
- ]
250
- }
251
- ],
252
- posts: [
253
- {
254
- id: '1',
255
- title: 'Post 1',
256
- category: {
257
- id: 'c1',
258
- name: 'Food'
259
- }
260
- },
261
- {
262
- id: '2',
263
- title: 'Post 2',
264
- category: {
265
- id: 'c2',
266
- name: 'Places'
267
- }
268
- },
269
- {
270
- id: '3',
271
- title: 'Post 3',
272
- category: {
273
- id: 'c1',
274
- name: 'Food'
275
- }
276
- },
277
- {
278
- id: '4',
279
- title: 'Post 4',
280
- category: {
281
- id: 'c1',
282
- name: 'Food'
283
- }
284
- }
285
- ]
286
- }
287
- }
288
-
289
- t.comment('first request')
290
-
291
- {
292
- const res = await app.inject({
293
- method: 'POST',
294
- url: '/graphql',
295
- body: { query }
296
- })
297
-
298
- t.same(res.json(), expected)
299
- }
300
-
301
- t.comment('second request')
302
-
303
- {
304
- const res = await app.inject({
305
- method: 'POST',
306
- url: '/graphql',
307
- body: { query }
308
- })
309
-
310
- t.same(res.json(), expected)
311
- }
312
- })
@@ -1,172 +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 { connInfo, isPg } = require('./helper')
8
-
9
- async function runMigrations (db, sql) {
10
- await db.query(sql`
11
- create table company (
12
- id SERIAL primary key,
13
- name varchar(60)
14
- );
15
-
16
- create table social_medias (
17
- id SERIAL primary key,
18
- name varchar(60)
19
- );
20
-
21
- create table company_social_medias (
22
- id SERIAL primary key,
23
- username varchar not null,
24
- social_medias_id integer not null,
25
- company_id integer not null,
26
- constraint "fk_company_id" foreign key ("company_id") references company,
27
- constraint "fk_social_medias_id" foreign key ("social_medias_id") references social_medias
28
- );
29
- `)
30
- }
31
-
32
- async function clean (db, sql) {
33
- await db.query(sql`
34
- drop table if exists company_social_medias;
35
- drop table if exists company;
36
- drop table if exists social_medias;
37
- `)
38
- }
39
-
40
- test('do not crash', { skip: !isPg }, async ({ pass, teardown, same, equal }) => {
41
- const app = fastify()
42
- app.register(sqlMapper, {
43
- ...connInfo,
44
- async onDatabaseLoad (db, sql) {
45
- pass('onDatabaseLoad called')
46
- await clean(db, sql)
47
-
48
- await runMigrations(db, sql)
49
- teardown(async () => {
50
- await clean(db, sql)
51
- })
52
- }
53
- })
54
- await app.register(sqlGraphQL)
55
- teardown(app.close.bind(app))
56
-
57
- await app.ready()
58
-
59
- {
60
- const res = await app.inject({
61
- method: 'POST',
62
- url: '/graphql',
63
- body: {
64
- query: `
65
- mutation {
66
- saveCompany(input: { name: "Platformatic" }) {
67
- id
68
- name
69
- }
70
- }
71
- `
72
- }
73
- })
74
- equal(res.statusCode, 200, 'saveCompany status code')
75
- same(res.json(), {
76
- data: {
77
- saveCompany: {
78
- id: 1,
79
- name: 'Platformatic'
80
- }
81
- }
82
- }, 'saveCompany response')
83
- }
84
-
85
- {
86
- const res = await app.inject({
87
- method: 'POST',
88
- url: '/graphql',
89
- body: {
90
- query: `
91
- mutation {
92
- saveSocialMedia(input: { name: "Twitter" }) {
93
- id
94
- name
95
- }
96
- }
97
- `
98
- }
99
- })
100
- equal(res.statusCode, 200, 'saveSocialMedia status code')
101
- same(res.json(), {
102
- data: {
103
- saveSocialMedia: {
104
- id: 1,
105
- name: 'Twitter'
106
- }
107
- }
108
- }, 'saveSocialMedia response')
109
- }
110
-
111
- {
112
- const res = await app.inject({
113
- method: 'POST',
114
- url: '/graphql',
115
- body: {
116
- query: `
117
- mutation {
118
- saveCompanySocialMedia(input: { companyId: 1, socialMediasId: 1, username: "platformatic" }) {
119
- id
120
- username
121
- }
122
- }
123
- `
124
- }
125
- })
126
- equal(res.statusCode, 200, 'saveCompanySocialMedia status code')
127
- same(res.json(), {
128
- data: {
129
- saveCompanySocialMedia: {
130
- id: 1,
131
- username: 'platformatic'
132
- }
133
- }
134
- }, 'saveCompanySocialMedia response')
135
- }
136
-
137
- {
138
- const res = await app.inject({
139
- method: 'POST',
140
- url: '/graphql',
141
- body: {
142
- query: `
143
- query {
144
- company {
145
- name
146
- companySocialMedias {
147
- username
148
- socialMedias {
149
- name
150
- }
151
- }
152
- }
153
- }
154
- `
155
- }
156
- })
157
- equal(res.statusCode, 200, 'complex status code')
158
- same(res.json(), {
159
- data: {
160
- company: [{
161
- name: 'Platformatic',
162
- companySocialMedias: [{
163
- username: 'platformatic',
164
- socialMedias: {
165
- name: 'Twitter'
166
- }
167
- }]
168
- }]
169
- }
170
- }, 'complex response')
171
- }
172
- })