graphql-language-service-cli 4.0.0-canary-e0e2c15a.0 → 4.0.0-rc.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/README.md CHANGED
@@ -53,7 +53,7 @@ yarn global add graphql-language-service-cli
53
53
  with `npm`:
54
54
 
55
55
  ```sh
56
- npm i -g graphql-language-service-cli
56
+ npm install -g graphql-language-service-cli
57
57
  ```
58
58
 
59
59
  either will install the `graphql-lsp` bin globally
package/dist/cli.js CHANGED
@@ -1,7 +1,12 @@
1
- import yargs from 'yargs';
2
- import client from './client';
3
- import { startServer } from 'graphql-language-service-server';
4
- const { argv } = yargs
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
+ const yargs_1 = __importDefault(require("yargs"));
7
+ const client_1 = __importDefault(require("./client"));
8
+ const graphql_language_service_server_1 = require("graphql-language-service-server");
9
+ const { argv } = yargs_1.default
5
10
  .usage('GraphQL Language Service Command-Line Interface.\n' +
6
11
  'Usage: graphql-lsp <command> <file>\n' +
7
12
  ' [-h | --help]\n' +
@@ -95,12 +100,12 @@ if (command === 'server') {
95
100
  if (argv.configDir) {
96
101
  options.configDir = argv.configDir;
97
102
  }
98
- startServer(options).catch(error => {
103
+ (0, graphql_language_service_server_1.startServer)(options).catch(error => {
99
104
  process.stderr.write('An error was thrown from GraphQL language service: ' + String(error));
100
105
  });
101
106
  }
102
107
  else {
103
- client(command, argv);
108
+ (0, client_1.default)(command, argv);
104
109
  }
105
110
  process.stdin.on('close', () => {
106
111
  process.exit(0);
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,MAAM,MAAM,UAAU,CAAC;AAE9B,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B,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,KAAK,CAAC,EAAE;QACtC,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,qDAAqD,GAAG,MAAM,CAAC,KAAK,CAAC,CACtE,CAAC;IAGJ,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAA2B,EAAE,CAAC;IAC3C,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;IAC3B,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAChB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC/B,CAAC;IACD,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;IACrC,CAAC;IAED,IAAA,6CAAW,EAAC,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;QACjC,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,qDAAqD,GAAG,MAAM,CAAC,KAAK,CAAC,CACtE,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;KAAM,CAAC;IACN,IAAA,gBAAM,EAAC,OAAiB,EAAE,IAA8B,CAAC,CAAC;AAC5D,CAAC;AAGD,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;IAC7B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
package/dist/client.js CHANGED
@@ -1,14 +1,20 @@
1
- import { buildSchema, buildClientSchema } from 'graphql';
2
- import invariant from 'node:assert';
3
- import fs from 'node:fs';
4
- import { getAutocompleteSuggestions, getDiagnostics, getOutline, Position, } from 'graphql-language-service';
5
- import path from 'node:path';
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.default = main;
7
+ const graphql_1 = require("graphql");
8
+ const node_assert_1 = __importDefault(require("node:assert"));
9
+ const node_fs_1 = __importDefault(require("node:fs"));
10
+ const graphql_language_service_1 = require("graphql-language-service");
11
+ const node_path_1 = __importDefault(require("node:path"));
6
12
  const GRAPHQL_SUCCESS_CODE = 0;
7
13
  const GRAPHQL_FAILURE_CODE = 1;
8
- export default function main(command, argv) {
14
+ function main(command, argv) {
9
15
  var _a, _b;
10
16
  const filePath = (_a = argv.file) === null || _a === void 0 ? void 0 : _a.trim();
11
- invariant(argv.text || argv.file, 'A path to the GraphQL file or its contents is required.');
17
+ (0, node_assert_1.default)(argv.text || argv.file, 'A path to the GraphQL file or its contents is required.');
12
18
  const text = ensureText(argv.text, filePath);
13
19
  const schemaPath = (_b = argv.schemaPath) === null || _b === void 0 ? void 0 : _b.trim();
14
20
  let exitCode;
@@ -17,7 +23,7 @@ export default function main(command, argv) {
17
23
  const lines = text.split('\n');
18
24
  const row = parseInt(argv.row, 10) || lines.length - 1;
19
25
  const column = parseInt(argv.column, 10) || lines.at(-1).length;
20
- const point = new Position(row, column);
26
+ const point = new graphql_language_service_1.Position(row, column);
21
27
  exitCode = _getAutocompleteSuggestions(text, point, schemaPath);
22
28
  break;
23
29
  case 'outline':
@@ -39,11 +45,11 @@ function formatUnknownError(error) {
39
45
  return message !== null && message !== void 0 ? message : String(error);
40
46
  }
41
47
  function _getAutocompleteSuggestions(queryText, point, schemaPath) {
42
- invariant(schemaPath, 'A schema path is required to provide GraphQL autocompletion');
48
+ (0, node_assert_1.default)(schemaPath, 'A schema path is required to provide GraphQL autocompletion');
43
49
  try {
44
50
  const schema = schemaPath ? generateSchema(schemaPath) : null;
45
51
  const resultArray = schema
46
- ? getAutocompleteSuggestions(schema, queryText, point)
52
+ ? (0, graphql_language_service_1.getAutocompleteSuggestions)(schema, queryText, point)
47
53
  : [];
48
54
  const resultObject = resultArray.reduce((prev, cur, index) => {
49
55
  prev[index] = cur;
@@ -60,7 +66,7 @@ function _getAutocompleteSuggestions(queryText, point, schemaPath) {
60
66
  function _getDiagnostics(_filePath, queryText, schemaPath) {
61
67
  try {
62
68
  const schema = schemaPath ? generateSchema(schemaPath) : null;
63
- const resultArray = getDiagnostics(queryText, schema);
69
+ const resultArray = (0, graphql_language_service_1.getDiagnostics)(queryText, schema);
64
70
  const resultObject = resultArray.reduce((prev, cur, index) => {
65
71
  prev[index] = cur;
66
72
  return prev;
@@ -75,7 +81,7 @@ function _getDiagnostics(_filePath, queryText, schemaPath) {
75
81
  }
76
82
  function _getOutline(queryText) {
77
83
  try {
78
- const outline = getOutline(queryText);
84
+ const outline = (0, graphql_language_service_1.getOutline)(queryText);
79
85
  if (outline) {
80
86
  process.stdout.write(JSON.stringify(outline, null, 2));
81
87
  }
@@ -93,7 +99,7 @@ function ensureText(queryText, filePath) {
93
99
  let text = queryText;
94
100
  if (!text) {
95
101
  try {
96
- text = fs.readFileSync(filePath, 'utf8');
102
+ text = node_fs_1.default.readFileSync(filePath, 'utf8');
97
103
  }
98
104
  catch (error) {
99
105
  throw new Error(String(error));
@@ -102,13 +108,13 @@ function ensureText(queryText, filePath) {
102
108
  return text;
103
109
  }
104
110
  function generateSchema(schemaPath) {
105
- const schemaDSL = fs.readFileSync(schemaPath, 'utf8');
106
- const schemaFileExt = path.extname(schemaPath);
111
+ const schemaDSL = node_fs_1.default.readFileSync(schemaPath, 'utf8');
112
+ const schemaFileExt = node_path_1.default.extname(schemaPath);
107
113
  switch (schemaFileExt) {
108
114
  case '.graphql':
109
- return buildSchema(schemaDSL);
115
+ return (0, graphql_1.buildSchema)(schemaDSL);
110
116
  case '.json':
111
- return buildClientSchema(JSON.parse(schemaDSL));
117
+ return (0, graphql_1.buildClientSchema)(JSON.parse(schemaDSL));
112
118
  default:
113
119
  throw new Error('Unsupported schema file extension');
114
120
  }
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.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CAAC;AAED,SAAS,UAAU,CAAC,SAAiB,EAAE,QAAgB;IACrD,IAAI,IAAI,GAAG,SAAS,CAAC;IAGrB,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,IAAI,CAAC;YACH,IAAI,GAAG,iBAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC3C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,cAAc,CAAC,UAAkB;IACxC,MAAM,SAAS,GAAG,iBAAE,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACtD,MAAM,aAAa,GAAG,mBAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC/C,QAAQ,aAAa,EAAE,CAAC;QACtB,KAAK,UAAU;YACb,OAAO,IAAA,qBAAW,EAAC,SAAS,CAAC,CAAC;QAChC,KAAK,OAAO;YACV,OAAO,IAAA,2BAAiB,EAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;QAClD;YACE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACzD,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "graphql-language-service-cli",
3
- "version": "4.0.0-canary-e0e2c15a.0",
3
+ "version": "4.0.0-rc.0",
4
4
  "description": "An interface for building GraphQL language services for IDEs",
5
5
  "contributors": [
6
6
  "Hyohyeon Jeong <asiandrummer@fb.com>",
7
- "Lee Byron <lee@leebyron.com> (http://leebyron.com/)"
7
+ "Lee Byron <lee@leebyron.com> (https://leebyron.com)"
8
8
  ],
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "http://github.com/graphql/graphiql",
11
+ "url": "https://github.com/graphql/graphiql",
12
12
  "directory": "packages/graphql-language-service-cli"
13
13
  },
14
14
  "homepage": "https://github.com/graphql/graphiql/tree/main/packages/graphql-language-service-cli#readme",
@@ -18,13 +18,11 @@
18
18
  "bin": {
19
19
  "graphql-lsp": "./bin/graphql.js"
20
20
  },
21
- "engines": {
22
- "node": ">=16"
23
- },
24
21
  "license": "MIT",
25
22
  "files": [
26
23
  "bin",
27
- "dist"
24
+ "dist",
25
+ "src"
28
26
  ],
29
27
  "keywords": [
30
28
  "graphql",
@@ -34,16 +32,22 @@
34
32
  "LSP"
35
33
  ],
36
34
  "peerDependencies": {
37
- "graphql": "^15.5.0 || ^16.0.0"
35
+ "graphql": "^16.11.0 || ^17.0.0"
38
36
  },
39
37
  "dependencies": {
40
- "@types/yargs": "^16.0.5",
41
38
  "@babel/polyfill": "^7.12.1",
42
- "graphql-language-service": "^6.0.0-canary-e0e2c15a.0",
43
- "graphql-language-service-server": "^3.0.0-canary-e0e2c15a.0",
39
+ "@types/yargs": "16.0.5",
40
+ "graphql-language-service": "6.0.0-rc.0",
41
+ "graphql-language-service-server": "3.0.0-rc.0",
44
42
  "yargs": "^16.2.0"
45
43
  },
46
44
  "devDependencies": {
47
- "graphql": "^16.4.0"
45
+ "@typescript/native-preview": "^7.0.0-dev.20260707.2",
46
+ "graphql": "^16.14.2"
47
+ },
48
+ "scripts": {
49
+ "build": "tsgo -p tsconfig.json && tsgo -p tsconfig.esm.json",
50
+ "types:check": "tsgo --noEmit",
51
+ "test": "vitest run"
48
52
  }
49
- }
53
+ }
@@ -0,0 +1,46 @@
1
+ import { describe, it, expect, vi, afterEach } from 'vitest';
2
+ /**
3
+ * Copyright (c) 2021 GraphQL Contributors
4
+ * All rights reserved.
5
+ *
6
+ * This source code is licensed under the license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+
10
+ import main from '../client';
11
+
12
+ describe('process.stderr.write', () => {
13
+ afterEach(() => {
14
+ vi.restoreAllMocks();
15
+ });
16
+
17
+ it('is passed information on error of string type', () => {
18
+ const argv = {
19
+ schemaPath: '...',
20
+ text: 'foo',
21
+ };
22
+ const mockStdErrWrite = vi
23
+ .spyOn(process.stderr, 'write')
24
+ .mockImplementation();
25
+ vi.spyOn(process, 'exit').mockImplementation(() => {});
26
+ const undefinedWithNewLine = /^undefined\n$/;
27
+
28
+ main('autocomplete', argv);
29
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
30
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(
31
+ expect.not.stringMatching(undefinedWithNewLine),
32
+ );
33
+
34
+ main('outline', argv);
35
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
36
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(
37
+ expect.not.stringMatching(undefinedWithNewLine),
38
+ );
39
+
40
+ main('validate', argv);
41
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
42
+ expect(mockStdErrWrite).toHaveBeenLastCalledWith(
43
+ expect.not.stringMatching(undefinedWithNewLine),
44
+ );
45
+ });
46
+ });
@@ -0,0 +1,17 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ /**
3
+ * Copyright (c) 2021 GraphQL Contributors
4
+ * All rights reserved.
5
+ *
6
+ * This source code is licensed under the license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+
10
+ describe('blinking light demo', () => {
11
+ it('runs', () => {
12
+ // This is just a placeholder for now as all the existing tests have moved
13
+ // down into the respective package directories. In the future, this will be
14
+ // the home of the integration tests.
15
+ expect(true).toEqual(true);
16
+ });
17
+ });
package/src/cli.ts ADDED
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Copyright (c) 2021 GraphQL Contributors
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+
10
+ import yargs from 'yargs';
11
+ import client from './client';
12
+
13
+ import { startServer } from 'graphql-language-service-server';
14
+
15
+ const { argv } = yargs
16
+ .usage(
17
+ 'GraphQL Language Service Command-Line Interface.\n' +
18
+ 'Usage: graphql-lsp <command> <file>\n' +
19
+ ' [-h | --help]\n' +
20
+ ' [-c | --configDir] {configDir}\n' +
21
+ ' [-t | --text] {textBuffer}\n' +
22
+ ' [-f | --file] {filePath}\n' +
23
+ ' [-s | --schema] {schemaPath}\n' +
24
+ ' [-m | --method] {method}\n' +
25
+ ' [-p | --port] {port}\n' +
26
+ '\n At least one command is required.\n',
27
+ )
28
+ .help('h')
29
+ .alias('h', 'help')
30
+ .strict()
31
+ .recommendCommands()
32
+ .demandCommand(
33
+ 1,
34
+ 'At least one command is required.\n' +
35
+ 'Commands: "server, validate, autocomplete, outline"\n',
36
+ )
37
+ .command('server', 'GraphQL language server service')
38
+ .command('validate', 'Validates the query')
39
+ .command('autocomplete', 'Get autocomplete suggestions')
40
+ .command('outline', 'Get outline')
41
+ .option('t', {
42
+ alias: 'text',
43
+ describe:
44
+ 'Text buffer to perform GraphQL diagnostics on.\n' +
45
+ 'Will defer to --file option if omitted.\n' +
46
+ 'Overrides the --file option, if any.\n',
47
+ type: 'string',
48
+ })
49
+ .option('f', {
50
+ alias: 'file',
51
+ describe:
52
+ 'File path to perform GraphQL diagnostics on.\n' +
53
+ 'Will be ignored if --text option is supplied.\n',
54
+ type: 'string',
55
+ })
56
+ .option('row', {
57
+ describe:
58
+ 'A row number from the cursor location for ' +
59
+ 'GraphQL autocomplete suggestions.\n' +
60
+ 'If omitted, the last row number will be used.\n',
61
+ type: 'number',
62
+ })
63
+ .option('column', {
64
+ describe:
65
+ 'A column number from the cursor location for ' +
66
+ 'GraphQL autocomplete suggestions.\n' +
67
+ 'If omitted, the last column number will be used.\n',
68
+ type: 'number',
69
+ })
70
+ .option('c', {
71
+ alias: 'configDir',
72
+ describe:
73
+ 'Path to the .graphqlrc configuration file.\n' +
74
+ 'Walks up the directory tree from the provided config directory, or ' +
75
+ 'the current working directory, until a .graphqlrc is found or ' +
76
+ 'the root directory is found.\n',
77
+ type: 'string',
78
+ })
79
+ .option('m', {
80
+ alias: 'method',
81
+ describe:
82
+ 'A IPC communication method between client and server.\n' +
83
+ 'Can be one of: stream, node, socket.\n' +
84
+ 'Will default to use a node IPC channel for communication.\n',
85
+ type: 'string',
86
+ default: 'node',
87
+ })
88
+ .option('p', {
89
+ alias: 'port',
90
+ describe:
91
+ 'Port number to communicate via socket.\n' +
92
+ 'The port number of a service running inside the IDE that the language ' +
93
+ 'service should connect to.\n' +
94
+ 'Required if the client communicates via socket connection.\n',
95
+ type: 'number',
96
+ })
97
+ .option('s', {
98
+ alias: 'schemaPath',
99
+ describe: 'a path to schema DSL file\n',
100
+ type: 'string',
101
+ });
102
+
103
+ const command = argv._.pop();
104
+
105
+ if (!command) {
106
+ process.stdout.write('no command supplied');
107
+ process.exit(0);
108
+ }
109
+
110
+ if (command === 'server') {
111
+ process.on('uncaughtException', error => {
112
+ process.stderr.write(
113
+ 'An error was thrown from GraphQL language service: ' + String(error),
114
+ );
115
+ // don't exit at all if there is an uncaughtException
116
+ // process.exit(0);
117
+ });
118
+
119
+ const options: { [key: string]: any } = {};
120
+ if (argv.port) {
121
+ options.port = argv.port;
122
+ }
123
+ if (argv.method) {
124
+ options.method = argv.method;
125
+ }
126
+ if (argv.configDir) {
127
+ options.configDir = argv.configDir;
128
+ }
129
+ // eslint-disable-next-line promise/prefer-await-to-then -- don't know if I can use top level await here
130
+ startServer(options).catch(error => {
131
+ process.stderr.write(
132
+ 'An error was thrown from GraphQL language service: ' + String(error),
133
+ );
134
+ });
135
+ } else {
136
+ client(command as string, argv as Record<string, string>);
137
+ }
138
+
139
+ // Exit the process when stream closes from remote end.
140
+ process.stdin.on('close', () => {
141
+ process.exit(0);
142
+ });
package/src/client.ts ADDED
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Copyright (c) 2021 GraphQL Contributors
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+
10
+ import { GraphQLSchema, buildSchema, buildClientSchema } from 'graphql';
11
+
12
+ import invariant from 'node:assert';
13
+ import fs from 'node:fs';
14
+ import {
15
+ getAutocompleteSuggestions,
16
+ getDiagnostics,
17
+ getOutline,
18
+ Position,
19
+ } from 'graphql-language-service';
20
+
21
+ import path from 'node:path';
22
+
23
+ import type { CompletionItem, Diagnostic } from 'graphql-language-service';
24
+
25
+ const GRAPHQL_SUCCESS_CODE = 0;
26
+ const GRAPHQL_FAILURE_CODE = 1;
27
+
28
+ type EXIT_CODE = 0 | 1;
29
+
30
+ /**
31
+ * Performs GraphQL language service features with provided arguments from
32
+ * the command-line interface.
33
+ *
34
+ * `autocomplete`: returns GraphQL autocomplete suggestions at the cursor
35
+ * location provided, or at the end of the query text.
36
+ * `outline`: returns GraphQL query outline information.
37
+ * `validate`: performs GraphQL query lint/validations and returns the results.
38
+ * Query validation is only performed if a schema path is supplied.
39
+ */
40
+
41
+ export default function main(
42
+ command: string,
43
+ argv: { [key: string]: string },
44
+ ): void {
45
+ const filePath = argv.file?.trim();
46
+ invariant(
47
+ argv.text || argv.file,
48
+ 'A path to the GraphQL file or its contents is required.',
49
+ );
50
+
51
+ const text = ensureText(argv.text, filePath);
52
+ const schemaPath = argv.schemaPath?.trim();
53
+
54
+ let exitCode;
55
+ switch (command) {
56
+ case 'autocomplete':
57
+ const lines = text.split('\n');
58
+ const row = parseInt(argv.row, 10) || lines.length - 1;
59
+ const column = parseInt(argv.column, 10) || lines.at(-1)!.length;
60
+ const point = new Position(row, column);
61
+ exitCode = _getAutocompleteSuggestions(text, point, schemaPath);
62
+ break;
63
+ case 'outline':
64
+ exitCode = _getOutline(text);
65
+ break;
66
+ case 'validate':
67
+ exitCode = _getDiagnostics(filePath, text, schemaPath);
68
+ break;
69
+ default:
70
+ throw new Error(`Unknown command '${command}'`);
71
+ }
72
+
73
+ process.exit(exitCode);
74
+ }
75
+
76
+ interface AutocompleteResultsMap {
77
+ [i: number]: CompletionItem;
78
+ }
79
+
80
+ function formatUnknownError(error: unknown) {
81
+ let message: string | undefined;
82
+ if (error instanceof Error) {
83
+ message = error.stack;
84
+ }
85
+ return message ?? String(error);
86
+ }
87
+
88
+ function _getAutocompleteSuggestions(
89
+ queryText: string,
90
+ point: Position,
91
+ schemaPath: string,
92
+ ): EXIT_CODE {
93
+ invariant(
94
+ schemaPath,
95
+ 'A schema path is required to provide GraphQL autocompletion',
96
+ );
97
+
98
+ try {
99
+ const schema = schemaPath ? generateSchema(schemaPath) : null;
100
+ const resultArray = schema
101
+ ? getAutocompleteSuggestions(schema, queryText, point)
102
+ : [];
103
+ const resultObject: AutocompleteResultsMap = resultArray.reduce(
104
+ (prev: AutocompleteResultsMap, cur, index) => {
105
+ prev[index] = cur;
106
+ return prev;
107
+ },
108
+ {},
109
+ );
110
+ process.stdout.write(JSON.stringify(resultObject, null, 2));
111
+ return GRAPHQL_SUCCESS_CODE;
112
+ } catch (error) {
113
+ process.stderr.write(formatUnknownError(error) + '\n');
114
+ return GRAPHQL_FAILURE_CODE;
115
+ }
116
+ }
117
+
118
+ interface DiagnosticResultsMap {
119
+ [i: number]: Diagnostic;
120
+ }
121
+
122
+ function _getDiagnostics(
123
+ _filePath: string,
124
+ queryText: string,
125
+ schemaPath?: string,
126
+ ): EXIT_CODE {
127
+ try {
128
+ // `schema` is not strictly required as GraphQL diagnostics may still notify
129
+ // whether the query text is syntactically valid.
130
+ const schema = schemaPath ? generateSchema(schemaPath) : null;
131
+ const resultArray = getDiagnostics(queryText, schema);
132
+ const resultObject: DiagnosticResultsMap = resultArray.reduce(
133
+ (prev: DiagnosticResultsMap, cur, index) => {
134
+ prev[index] = cur;
135
+ return prev;
136
+ },
137
+ {},
138
+ );
139
+ process.stdout.write(JSON.stringify(resultObject, null, 2));
140
+ return GRAPHQL_SUCCESS_CODE;
141
+ } catch (error) {
142
+ process.stderr.write(formatUnknownError(error) + '\n');
143
+ return GRAPHQL_FAILURE_CODE;
144
+ }
145
+ }
146
+
147
+ function _getOutline(queryText: string): EXIT_CODE {
148
+ try {
149
+ const outline = getOutline(queryText);
150
+ if (outline) {
151
+ process.stdout.write(JSON.stringify(outline, null, 2));
152
+ } else {
153
+ throw new Error('Error parsing or no outline tree found');
154
+ }
155
+ } catch (error) {
156
+ process.stderr.write(formatUnknownError(error) + '\n');
157
+ return GRAPHQL_FAILURE_CODE;
158
+ }
159
+ return GRAPHQL_SUCCESS_CODE;
160
+ }
161
+
162
+ function ensureText(queryText: string, filePath: string): string {
163
+ let text = queryText;
164
+ // Always honor text argument over filePath.
165
+ // If text isn't available, try reading from the filePath.
166
+ if (!text) {
167
+ try {
168
+ text = fs.readFileSync(filePath, 'utf8');
169
+ } catch (error) {
170
+ throw new Error(String(error));
171
+ }
172
+ }
173
+ return text;
174
+ }
175
+
176
+ function generateSchema(schemaPath: string): GraphQLSchema {
177
+ const schemaDSL = fs.readFileSync(schemaPath, 'utf8');
178
+ const schemaFileExt = path.extname(schemaPath);
179
+ switch (schemaFileExt) {
180
+ case '.graphql':
181
+ return buildSchema(schemaDSL);
182
+ case '.json':
183
+ return buildClientSchema(JSON.parse(schemaDSL));
184
+ default:
185
+ throw new Error('Unsupported schema file extension');
186
+ }
187
+ }