@smartive/graphql-magic 2.1.0 → 3.0.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.
- package/.github/workflows/release.yml +1 -1
- package/.github/workflows/testing.yml +3 -2
- package/.nvmrc +1 -1
- package/CHANGELOG.md +1 -6
- package/dist/cjs/index.cjs +53 -15
- package/dist/esm/db/generate.js +45 -8
- package/dist/esm/db/generate.js.map +1 -1
- 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 +15 -9
- package/src/db/generate.ts +63 -17
- 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__/query.spec.ts.snap +22 -0
- package/tests/api/query.spec.ts +28 -0
- package/tests/unit/__snapshots__/generate.spec.ts.snap +6 -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 +51 -0
- package/tests/utils/database/seed.ts +53 -0
- package/tests/utils/generate-migration.ts +35 -0
- package/tests/{unit/utils.ts → utils/models.ts} +16 -5
- package/tests/utils/server.ts +108 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Knex } from 'knex';
|
|
2
|
+
|
|
3
|
+
export const setupSchema = async (knex: Knex) => {
|
|
4
|
+
await knex.raw(`CREATE TYPE "someEnum" AS ENUM ('A','B','C')`);
|
|
5
|
+
|
|
6
|
+
await knex.raw(`CREATE TYPE "role" AS ENUM ('ADMIN','USER')`);
|
|
7
|
+
|
|
8
|
+
await knex.schema.createTable('User', (table) => {
|
|
9
|
+
table.uuid('id').notNullable().primary();
|
|
10
|
+
table.string('username', undefined).nullable();
|
|
11
|
+
table
|
|
12
|
+
.enum('role', null as any, {
|
|
13
|
+
useNative: true,
|
|
14
|
+
existingType: true,
|
|
15
|
+
enumName: 'role',
|
|
16
|
+
})
|
|
17
|
+
.nullable();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
await knex.schema.createTable('AnotherObject', (table) => {
|
|
21
|
+
table.uuid('id').notNullable().primary();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
await knex.schema.createTable('SomeObject', (table) => {
|
|
25
|
+
table.uuid('id').notNullable().primary();
|
|
26
|
+
table.string('field', undefined).nullable();
|
|
27
|
+
table.uuid('anotherId').notNullable();
|
|
28
|
+
table.foreign('anotherId').references('id').inTable('AnotherObject');
|
|
29
|
+
table.decimal('list', 1, 1).notNullable();
|
|
30
|
+
table.integer('xyz').notNullable();
|
|
31
|
+
table.timestamp('createdAt').notNullable();
|
|
32
|
+
table.uuid('createdById').notNullable();
|
|
33
|
+
table.foreign('createdById').references('id').inTable('User');
|
|
34
|
+
table.timestamp('updatedAt').notNullable();
|
|
35
|
+
table.uuid('updatedById').notNullable();
|
|
36
|
+
table.foreign('updatedById').references('id').inTable('User');
|
|
37
|
+
table.boolean('deleted').notNullable().defaultTo(false);
|
|
38
|
+
table.timestamp('deletedAt').nullable();
|
|
39
|
+
table.uuid('deletedById').nullable();
|
|
40
|
+
table.foreign('deletedById').references('id').inTable('User');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
await knex.schema.createTable('SomeObjectRevision', (table) => {
|
|
44
|
+
table.uuid('id').notNullable().primary();
|
|
45
|
+
table.uuid('someObjectId').notNullable();
|
|
46
|
+
table.uuid('createdById').notNullable();
|
|
47
|
+
table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now(0));
|
|
48
|
+
table.boolean('deleted').notNullable();
|
|
49
|
+
table.integer('xyz').notNullable();
|
|
50
|
+
});
|
|
51
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Knex } from 'knex';
|
|
2
|
+
import { DateTime } from 'luxon';
|
|
3
|
+
import { summonByName } from '../../../src';
|
|
4
|
+
import { models } from '../models';
|
|
5
|
+
|
|
6
|
+
export const ADMIN_ID = '04e45b48-04cf-4b38-bb25-b9af5ae0b2c4';
|
|
7
|
+
|
|
8
|
+
export const SOME_ID = '604ab55d-ec3e-4857-9f27-219158f80e64';
|
|
9
|
+
export const ANOTHER_ID = '226a20e8-5c18-4423-99ca-eb0df6ff4fdd';
|
|
10
|
+
|
|
11
|
+
export const seed = {
|
|
12
|
+
User: [
|
|
13
|
+
{
|
|
14
|
+
id: ADMIN_ID,
|
|
15
|
+
username: 'admin',
|
|
16
|
+
role: 'ADMIN',
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
AnotherObject: [
|
|
20
|
+
{
|
|
21
|
+
id: ANOTHER_ID,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
SomeObject: [
|
|
25
|
+
{
|
|
26
|
+
id: SOME_ID,
|
|
27
|
+
anotherId: ANOTHER_ID,
|
|
28
|
+
list: 0,
|
|
29
|
+
xyz: 1,
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const setupSeed = async (knex: Knex) => {
|
|
35
|
+
const now = DateTime.now();
|
|
36
|
+
for (const [table, entities] of Object.entries(seed)) {
|
|
37
|
+
const model = summonByName(models, table);
|
|
38
|
+
await knex.batchInsert(
|
|
39
|
+
table,
|
|
40
|
+
entities.map((entity, i) => ({
|
|
41
|
+
...entity,
|
|
42
|
+
...(model.creatable && {
|
|
43
|
+
createdAt: now.plus({ second: i }),
|
|
44
|
+
createdById: ADMIN_ID,
|
|
45
|
+
}),
|
|
46
|
+
...(model.updatable && {
|
|
47
|
+
updatedAt: now.plus({ second: i }),
|
|
48
|
+
updatedById: ADMIN_ID,
|
|
49
|
+
}),
|
|
50
|
+
}))
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { writeFileSync } from 'fs';
|
|
2
|
+
import { simpleGit } from 'simple-git';
|
|
3
|
+
import { MigrationGenerator } from '../../src/migrations/generate';
|
|
4
|
+
import { getKnex } from './database/knex';
|
|
5
|
+
import { rawModels } from './models';
|
|
6
|
+
|
|
7
|
+
const git = simpleGit();
|
|
8
|
+
|
|
9
|
+
const getDate = () => {
|
|
10
|
+
const date = new Date();
|
|
11
|
+
const year = date.getFullYear();
|
|
12
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
13
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
14
|
+
const hours = String(date.getHours()).padStart(2, '0');
|
|
15
|
+
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
16
|
+
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
17
|
+
|
|
18
|
+
return `${year}${month}${day}${hours}${minutes}${seconds}`;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const writeMigration = async () => {
|
|
22
|
+
const name = process.argv[2] || (await git.branch()).current.split('/').pop();
|
|
23
|
+
|
|
24
|
+
const knex = getKnex();
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const migrations = await new MigrationGenerator(knex, rawModels).generate();
|
|
28
|
+
|
|
29
|
+
writeFileSync(`tmp/${getDate()}_${name}.ts`, migrations);
|
|
30
|
+
} finally {
|
|
31
|
+
await knex.destroy();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
void writeMigration();
|
|
@@ -8,6 +8,11 @@ export const rawModels: RawModels = [
|
|
|
8
8
|
type: 'enum',
|
|
9
9
|
values: ['A', 'B', 'C'],
|
|
10
10
|
},
|
|
11
|
+
{
|
|
12
|
+
name: 'Role',
|
|
13
|
+
type: 'enum',
|
|
14
|
+
values: ['ADMIN', 'USER'],
|
|
15
|
+
},
|
|
11
16
|
|
|
12
17
|
{
|
|
13
18
|
name: 'SomeRawObject',
|
|
@@ -23,8 +28,17 @@ export const rawModels: RawModels = [
|
|
|
23
28
|
name: 'username',
|
|
24
29
|
type: 'String',
|
|
25
30
|
},
|
|
31
|
+
{
|
|
32
|
+
name: 'role',
|
|
33
|
+
type: 'Role',
|
|
34
|
+
},
|
|
26
35
|
],
|
|
27
36
|
},
|
|
37
|
+
{
|
|
38
|
+
type: 'object',
|
|
39
|
+
name: 'AnotherObject',
|
|
40
|
+
fields: [],
|
|
41
|
+
},
|
|
28
42
|
{
|
|
29
43
|
type: 'object',
|
|
30
44
|
name: 'SomeObject',
|
|
@@ -51,6 +65,8 @@ export const rawModels: RawModels = [
|
|
|
51
65
|
{
|
|
52
66
|
name: 'list',
|
|
53
67
|
type: 'Float',
|
|
68
|
+
scale: 1,
|
|
69
|
+
precision: 1,
|
|
54
70
|
nonNull: true,
|
|
55
71
|
list: true,
|
|
56
72
|
args: [{ name: 'magic', type: 'Boolean' }],
|
|
@@ -66,11 +82,6 @@ export const rawModels: RawModels = [
|
|
|
66
82
|
},
|
|
67
83
|
],
|
|
68
84
|
},
|
|
69
|
-
{
|
|
70
|
-
type: 'object',
|
|
71
|
-
name: 'AnotherObject',
|
|
72
|
-
fields: [],
|
|
73
|
-
},
|
|
74
85
|
];
|
|
75
86
|
|
|
76
87
|
export const models = getModels(rawModels);
|
|
@@ -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