@axinom/mosaic-graphql-codegen-plugins 0.2.0-rc.0 → 0.2.0-rc.4
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/dist/generate-bulk-edit-ui-config/generate-bulk-edit-ui-config.d.ts +8 -0
- package/dist/generate-bulk-edit-ui-config/generate-bulk-edit-ui-config.js +72 -0
- package/dist/generate-enum-comments/generate-enum-comments.d.ts +2 -0
- package/dist/generate-enum-comments/generate-enum-comments.js +49 -0
- package/package.json +4 -2
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PluginFunction } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
export interface BulkEditPluginConfig {
|
|
3
|
+
addKey?: string;
|
|
4
|
+
removeKey?: string;
|
|
5
|
+
setKey?: string;
|
|
6
|
+
filterKey?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const plugin: PluginFunction<Partial<BulkEditPluginConfig>>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.plugin = void 0;
|
|
4
|
+
const change_case_all_1 = require("change-case-all");
|
|
5
|
+
const plugin = (schema, _documents, config) => {
|
|
6
|
+
const addKey = config.addKey || 'relatedEntitiesToAdd';
|
|
7
|
+
const removeKey = config.removeKey || 'relatedEntitiesToRemove';
|
|
8
|
+
const setKey = config.setKey || 'set';
|
|
9
|
+
const filterKey = config.filterKey || 'filter';
|
|
10
|
+
const mutationType = schema.getMutationType();
|
|
11
|
+
const queryType = schema.getQueryType();
|
|
12
|
+
if (!mutationType || !queryType) {
|
|
13
|
+
// eslint-disable-next-line no-console
|
|
14
|
+
console.warn('No mutation type or query type found in schema');
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
const typesMap = schema.getTypeMap();
|
|
18
|
+
const mutations = mutationType.getFields();
|
|
19
|
+
const configs = Object.keys(mutations).map((mutationName) => {
|
|
20
|
+
const mutation = mutations[mutationName];
|
|
21
|
+
let formFieldsConfig = {};
|
|
22
|
+
mutation.args.map((arg) => {
|
|
23
|
+
switch (arg.name.toString()) {
|
|
24
|
+
case 'relatedEntitiesToAdd':
|
|
25
|
+
formFieldsConfig = Object.assign(Object.assign({}, formFieldsConfig), resolveFields(typesMap[arg.type.toString()].getFields(), addKey, 'Add'));
|
|
26
|
+
break;
|
|
27
|
+
case 'relatedEntitiesToRemove':
|
|
28
|
+
formFieldsConfig = Object.assign(Object.assign({}, formFieldsConfig), resolveFields(typesMap[arg.type.toString()].getFields(), removeKey, 'Remove'));
|
|
29
|
+
break;
|
|
30
|
+
case 'set':
|
|
31
|
+
formFieldsConfig = Object.assign(Object.assign({}, formFieldsConfig), resolveFields(typesMap[arg.type.toString()].getFields(), setKey));
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
if (Object.keys(formFieldsConfig).length > 0) {
|
|
36
|
+
return `export const ${(0, change_case_all_1.pascalCase)(mutationName)}FormFieldsConfig = { mutation: '${mutationName}', keys: { add: '${addKey}', remove: '${removeKey}', set: '${setKey}', filter: '${filterKey}' }, fields: ${JSON.stringify(formFieldsConfig, null, 2)}};`;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return ['/** Bulk Edit Configurations **/', ...configs]
|
|
40
|
+
.filter((config) => config !== null && config !== undefined)
|
|
41
|
+
.join('\n');
|
|
42
|
+
};
|
|
43
|
+
exports.plugin = plugin;
|
|
44
|
+
function resolveFields(fields, action, postfix = '') {
|
|
45
|
+
const resultingFields = {};
|
|
46
|
+
Object.keys(fields).map((fieldName) => {
|
|
47
|
+
const field = fields[fieldName];
|
|
48
|
+
let type = field.type.toString();
|
|
49
|
+
if (Object.keys(field.type).includes('ofType')) {
|
|
50
|
+
const compositeType = [];
|
|
51
|
+
Object.keys(field.type.ofType.getFields()).forEach((key) => {
|
|
52
|
+
const name = field.type.ofType.getFields()[key]
|
|
53
|
+
.name;
|
|
54
|
+
const type = field.type.ofType.getFields()[key]
|
|
55
|
+
.type;
|
|
56
|
+
compositeType.push({
|
|
57
|
+
[name]: type,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
type = compositeType;
|
|
61
|
+
}
|
|
62
|
+
resultingFields[postfix ? `${fieldName}${postfix}` : fieldName] = {
|
|
63
|
+
type: type,
|
|
64
|
+
label: postfix
|
|
65
|
+
? `${(0, change_case_all_1.capitalCase)(fieldName)} (${postfix})`
|
|
66
|
+
: (0, change_case_all_1.capitalCase)(fieldName),
|
|
67
|
+
originalFieldName: fieldName,
|
|
68
|
+
action: action,
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
return resultingFields;
|
|
72
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.plugin = void 0;
|
|
7
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
8
|
+
handlebars_1.default.registerHelper('jsonSafe', (/** @type {String} */ value) => {
|
|
9
|
+
return value.replace(/'/g, "\\'").replace(/\s+/g, ' ');
|
|
10
|
+
});
|
|
11
|
+
const template = handlebars_1.default.compile(`
|
|
12
|
+
/** generate-enum-comments **/
|
|
13
|
+
const getWithFallbackHandler: ProxyHandler<{ [key: string | symbol]: string}> = {
|
|
14
|
+
get: function(target, name) {
|
|
15
|
+
return target.hasOwnProperty(name) ? target[name] : name;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
{{#each enums}}
|
|
19
|
+
|
|
20
|
+
export const {{{name}}}Label = new Proxy<Record<string,string>>({
|
|
21
|
+
{{#each _values}}
|
|
22
|
+
{{#if description}}
|
|
23
|
+
'{{{name}}}' : '{{{jsonSafe description}}}',
|
|
24
|
+
{{/if}}
|
|
25
|
+
{{/each}}
|
|
26
|
+
}, getWithFallbackHandler);
|
|
27
|
+
{{/each}}
|
|
28
|
+
`);
|
|
29
|
+
function getEnumTypeMap(schema) {
|
|
30
|
+
const typeMap = schema.getTypeMap();
|
|
31
|
+
const result = [];
|
|
32
|
+
for (const typeName in typeMap) {
|
|
33
|
+
const type = typeMap[typeName];
|
|
34
|
+
if (type.constructor.name === 'GraphQLEnumType' &&
|
|
35
|
+
!typeName.startsWith('__') // Filter out internal types
|
|
36
|
+
) {
|
|
37
|
+
result.push(type);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
const plugin = (schema) => {
|
|
43
|
+
const enums = getEnumTypeMap(schema);
|
|
44
|
+
const result = template({
|
|
45
|
+
enums,
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
exports.plugin = plugin;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axinom/mosaic-graphql-codegen-plugins",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.4",
|
|
4
4
|
"description": "Library of graphql-codegen plugins for Mosaic workflows",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist",
|
|
7
7
|
"build": "yarn clean && tsc --project tsconfig.build.json",
|
|
8
|
+
"build:ci": "yarn workspaces focus && yarn build",
|
|
8
9
|
"test": "jest --silent",
|
|
9
10
|
"lint": "eslint . --ext .ts,.tsx,.js --color --cache"
|
|
10
11
|
},
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@graphql-codegen/plugin-helpers": "^5.1.1",
|
|
33
|
+
"graphql": "^15.0.0",
|
|
32
34
|
"jest": "^29",
|
|
33
35
|
"rimraf": "^3.0.2",
|
|
34
36
|
"ts-node": "^10.9.1",
|
|
@@ -40,5 +42,5 @@
|
|
|
40
42
|
"publishConfig": {
|
|
41
43
|
"access": "public"
|
|
42
44
|
},
|
|
43
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "309b232320896ccf706d8adb4bd6b38be6499e94"
|
|
44
46
|
}
|