@smartive/graphql-magic 2.1.1 → 3.0.1
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/.eslintrc +1 -1
- package/.github/workflows/release.yml +1 -1
- package/.github/workflows/testing.yml +3 -2
- package/.nvmrc +1 -1
- package/CHANGELOG.md +2 -2
- package/dist/cjs/index.cjs +8 -6
- package/dist/esm/migrations/generate.js.map +1 -1
- package/dist/esm/permissions/check.js.map +1 -1
- package/dist/esm/permissions/generate.js.map +1 -1
- package/dist/esm/resolvers/filters.js.map +1 -1
- package/dist/esm/resolvers/mutations.js +7 -6
- package/dist/esm/resolvers/mutations.js.map +1 -1
- package/dist/esm/resolvers/node.js.map +1 -1
- package/dist/esm/resolvers/resolver.js.map +1 -1
- package/dist/esm/resolvers/utils.js.map +1 -1
- package/docker-compose.yml +17 -0
- package/package.json +16 -9
- package/src/migrations/generate.ts +9 -9
- package/src/permissions/check.ts +1 -1
- package/src/permissions/generate.ts +7 -7
- package/src/resolvers/filters.ts +3 -3
- package/src/resolvers/mutations.ts +17 -14
- package/src/resolvers/node.ts +1 -1
- package/src/resolvers/resolver.ts +2 -2
- package/src/resolvers/utils.ts +2 -2
- package/tests/api/__snapshots__/delete.spec.ts.snap +18 -0
- package/tests/api/__snapshots__/query.spec.ts.snap +22 -0
- package/tests/api/delete.spec.ts +28 -0
- package/tests/api/query.spec.ts +28 -0
- package/tests/unit/__snapshots__/generate.spec.ts.snap +20 -0
- package/tests/unit/__snapshots__/resolve.spec.ts.snap +3 -0
- package/tests/unit/generate.spec.ts +2 -2
- package/tests/unit/resolve.spec.ts +2 -2
- package/tests/utils/database/knex.ts +19 -0
- package/tests/utils/database/schema.ts +62 -0
- package/tests/utils/database/seed.ts +54 -0
- package/tests/utils/generate-migration.ts +35 -0
- package/tests/{unit/utils.ts → utils/models.ts} +26 -5
- package/tests/utils/server.ts +108 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { makeExecutableSchema } from '@graphql-tools/schema';
|
|
2
|
+
import { execute, parse, Source, TypedQueryDocumentNode } from 'graphql';
|
|
3
|
+
import graphqlRequest, { RequestDocument, Variables } from 'graphql-request';
|
|
4
|
+
import { createServer, RequestListener } from 'http';
|
|
5
|
+
import { Knex } from 'knex';
|
|
6
|
+
import { DateTime } from 'luxon';
|
|
7
|
+
import { Context } from '../../src/context';
|
|
8
|
+
import { generate } from '../../src/generate';
|
|
9
|
+
import { getResolvers } from '../../src/resolvers';
|
|
10
|
+
import { getKnex } from './database/knex';
|
|
11
|
+
import { setupSchema } from './database/schema';
|
|
12
|
+
import { ADMIN_ID, setupSeed } from './database/seed';
|
|
13
|
+
import { models, permissions, rawModels } from './models';
|
|
14
|
+
|
|
15
|
+
const MIN_PORT = 49152;
|
|
16
|
+
const MAX_PORT = 65535;
|
|
17
|
+
|
|
18
|
+
export const withServer = async (
|
|
19
|
+
cb: (
|
|
20
|
+
request: (document: RequestDocument | TypedQueryDocumentNode, ...variablesAndRequestHeaders: any) => Promise<any>,
|
|
21
|
+
knex: Knex
|
|
22
|
+
) => Promise<void>
|
|
23
|
+
) => {
|
|
24
|
+
// eslint-disable-next-line prefer-const
|
|
25
|
+
let handler: RequestListener;
|
|
26
|
+
let port: number;
|
|
27
|
+
const server = createServer((req, res) => handler(req, res));
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line no-constant-condition
|
|
30
|
+
while (true) {
|
|
31
|
+
try {
|
|
32
|
+
port = Math.floor(Math.random() * (MAX_PORT - MIN_PORT + 1)) + MIN_PORT;
|
|
33
|
+
await new Promise<void>((res, rej) => server.listen(port, res).once('error', rej));
|
|
34
|
+
break;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error(e);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const rootKnex = getKnex();
|
|
41
|
+
const dbName = `test_${port}`;
|
|
42
|
+
await rootKnex.raw('DROP DATABASE IF EXISTS ?? WITH (FORCE)', dbName);
|
|
43
|
+
await rootKnex.raw(`CREATE DATABASE ?? OWNER ??`, [dbName, 'postgres']);
|
|
44
|
+
|
|
45
|
+
const knex = getKnex(dbName);
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await setupSchema(knex);
|
|
49
|
+
await setupSeed(knex);
|
|
50
|
+
|
|
51
|
+
handler = async (req, res) => {
|
|
52
|
+
const user = await knex('User').where({ id: ADMIN_ID }).first();
|
|
53
|
+
|
|
54
|
+
const typeDefs = generate(rawModels);
|
|
55
|
+
const contextValue: Context = {
|
|
56
|
+
req,
|
|
57
|
+
knex,
|
|
58
|
+
document: typeDefs,
|
|
59
|
+
locale: 'en',
|
|
60
|
+
locales: ['en'],
|
|
61
|
+
user,
|
|
62
|
+
rawModels,
|
|
63
|
+
models,
|
|
64
|
+
permissions,
|
|
65
|
+
now: DateTime.fromISO('2020-01-01T00:00:00.000Z'),
|
|
66
|
+
};
|
|
67
|
+
const {
|
|
68
|
+
query,
|
|
69
|
+
variables: variableValues,
|
|
70
|
+
operationName,
|
|
71
|
+
} = await new Promise<any>((res) => {
|
|
72
|
+
const chunks: any = [];
|
|
73
|
+
req
|
|
74
|
+
.on('data', (chunk) => {
|
|
75
|
+
chunks.push(chunk);
|
|
76
|
+
})
|
|
77
|
+
.on('end', () => {
|
|
78
|
+
res(JSON.parse(Buffer.concat(chunks).toString()));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
const result = await execute({
|
|
82
|
+
schema: makeExecutableSchema({
|
|
83
|
+
typeDefs,
|
|
84
|
+
resolvers: getResolvers(models),
|
|
85
|
+
}),
|
|
86
|
+
document: parse(new Source(query, 'GraphQL request')),
|
|
87
|
+
contextValue,
|
|
88
|
+
variableValues,
|
|
89
|
+
operationName,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
93
|
+
res.end(JSON.stringify(result));
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const request = <T, V extends Variables = Variables>(
|
|
97
|
+
document: RequestDocument | TypedQueryDocumentNode<T, V>,
|
|
98
|
+
...variablesAndRequestHeaders: any
|
|
99
|
+
) => graphqlRequest(`http://localhost:${port}`, document, ...variablesAndRequestHeaders);
|
|
100
|
+
|
|
101
|
+
await cb(request, knex);
|
|
102
|
+
} finally {
|
|
103
|
+
server.close();
|
|
104
|
+
await knex.destroy();
|
|
105
|
+
await rootKnex.raw('DROP DATABASE IF EXISTS ?? WITH (FORCE)', dbName);
|
|
106
|
+
await rootKnex.destroy();
|
|
107
|
+
}
|
|
108
|
+
};
|
package/tsconfig.json
CHANGED