@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,277 +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 { isSQLite, isPg, connInfo, isMysql, clear } = require('./helper')
8
-
9
- test('should handle relationships with different schemas', { skip: isSQLite }, async ({ pass, teardown, same, equal }) => {
10
- const app = fastify()
11
- app.register(sqlMapper, {
12
- ...connInfo,
13
- schema: ['test1', 'test2'],
14
- async onDatabaseLoad (db, sql) {
15
- pass('onDatabaseLoad called')
16
-
17
- await clear(db, sql)
18
-
19
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
20
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
21
- if (isMysql) {
22
- await db.query(sql`
23
- CREATE TABLE test1.authors (
24
- id SERIAL PRIMARY KEY,
25
- name VARCHAR(255)
26
- );`)
27
- await db.query(sql`
28
- CREATE TABLE test2.books (
29
- id SERIAL PRIMARY KEY,
30
- title VARCHAR(42),
31
- author_id BIGINT UNSIGNED,
32
- FOREIGN KEY(author_id) REFERENCES test1.authors(id) ON DELETE CASCADE
33
- );
34
- `)
35
- } else {
36
- await db.query(sql`
37
- CREATE TABLE test1.authors (
38
- id SERIAL PRIMARY KEY,
39
- name VARCHAR(42)
40
- );
41
- CREATE TABLE test2.books (
42
- id SERIAL PRIMARY KEY,
43
- title VARCHAR(42),
44
- author_id INTEGER REFERENCES test1.authors(id)
45
- );
46
- `)
47
- }
48
- }
49
- })
50
- app.register(sqlGraphQL)
51
- teardown(app.close.bind(app))
52
-
53
- await app.ready()
54
-
55
- const authors = [{
56
- id: 1,
57
- name: 'Mark'
58
- }]
59
-
60
- const books = [{
61
- id: 1,
62
- title: 'Harry',
63
- authorId: 1
64
- }]
65
-
66
- {
67
- const res = await app.inject({
68
- method: 'POST',
69
- url: '/graphql',
70
- body: {
71
- query: `
72
- mutation batch($inputs : [Test1AuthorInput]!) {
73
- insertTest1Authors(inputs: $inputs) {
74
- id
75
- name
76
- }
77
- }
78
- `,
79
- variables: {
80
- inputs: authors
81
- }
82
- }
83
- })
84
- equal(res.statusCode, 200, 'authors status code')
85
- }
86
-
87
- {
88
- const res = await app.inject({
89
- method: 'POST',
90
- url: '/graphql',
91
- body: {
92
- query: `
93
- mutation batch($inputs : [Test2BookInput]!) {
94
- insertTest2Books(inputs: $inputs) {
95
- id
96
- title
97
- }
98
- }
99
- `,
100
- variables: {
101
- inputs: books
102
- }
103
- }
104
- })
105
- equal(res.statusCode, 200, 'books status code')
106
- }
107
-
108
- {
109
- const res = await app.inject({
110
- method: 'POST',
111
- url: '/graphql',
112
- body: {
113
- query: `
114
- query {
115
- test2Books {
116
- id
117
- author {
118
- id
119
- }
120
- }
121
- }
122
- `
123
- }
124
- })
125
- equal(res.statusCode, 200, 'query books')
126
- same(res.json(), {
127
- data: {
128
- test2Books: [{
129
- id: 1,
130
- author: {
131
- id: 1
132
- }
133
- }]
134
- }
135
- }, 'query book response')
136
- }
137
-
138
- {
139
- const res = await app.inject({
140
- method: 'POST',
141
- url: '/graphql',
142
- body: {
143
- query: `
144
- query {
145
- test1Authors {
146
- id
147
- test2Books {
148
- id
149
- }
150
- }
151
- }
152
- `
153
- }
154
- })
155
- equal(res.statusCode, 200, 'query authors')
156
- same(res.json(), {
157
- data: {
158
- test1Authors: [{
159
- id: 1,
160
- test2Books: [{
161
- id: 1
162
- }]
163
- }]
164
- }
165
- }, 'query authors response')
166
- }
167
- })
168
-
169
- test('should not throw if all of the schema with contraint references are loaded on the config', { skip: !isPg }, async ({ pass, teardown, same }) => {
170
- const app = fastify()
171
- app.register(sqlMapper, {
172
- ...connInfo,
173
- schema: ['test1', 'test2', 'test3', 'test4'],
174
- async onDatabaseLoad (db, sql) {
175
- pass('onDatabaseLoad called')
176
-
177
- await clear(db, sql)
178
-
179
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
180
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
181
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test3;`)
182
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test4;`)
183
- await db.query(sql`
184
- CREATE TABLE test1.authors (
185
- id INTEGER PRIMARY KEY,
186
- name VARCHAR(42)
187
- );
188
-
189
- CREATE TABLE test2.books (
190
- id INTEGER PRIMARY KEY,
191
- title VARCHAR(42),
192
- author_id INTEGER
193
- );
194
-
195
- CREATE TABLE test3.authors (
196
- id INTEGER PRIMARY KEY,
197
- name VARCHAR(42)
198
- );
199
-
200
- CREATE TABLE test4.books (
201
- id INTEGER PRIMARY KEY,
202
- title VARCHAR(42),
203
- author_id INTEGER
204
- );
205
-
206
- ALTER TABLE ONLY test2.books
207
- ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test1.authors(id);
208
-
209
- ALTER TABLE ONLY test4.books
210
- ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test3.authors(id);
211
- `)
212
- }
213
- })
214
- app.register(sqlGraphQL)
215
- teardown(app.close.bind(app))
216
-
217
- try {
218
- await app.ready()
219
- } catch (error) {
220
- same(true, false, 'app should not throw')
221
- }
222
- })
223
-
224
- test('should not throw if some of the schema with contraint references are not passed to the config', { skip: !isPg }, async ({ pass, teardown, same }) => {
225
- const app = fastify()
226
- app.register(sqlMapper, {
227
- ...connInfo,
228
- schema: ['test1', 'test2'],
229
- async onDatabaseLoad (db, sql) {
230
- pass('onDatabaseLoad called')
231
-
232
- await clear(db, sql)
233
-
234
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
235
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
236
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test3;`)
237
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test4;`)
238
- await db.query(sql`
239
- CREATE TABLE test1.authors (
240
- id INTEGER PRIMARY KEY,
241
- name VARCHAR(42)
242
- );
243
-
244
- CREATE TABLE test2.books (
245
- id INTEGER PRIMARY KEY,
246
- title VARCHAR(42),
247
- author_id INTEGER
248
- );
249
-
250
- CREATE TABLE test3.authors (
251
- id INTEGER PRIMARY KEY,
252
- name VARCHAR(42)
253
- );
254
-
255
- CREATE TABLE test4.books (
256
- id INTEGER PRIMARY KEY,
257
- title VARCHAR(42),
258
- author_id INTEGER
259
- );
260
-
261
- ALTER TABLE ONLY test2.books
262
- ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test1.authors(id);
263
-
264
- ALTER TABLE ONLY test4.books
265
- ADD CONSTRAINT authors_fkey FOREIGN KEY (author_id) REFERENCES test3.authors(id);
266
- `)
267
- }
268
- })
269
- app.register(sqlGraphQL)
270
- teardown(app.close.bind(app))
271
-
272
- try {
273
- await app.ready()
274
- } catch (error) {
275
- same(true, false, 'we expect the app to not throw')
276
- }
277
- })