@graphql-inspector/similar-command 3.1.4 → 3.4.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.
File without changes
@@ -25,7 +25,7 @@ function handler({ schema, writePath, type, threshold, }) {
25
25
  logger.Logger.log('');
26
26
  logger.Logger.log(`${prefix} ${sourceType}`);
27
27
  logger.Logger.log(printResult(name, matches.bestMatch.rating));
28
- matches.ratings.forEach((match) => {
28
+ matches.ratings.forEach(match => {
29
29
  logger.Logger.log(printResult(match.target.typeId, match.rating));
30
30
  });
31
31
  }
@@ -53,7 +53,7 @@ function handler({ schema, writePath, type, threshold, }) {
53
53
  }
54
54
  }
55
55
  }
56
- const index = commands.createCommand((api) => {
56
+ const index = commands.createCommand(api => {
57
57
  const { loaders } = api;
58
58
  return {
59
59
  command: 'similar <schema>',
@@ -139,8 +139,8 @@ function printScale(ratio) {
139
139
  const percentage = Math.floor(ratio * 100);
140
140
  const levels = [0, 30, 50, 70, 90];
141
141
  return levels
142
- .map((level) => percentage >= level)
143
- .map((enabled) => (enabled ? logger.figures.bullet : logger.chalk.gray(logger.figures.bullet)))
142
+ .map(level => percentage >= level)
143
+ .map(enabled => (enabled ? logger.figures.bullet : logger.chalk.gray(logger.figures.bullet)))
144
144
  .join('');
145
145
  }
146
146
  function formatRating(ratio) {
@@ -21,7 +21,7 @@ function handler({ schema, writePath, type, threshold, }) {
21
21
  Logger.log('');
22
22
  Logger.log(`${prefix} ${sourceType}`);
23
23
  Logger.log(printResult(name, matches.bestMatch.rating));
24
- matches.ratings.forEach((match) => {
24
+ matches.ratings.forEach(match => {
25
25
  Logger.log(printResult(match.target.typeId, match.rating));
26
26
  });
27
27
  }
@@ -49,7 +49,7 @@ function handler({ schema, writePath, type, threshold, }) {
49
49
  }
50
50
  }
51
51
  }
52
- const index = createCommand((api) => {
52
+ const index = createCommand(api => {
53
53
  const { loaders } = api;
54
54
  return {
55
55
  command: 'similar <schema>',
@@ -135,8 +135,8 @@ function printScale(ratio) {
135
135
  const percentage = Math.floor(ratio * 100);
136
136
  const levels = [0, 30, 50, 70, 90];
137
137
  return levels
138
- .map((level) => percentage >= level)
139
- .map((enabled) => (enabled ? figures.bullet : chalk.gray(figures.bullet)))
138
+ .map(level => percentage >= level)
139
+ .map(enabled => (enabled ? figures.bullet : chalk.gray(figures.bullet)))
140
140
  .join('');
141
141
  }
142
142
  function formatRating(ratio) {
package/package.json CHANGED
@@ -1,51 +1,48 @@
1
1
  {
2
2
  "name": "@graphql-inspector/similar-command",
3
- "version": "3.1.4",
3
+ "version": "3.4.0",
4
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": "3.4.0",
11
+ "@graphql-inspector/core": "3.4.0",
12
+ "@graphql-inspector/logger": "3.4.0",
13
+ "tslib": "^2.0.0"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "kamilkisiela/graphql-inspector",
18
+ "directory": "packages/commands/similar"
19
+ },
5
20
  "keywords": [
6
21
  "graphql",
7
22
  "graphql-inspector",
8
23
  "graphql-inspector-command",
9
24
  "tools"
10
25
  ],
11
- "sideEffects": false,
12
- "main": "dist/index.js",
13
- "module": "dist/index.mjs",
14
- "exports": {
15
- ".": {
16
- "require": "./dist/index.js",
17
- "import": "./dist/index.mjs"
18
- },
19
- "./*": {
20
- "require": "./dist/*.js",
21
- "import": "./dist/*.mjs"
22
- }
23
- },
24
- "typings": "dist/index.d.ts",
25
- "typescript": {
26
- "definition": "dist/index.d.ts"
27
- },
28
26
  "author": {
29
27
  "name": "Kamil Kisiela",
30
28
  "email": "kamil.kisiela@gmail.com",
31
29
  "url": "https://github.com/kamilkisiela"
32
30
  },
33
31
  "license": "MIT",
34
- "repository": {
35
- "type": "git",
36
- "url": "kamilkisiela/graphql-inspector",
37
- "directory": "packages/commands/similar"
38
- },
39
- "peerDependencies": {
40
- "graphql": "^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
41
- },
42
- "dependencies": {
43
- "@graphql-inspector/commands": "3.1.4",
44
- "@graphql-inspector/core": "3.1.4",
45
- "@graphql-inspector/logger": "3.1.4",
46
- "tslib": "^2.0.0"
32
+ "main": "index.js",
33
+ "module": "index.mjs",
34
+ "typings": "index.d.ts",
35
+ "typescript": {
36
+ "definition": "index.d.ts"
47
37
  },
48
- "scripts": {
49
- "prepack": "bob prepack"
38
+ "exports": {
39
+ ".": {
40
+ "require": "./index.js",
41
+ "import": "./index.mjs"
42
+ },
43
+ "./*": {
44
+ "require": "./*.js",
45
+ "import": "./*.mjs"
46
+ }
50
47
  }
51
48
  }
package/dist/package.json DELETED
@@ -1,48 +0,0 @@
1
- {
2
- "name": "@graphql-inspector/similar-command",
3
- "version": "3.1.4",
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": "3.1.4",
11
- "@graphql-inspector/core": "3.1.4",
12
- "@graphql-inspector/logger": "3.1.4",
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
- }
package/src/index.ts DELETED
@@ -1,213 +0,0 @@
1
- import {
2
- createCommand,
3
- GlobalArgs,
4
- ensureAbsolute,
5
- parseGlobalArgs,
6
- CommandFactory,
7
- } from '@graphql-inspector/commands';
8
- import { Logger, figures, chalk } from '@graphql-inspector/logger';
9
- import {
10
- similar as findSimilar,
11
- getTypePrefix,
12
- SimilarMap,
13
- Rating,
14
- } from '@graphql-inspector/core';
15
- import { extname } from 'path';
16
- import { GraphQLNamedType, GraphQLSchema } from 'graphql';
17
- import { writeFileSync } from 'fs';
18
-
19
- export { CommandFactory };
20
-
21
- export function handler({
22
- schema,
23
- writePath,
24
- type,
25
- threshold,
26
- }: {
27
- schema: GraphQLSchema;
28
- writePath?: string;
29
- type?: string;
30
- threshold?: number;
31
- }) {
32
- const shouldWrite = typeof writePath !== 'undefined';
33
- const similarMap = findSimilar(schema, type, threshold);
34
-
35
- if (!Object.keys(similarMap).length) {
36
- Logger.info('No similar types found');
37
- } else {
38
- for (const typeName in similarMap) {
39
- if (similarMap.hasOwnProperty(typeName)) {
40
- const matches = similarMap[typeName];
41
- const prefix = getTypePrefix(
42
- schema.getType(typeName) as GraphQLNamedType,
43
- );
44
- const sourceType = chalk.bold(typeName);
45
- const name = matches.bestMatch.target.typeId;
46
-
47
- Logger.log('');
48
- Logger.log(`${prefix} ${sourceType}`);
49
- Logger.log(printResult(name, matches.bestMatch.rating));
50
-
51
- matches.ratings.forEach((match) => {
52
- Logger.log(printResult(match.target.typeId, match.rating));
53
- });
54
- }
55
- }
56
-
57
- if (shouldWrite) {
58
- if (typeof writePath !== 'string') {
59
- throw new Error(`--write is not valid file path: ${writePath}`);
60
- }
61
-
62
- const absPath = ensureAbsolute(writePath);
63
- const ext = extname(absPath).replace('.', '').toLocaleLowerCase();
64
-
65
- let output: string | undefined = undefined;
66
- const results = transformMap(similarMap);
67
-
68
- if (ext === 'json') {
69
- output = outputJSON(results);
70
- }
71
-
72
- if (output) {
73
- writeFileSync(absPath, output, {
74
- encoding: 'utf-8',
75
- });
76
-
77
- Logger.success(`Available at ${absPath}\n`);
78
- } else {
79
- throw new Error(`Extension ${ext} is not supported`);
80
- }
81
- }
82
- }
83
- }
84
-
85
- export default createCommand<
86
- {},
87
- {
88
- schema: string;
89
- name?: string;
90
- threshold?: number;
91
- write?: string;
92
- } & GlobalArgs
93
- >((api) => {
94
- const { loaders } = api;
95
-
96
- return {
97
- command: 'similar <schema>',
98
- describe: 'Find similar types in a schema',
99
- builder(yargs) {
100
- return yargs
101
- .positional('schema', {
102
- describe: 'Point to a schema',
103
- type: 'string',
104
- demandOption: true,
105
- })
106
- .options({
107
- n: {
108
- alias: 'name',
109
- describe: 'Name of a type',
110
- type: 'string',
111
- },
112
- t: {
113
- alias: 'threshold',
114
- describe: 'Threshold of similarity ratio',
115
- type: 'number',
116
- },
117
- w: {
118
- alias: 'write',
119
- describe: 'Write a file with stats',
120
- type: 'string',
121
- },
122
- });
123
- },
124
- async handler(args) {
125
- const { headers, token } = parseGlobalArgs(args);
126
- const writePath = args.write;
127
- const type = args.name;
128
- const threshold = args.threshold;
129
- const apolloFederation = args.federation || false;
130
- const aws = args.aws || false;
131
- const method = args.method?.toUpperCase() || 'POST';
132
-
133
- const schema = await loaders.loadSchema(
134
- args.schema,
135
- {
136
- headers,
137
- token,
138
- method,
139
- },
140
- apolloFederation,
141
- aws,
142
- );
143
-
144
- return handler({ schema, writePath, type, threshold });
145
- },
146
- };
147
- });
148
-
149
- function indent(line: string, space: number): string {
150
- return line.padStart(line.length + space, ' ');
151
- }
152
-
153
- interface SimilarRecord {
154
- typename: string;
155
- rating: number;
156
- }
157
-
158
- interface SimilarResults {
159
- [typename: string]: SimilarRecord[];
160
- }
161
-
162
- function transformMap(similarMap: SimilarMap): SimilarResults {
163
- const results: SimilarResults = {};
164
-
165
- for (const typename in similarMap) {
166
- if (similarMap.hasOwnProperty(typename)) {
167
- const result = similarMap[typename];
168
-
169
- results[typename] = [];
170
-
171
- if (result.bestMatch) {
172
- results[typename].push(trasformResult(result.bestMatch));
173
- }
174
-
175
- if (result.ratings) {
176
- results[typename].push(...result.ratings.map(trasformResult));
177
- }
178
- }
179
- }
180
-
181
- return results;
182
- }
183
-
184
- function trasformResult(record: Rating): SimilarRecord {
185
- return {
186
- typename: record.target.typeId,
187
- rating: record.rating,
188
- };
189
- }
190
-
191
- function outputJSON(results: SimilarResults): string {
192
- return JSON.stringify(results);
193
- }
194
-
195
- function printResult(name: string, rating: number): string {
196
- const percentage = chalk.grey(`(${formatRating(rating)}%)`);
197
-
198
- return indent(`${printScale(rating)} ${percentage} ${name}`, 0);
199
- }
200
-
201
- function printScale(ratio: number): string {
202
- const percentage = Math.floor(ratio * 100);
203
- const levels = [0, 30, 50, 70, 90];
204
-
205
- return levels
206
- .map((level) => percentage >= level)
207
- .map((enabled) => (enabled ? figures.bullet : chalk.gray(figures.bullet)))
208
- .join('');
209
- }
210
-
211
- function formatRating(ratio: number): number {
212
- return Math.floor(ratio * 100);
213
- }