@graphql-inspector/similar-command 0.0.0-PLACEHOLDER
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/index.d.ts +16 -0
- package/index.js +151 -0
- package/index.mjs +147 -0
- package/package.json +48 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GlobalArgs, CommandFactory } from '@graphql-inspector/commands';
|
|
2
|
+
import { GraphQLSchema } from 'graphql';
|
|
3
|
+
export { CommandFactory };
|
|
4
|
+
export declare function handler({ schema, writePath, type, threshold, }: {
|
|
5
|
+
schema: GraphQLSchema;
|
|
6
|
+
writePath?: string;
|
|
7
|
+
type?: string;
|
|
8
|
+
threshold?: number;
|
|
9
|
+
}): void;
|
|
10
|
+
declare const _default: CommandFactory<{}, {
|
|
11
|
+
schema: string;
|
|
12
|
+
name?: string | undefined;
|
|
13
|
+
threshold?: number | undefined;
|
|
14
|
+
write?: string | undefined;
|
|
15
|
+
} & GlobalArgs>;
|
|
16
|
+
export default _default;
|
package/index.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const tslib = require('tslib');
|
|
6
|
+
const commands = require('@graphql-inspector/commands');
|
|
7
|
+
const logger = require('@graphql-inspector/logger');
|
|
8
|
+
const core = require('@graphql-inspector/core');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
|
|
12
|
+
function handler({ schema, writePath, type, threshold, }) {
|
|
13
|
+
const shouldWrite = typeof writePath !== 'undefined';
|
|
14
|
+
const similarMap = core.similar(schema, type, threshold);
|
|
15
|
+
if (!Object.keys(similarMap).length) {
|
|
16
|
+
logger.Logger.info('No similar types found');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
for (const typeName in similarMap) {
|
|
20
|
+
if (similarMap.hasOwnProperty(typeName)) {
|
|
21
|
+
const matches = similarMap[typeName];
|
|
22
|
+
const prefix = core.getTypePrefix(schema.getType(typeName));
|
|
23
|
+
const sourceType = logger.chalk.bold(typeName);
|
|
24
|
+
const name = matches.bestMatch.target.typeId;
|
|
25
|
+
logger.Logger.log('');
|
|
26
|
+
logger.Logger.log(`${prefix} ${sourceType}`);
|
|
27
|
+
logger.Logger.log(printResult(name, matches.bestMatch.rating));
|
|
28
|
+
matches.ratings.forEach((match) => {
|
|
29
|
+
logger.Logger.log(printResult(match.target.typeId, match.rating));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (shouldWrite) {
|
|
34
|
+
if (typeof writePath !== 'string') {
|
|
35
|
+
throw new Error(`--write is not valid file path: ${writePath}`);
|
|
36
|
+
}
|
|
37
|
+
const absPath = commands.ensureAbsolute(writePath);
|
|
38
|
+
const ext = path.extname(absPath).replace('.', '').toLocaleLowerCase();
|
|
39
|
+
let output = undefined;
|
|
40
|
+
const results = transformMap(similarMap);
|
|
41
|
+
if (ext === 'json') {
|
|
42
|
+
output = outputJSON(results);
|
|
43
|
+
}
|
|
44
|
+
if (output) {
|
|
45
|
+
fs.writeFileSync(absPath, output, {
|
|
46
|
+
encoding: 'utf-8',
|
|
47
|
+
});
|
|
48
|
+
logger.Logger.success(`Available at ${absPath}\n`);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
throw new Error(`Extension ${ext} is not supported`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const index = commands.createCommand((api) => {
|
|
57
|
+
const { loaders } = api;
|
|
58
|
+
return {
|
|
59
|
+
command: 'similar <schema>',
|
|
60
|
+
describe: 'Find similar types in a schema',
|
|
61
|
+
builder(yargs) {
|
|
62
|
+
return yargs
|
|
63
|
+
.positional('schema', {
|
|
64
|
+
describe: 'Point to a schema',
|
|
65
|
+
type: 'string',
|
|
66
|
+
demandOption: true,
|
|
67
|
+
})
|
|
68
|
+
.options({
|
|
69
|
+
n: {
|
|
70
|
+
alias: 'name',
|
|
71
|
+
describe: 'Name of a type',
|
|
72
|
+
type: 'string',
|
|
73
|
+
},
|
|
74
|
+
t: {
|
|
75
|
+
alias: 'threshold',
|
|
76
|
+
describe: 'Threshold of similarity ratio',
|
|
77
|
+
type: 'number',
|
|
78
|
+
},
|
|
79
|
+
w: {
|
|
80
|
+
alias: 'write',
|
|
81
|
+
describe: 'Write a file with stats',
|
|
82
|
+
type: 'string',
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
handler(args) {
|
|
87
|
+
var _a;
|
|
88
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const { headers, token } = commands.parseGlobalArgs(args);
|
|
90
|
+
const writePath = args.write;
|
|
91
|
+
const type = args.name;
|
|
92
|
+
const threshold = args.threshold;
|
|
93
|
+
const apolloFederation = args.federation || false;
|
|
94
|
+
const aws = args.aws || false;
|
|
95
|
+
const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
|
|
96
|
+
const schema = yield loaders.loadSchema(args.schema, {
|
|
97
|
+
headers,
|
|
98
|
+
token,
|
|
99
|
+
method,
|
|
100
|
+
}, apolloFederation, aws);
|
|
101
|
+
return handler({ schema, writePath, type, threshold });
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
function indent(line, space) {
|
|
107
|
+
return line.padStart(line.length + space, ' ');
|
|
108
|
+
}
|
|
109
|
+
function transformMap(similarMap) {
|
|
110
|
+
const results = {};
|
|
111
|
+
for (const typename in similarMap) {
|
|
112
|
+
if (similarMap.hasOwnProperty(typename)) {
|
|
113
|
+
const result = similarMap[typename];
|
|
114
|
+
results[typename] = [];
|
|
115
|
+
if (result.bestMatch) {
|
|
116
|
+
results[typename].push(trasformResult(result.bestMatch));
|
|
117
|
+
}
|
|
118
|
+
if (result.ratings) {
|
|
119
|
+
results[typename].push(...result.ratings.map(trasformResult));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return results;
|
|
124
|
+
}
|
|
125
|
+
function trasformResult(record) {
|
|
126
|
+
return {
|
|
127
|
+
typename: record.target.typeId,
|
|
128
|
+
rating: record.rating,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function outputJSON(results) {
|
|
132
|
+
return JSON.stringify(results);
|
|
133
|
+
}
|
|
134
|
+
function printResult(name, rating) {
|
|
135
|
+
const percentage = logger.chalk.grey(`(${formatRating(rating)}%)`);
|
|
136
|
+
return indent(`${printScale(rating)} ${percentage} ${name}`, 0);
|
|
137
|
+
}
|
|
138
|
+
function printScale(ratio) {
|
|
139
|
+
const percentage = Math.floor(ratio * 100);
|
|
140
|
+
const levels = [0, 30, 50, 70, 90];
|
|
141
|
+
return levels
|
|
142
|
+
.map((level) => percentage >= level)
|
|
143
|
+
.map((enabled) => (enabled ? logger.figures.bullet : logger.chalk.gray(logger.figures.bullet)))
|
|
144
|
+
.join('');
|
|
145
|
+
}
|
|
146
|
+
function formatRating(ratio) {
|
|
147
|
+
return Math.floor(ratio * 100);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
exports.default = index;
|
|
151
|
+
exports.handler = handler;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { __awaiter } from 'tslib';
|
|
2
|
+
import { createCommand, parseGlobalArgs, ensureAbsolute } from '@graphql-inspector/commands';
|
|
3
|
+
import { Logger, chalk, figures } from '@graphql-inspector/logger';
|
|
4
|
+
import { similar, getTypePrefix } from '@graphql-inspector/core';
|
|
5
|
+
import { extname } from 'path';
|
|
6
|
+
import { writeFileSync } from 'fs';
|
|
7
|
+
|
|
8
|
+
function handler({ schema, writePath, type, threshold, }) {
|
|
9
|
+
const shouldWrite = typeof writePath !== 'undefined';
|
|
10
|
+
const similarMap = similar(schema, type, threshold);
|
|
11
|
+
if (!Object.keys(similarMap).length) {
|
|
12
|
+
Logger.info('No similar types found');
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
for (const typeName in similarMap) {
|
|
16
|
+
if (similarMap.hasOwnProperty(typeName)) {
|
|
17
|
+
const matches = similarMap[typeName];
|
|
18
|
+
const prefix = getTypePrefix(schema.getType(typeName));
|
|
19
|
+
const sourceType = chalk.bold(typeName);
|
|
20
|
+
const name = matches.bestMatch.target.typeId;
|
|
21
|
+
Logger.log('');
|
|
22
|
+
Logger.log(`${prefix} ${sourceType}`);
|
|
23
|
+
Logger.log(printResult(name, matches.bestMatch.rating));
|
|
24
|
+
matches.ratings.forEach((match) => {
|
|
25
|
+
Logger.log(printResult(match.target.typeId, match.rating));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (shouldWrite) {
|
|
30
|
+
if (typeof writePath !== 'string') {
|
|
31
|
+
throw new Error(`--write is not valid file path: ${writePath}`);
|
|
32
|
+
}
|
|
33
|
+
const absPath = ensureAbsolute(writePath);
|
|
34
|
+
const ext = extname(absPath).replace('.', '').toLocaleLowerCase();
|
|
35
|
+
let output = undefined;
|
|
36
|
+
const results = transformMap(similarMap);
|
|
37
|
+
if (ext === 'json') {
|
|
38
|
+
output = outputJSON(results);
|
|
39
|
+
}
|
|
40
|
+
if (output) {
|
|
41
|
+
writeFileSync(absPath, output, {
|
|
42
|
+
encoding: 'utf-8',
|
|
43
|
+
});
|
|
44
|
+
Logger.success(`Available at ${absPath}\n`);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw new Error(`Extension ${ext} is not supported`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const index = createCommand((api) => {
|
|
53
|
+
const { loaders } = api;
|
|
54
|
+
return {
|
|
55
|
+
command: 'similar <schema>',
|
|
56
|
+
describe: 'Find similar types in a schema',
|
|
57
|
+
builder(yargs) {
|
|
58
|
+
return yargs
|
|
59
|
+
.positional('schema', {
|
|
60
|
+
describe: 'Point to a schema',
|
|
61
|
+
type: 'string',
|
|
62
|
+
demandOption: true,
|
|
63
|
+
})
|
|
64
|
+
.options({
|
|
65
|
+
n: {
|
|
66
|
+
alias: 'name',
|
|
67
|
+
describe: 'Name of a type',
|
|
68
|
+
type: 'string',
|
|
69
|
+
},
|
|
70
|
+
t: {
|
|
71
|
+
alias: 'threshold',
|
|
72
|
+
describe: 'Threshold of similarity ratio',
|
|
73
|
+
type: 'number',
|
|
74
|
+
},
|
|
75
|
+
w: {
|
|
76
|
+
alias: 'write',
|
|
77
|
+
describe: 'Write a file with stats',
|
|
78
|
+
type: 'string',
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
handler(args) {
|
|
83
|
+
var _a;
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
const { headers, token } = parseGlobalArgs(args);
|
|
86
|
+
const writePath = args.write;
|
|
87
|
+
const type = args.name;
|
|
88
|
+
const threshold = args.threshold;
|
|
89
|
+
const apolloFederation = args.federation || false;
|
|
90
|
+
const aws = args.aws || false;
|
|
91
|
+
const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST';
|
|
92
|
+
const schema = yield loaders.loadSchema(args.schema, {
|
|
93
|
+
headers,
|
|
94
|
+
token,
|
|
95
|
+
method,
|
|
96
|
+
}, apolloFederation, aws);
|
|
97
|
+
return handler({ schema, writePath, type, threshold });
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
function indent(line, space) {
|
|
103
|
+
return line.padStart(line.length + space, ' ');
|
|
104
|
+
}
|
|
105
|
+
function transformMap(similarMap) {
|
|
106
|
+
const results = {};
|
|
107
|
+
for (const typename in similarMap) {
|
|
108
|
+
if (similarMap.hasOwnProperty(typename)) {
|
|
109
|
+
const result = similarMap[typename];
|
|
110
|
+
results[typename] = [];
|
|
111
|
+
if (result.bestMatch) {
|
|
112
|
+
results[typename].push(trasformResult(result.bestMatch));
|
|
113
|
+
}
|
|
114
|
+
if (result.ratings) {
|
|
115
|
+
results[typename].push(...result.ratings.map(trasformResult));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return results;
|
|
120
|
+
}
|
|
121
|
+
function trasformResult(record) {
|
|
122
|
+
return {
|
|
123
|
+
typename: record.target.typeId,
|
|
124
|
+
rating: record.rating,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function outputJSON(results) {
|
|
128
|
+
return JSON.stringify(results);
|
|
129
|
+
}
|
|
130
|
+
function printResult(name, rating) {
|
|
131
|
+
const percentage = chalk.grey(`(${formatRating(rating)}%)`);
|
|
132
|
+
return indent(`${printScale(rating)} ${percentage} ${name}`, 0);
|
|
133
|
+
}
|
|
134
|
+
function printScale(ratio) {
|
|
135
|
+
const percentage = Math.floor(ratio * 100);
|
|
136
|
+
const levels = [0, 30, 50, 70, 90];
|
|
137
|
+
return levels
|
|
138
|
+
.map((level) => percentage >= level)
|
|
139
|
+
.map((enabled) => (enabled ? figures.bullet : chalk.gray(figures.bullet)))
|
|
140
|
+
.join('');
|
|
141
|
+
}
|
|
142
|
+
function formatRating(ratio) {
|
|
143
|
+
return Math.floor(ratio * 100);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export default index;
|
|
147
|
+
export { handler };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-inspector/similar-command",
|
|
3
|
+
"version": "0.0.0-PLACEHOLDER",
|
|
4
|
+
"description": "Find similar types in GraphQL Schema",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@graphql-inspector/commands": "0.0.0-PLACEHOLDER",
|
|
11
|
+
"@graphql-inspector/core": "0.0.0-PLACEHOLDER",
|
|
12
|
+
"@graphql-inspector/logger": "0.0.0-PLACEHOLDER",
|
|
13
|
+
"tslib": "^2.0.0"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "kamilkisiela/graphql-inspector",
|
|
18
|
+
"directory": "packages/commands/similar"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"graphql",
|
|
22
|
+
"graphql-inspector",
|
|
23
|
+
"graphql-inspector-command",
|
|
24
|
+
"tools"
|
|
25
|
+
],
|
|
26
|
+
"author": {
|
|
27
|
+
"name": "Kamil Kisiela",
|
|
28
|
+
"email": "kamil.kisiela@gmail.com",
|
|
29
|
+
"url": "https://github.com/kamilkisiela"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"main": "index.js",
|
|
33
|
+
"module": "index.mjs",
|
|
34
|
+
"typings": "index.d.ts",
|
|
35
|
+
"typescript": {
|
|
36
|
+
"definition": "index.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"require": "./index.js",
|
|
41
|
+
"import": "./index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./*": {
|
|
44
|
+
"require": "./*.js",
|
|
45
|
+
"import": "./*.mjs"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|