@graphql-inspector/introspect-command 3.1.0 → 3.1.3

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.
@@ -0,0 +1,129 @@
1
+ import { mockGraphQLServer } from '@graphql-inspector/testing';
2
+ import { LoadersRegistry } from '@graphql-inspector/loaders';
3
+ import loader from '@graphql-inspector/url-loader';
4
+ import { mockCommand } from '@graphql-inspector/commands';
5
+ import { mockLogger, unmockLogger } from '@graphql-inspector/logger';
6
+ import yargs from 'yargs';
7
+ import { buildSchema } from 'graphql';
8
+ import { unlinkSync, existsSync, readFileSync } from 'fs';
9
+ import createCommand from '../src';
10
+
11
+ function sleepFor(ms: number) {
12
+ return new Promise((resolve) => {
13
+ setTimeout(resolve, ms);
14
+ });
15
+ }
16
+
17
+ const schema = buildSchema(/* GraphQL */ `
18
+ type Post {
19
+ id: ID!
20
+ title: String!
21
+ createdAt: String!
22
+ }
23
+
24
+ type Query {
25
+ post: Post!
26
+ }
27
+ `);
28
+
29
+ const loaders = new LoadersRegistry();
30
+
31
+ loaders.register(loader);
32
+
33
+ const introspect = createCommand({
34
+ config: {
35
+ commands: [],
36
+ loaders: [],
37
+ },
38
+ loaders,
39
+ });
40
+
41
+ describe('introspect', () => {
42
+ let spyReporter: jest.SpyInstance;
43
+ let spyProcessCwd: jest.SpyInstance;
44
+
45
+ beforeEach(() => {
46
+ yargs.reset();
47
+
48
+ spyProcessCwd = jest
49
+ .spyOn(process, 'cwd')
50
+ .mockImplementation(() => __dirname);
51
+
52
+ spyReporter = jest.fn();
53
+ mockLogger(spyReporter as any);
54
+ });
55
+
56
+ afterEach(() => {
57
+ yargs.reset();
58
+ unmockLogger();
59
+ spyProcessCwd.mockRestore();
60
+ spyReporter.mockRestore();
61
+ try {
62
+ unlinkSync('graphql.schema.json');
63
+ } catch (e) {}
64
+ });
65
+
66
+ test('graphql api with port and ws in name using url-loader', async () => {
67
+ const done = mockGraphQLServer({
68
+ schema,
69
+ host: 'http://foo.ws:8020',
70
+ path: '/graphql',
71
+ });
72
+ await mockCommand(introspect, 'introspect http://foo.ws:8020/graphql');
73
+ await sleepFor(500);
74
+
75
+ done();
76
+ expect(existsSync('graphql.schema.json')).toBe(true);
77
+ });
78
+
79
+ test('saved to graphql files using url-loader', async () => {
80
+ const done = mockGraphQLServer({
81
+ schema,
82
+ host: 'https://example.com',
83
+ path: '/graphql',
84
+ });
85
+ await mockCommand(
86
+ introspect,
87
+ 'introspect https://example.com/graphql -w schema.graphql',
88
+ );
89
+ await sleepFor(500);
90
+
91
+ done();
92
+ expect(existsSync('schema.graphql')).toBe(true);
93
+
94
+ const printed = readFileSync('schema.graphql', {
95
+ encoding: 'utf-8',
96
+ });
97
+ unlinkSync('schema.graphql');
98
+
99
+ const builtSchema = buildSchema(printed);
100
+
101
+ expect(builtSchema.getQueryType().getFields()).toHaveProperty('post');
102
+ });
103
+
104
+ test('saved to graphql files using url-loader by GET method', async () => {
105
+ const done = mockGraphQLServer({
106
+ schema,
107
+ host: 'https://example.com',
108
+ path: '/graphql',
109
+ method: 'GET',
110
+ });
111
+ await mockCommand(
112
+ introspect,
113
+ 'introspect https://example.com/graphql -w schema.graphql --method GET',
114
+ );
115
+ await sleepFor(500);
116
+
117
+ done();
118
+ expect(existsSync('schema.graphql')).toBe(true);
119
+
120
+ const printed = readFileSync('schema.graphql', {
121
+ encoding: 'utf-8',
122
+ });
123
+ unlinkSync('schema.graphql');
124
+
125
+ const builtSchema = buildSchema(printed);
126
+
127
+ expect(builtSchema.getQueryType().getFields()).toHaveProperty('post');
128
+ });
129
+ });
File without changes
File without changes
File without changes
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@graphql-inspector/introspect-command",
3
+ "version": "3.1.3",
4
+ "description": "Introspects 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.3",
11
+ "@graphql-inspector/core": "3.1.3",
12
+ "@graphql-inspector/logger": "3.1.3",
13
+ "tslib": "^2.0.0"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "kamilkisiela/graphql-inspector",
18
+ "directory": "packages/commands/introspect"
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/package.json CHANGED
@@ -1,48 +1,51 @@
1
1
  {
2
2
  "name": "@graphql-inspector/introspect-command",
3
- "version": "3.1.0",
3
+ "version": "3.1.3",
4
4
  "description": "Introspects 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.0",
11
- "@graphql-inspector/core": "3.1.0",
12
- "@graphql-inspector/logger": "3.1.0",
13
- "tslib": "^2.0.0"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "kamilkisiela/graphql-inspector",
18
- "directory": "packages/commands/introspect"
19
- },
20
5
  "keywords": [
21
6
  "graphql",
22
7
  "graphql-inspector",
23
8
  "graphql-inspector-command",
24
9
  "tools"
25
10
  ],
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
+ },
26
28
  "author": {
27
29
  "name": "Kamil Kisiela",
28
30
  "email": "kamil.kisiela@gmail.com",
29
31
  "url": "https://github.com/kamilkisiela"
30
32
  },
31
33
  "license": "MIT",
32
- "main": "index.js",
33
- "module": "index.mjs",
34
- "typings": "index.d.ts",
35
- "typescript": {
36
- "definition": "index.d.ts"
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "kamilkisiela/graphql-inspector",
37
+ "directory": "packages/commands/introspect"
37
38
  },
38
- "exports": {
39
- ".": {
40
- "require": "./index.js",
41
- "import": "./index.mjs"
42
- },
43
- "./*": {
44
- "require": "./*.js",
45
- "import": "./*.mjs"
46
- }
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.3",
44
+ "@graphql-inspector/core": "3.1.3",
45
+ "@graphql-inspector/logger": "3.1.3",
46
+ "tslib": "^2.0.0"
47
+ },
48
+ "scripts": {
49
+ "prepack": "bob prepack"
47
50
  }
48
51
  }
package/src/index.ts ADDED
@@ -0,0 +1,111 @@
1
+ import {
2
+ createCommand,
3
+ GlobalArgs,
4
+ CommandFactory,
5
+ parseGlobalArgs,
6
+ } from '@graphql-inspector/commands';
7
+ import { Logger } from '@graphql-inspector/logger';
8
+ import { writeFileSync } from 'fs';
9
+ import { resolve, extname } from 'path';
10
+ import {
11
+ introspectionFromSchema,
12
+ lexicographicSortSchema,
13
+ printSchema,
14
+ GraphQLSchema,
15
+ } from 'graphql';
16
+
17
+ export { CommandFactory };
18
+
19
+ export function handler({
20
+ schema: unsortedSchema,
21
+ output,
22
+ comments,
23
+ }: {
24
+ schema: GraphQLSchema;
25
+ output: string;
26
+ comments: boolean;
27
+ }) {
28
+ const schema = lexicographicSortSchema(unsortedSchema);
29
+ const introspection = introspectionFromSchema(schema);
30
+ const filepath = resolve(process.cwd(), output);
31
+ let content: string;
32
+
33
+ switch (extname(output.toLowerCase())) {
34
+ case '.graphql':
35
+ case '.gql':
36
+ case '.gqls':
37
+ case '.graphqls':
38
+ content = (printSchema as any)(schema, {
39
+ commentDescriptions: comments,
40
+ });
41
+ break;
42
+ case '.json':
43
+ content = JSON.stringify(introspection, null, 2);
44
+ break;
45
+ default:
46
+ throw new Error('Only .graphql, .gql and .json files are supported');
47
+ }
48
+
49
+ writeFileSync(output, content!, {
50
+ encoding: 'utf-8',
51
+ });
52
+
53
+ Logger.success(`Saved to ${filepath}`);
54
+ }
55
+
56
+ export default createCommand<
57
+ {},
58
+ {
59
+ schema: string;
60
+ write?: string;
61
+ comments?: boolean;
62
+ } & GlobalArgs
63
+ >((api) => {
64
+ const { loaders } = api;
65
+
66
+ return {
67
+ command: 'introspect <schema>',
68
+ describe: 'Introspect a schema',
69
+ builder(yargs) {
70
+ return yargs
71
+ .positional('schema', {
72
+ describe: 'Point to a schema',
73
+ type: 'string',
74
+ demandOption: true,
75
+ })
76
+ .options({
77
+ w: {
78
+ alias: 'write',
79
+ describe: 'Write to a file',
80
+ type: 'string',
81
+ },
82
+ comments: {
83
+ describe: 'Use preceding comments as the description',
84
+ type: 'boolean',
85
+ },
86
+ })
87
+ .default('w', 'graphql.schema.json');
88
+ },
89
+ async handler(args) {
90
+ const { headers, token } = parseGlobalArgs(args);
91
+ const output = args.write!;
92
+ const comments = args.comments || false;
93
+ const apolloFederation = args.federation || false;
94
+ const aws = args.aws || false;
95
+ const method = args.method?.toUpperCase() || 'POST';
96
+
97
+ const schema = await loaders.loadSchema(
98
+ args.schema,
99
+ {
100
+ token,
101
+ headers,
102
+ method,
103
+ },
104
+ apolloFederation,
105
+ aws,
106
+ );
107
+
108
+ return handler({ schema, output, comments });
109
+ },
110
+ };
111
+ });