graphql-language-service-cli 3.2.3 → 3.2.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/CHANGELOG.md +11 -0
- package/package.json +6 -5
- package/src/__tests__/client-test.ts +45 -0
- package/src/__tests__/index-test.ts +16 -0
- package/src/cli.ts +145 -0
- package/src/client.ts +180 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# graphql-language-service-cli
|
|
2
2
|
|
|
3
|
+
## 3.2.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`bdd57312`](https://github.com/graphql/graphiql/commit/bdd573129844168749aba0aaa20e31b9da81aacf) [#2047](https://github.com/graphql/graphiql/pull/2047) Thanks [@willstott101](https://github.com/willstott101)! - Source code included in all packages to fix source maps. codemirror-graphql includes esm build in package.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`bdd57312`](https://github.com/graphql/graphiql/commit/bdd573129844168749aba0aaa20e31b9da81aacf)]:
|
|
10
|
+
- graphql-language-service@3.2.3
|
|
11
|
+
- graphql-language-service-server@2.7.4
|
|
12
|
+
- graphql-language-service-utils@2.6.2
|
|
13
|
+
|
|
3
14
|
## 3.2.3
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-language-service-cli",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.4",
|
|
4
4
|
"description": "An interface for building GraphQL language services for IDEs",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Hyohyeon Jeong <asiandrummer@fb.com>",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"files": [
|
|
19
19
|
"bin",
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
21
22
|
],
|
|
22
23
|
"keywords": [
|
|
23
24
|
"graphql",
|
|
@@ -31,9 +32,9 @@
|
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@babel/polyfill": "^7.8.7",
|
|
34
|
-
"graphql-language-service": "^3.2.
|
|
35
|
-
"graphql-language-service-server": "^2.7.
|
|
36
|
-
"graphql-language-service-utils": "^2.6.
|
|
35
|
+
"graphql-language-service": "^3.2.3",
|
|
36
|
+
"graphql-language-service-server": "^2.7.4",
|
|
37
|
+
"graphql-language-service-utils": "^2.6.2",
|
|
37
38
|
"yargs": "^16.2.0"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
import main from '../client';
|
|
10
|
+
|
|
11
|
+
describe('process.stderr.write', () => {
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
jest.restoreAllMocks();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('is passed information on error of string type', () => {
|
|
17
|
+
const argv = {
|
|
18
|
+
schemaPath: '...',
|
|
19
|
+
text: 'foo',
|
|
20
|
+
};
|
|
21
|
+
const mockStdErrWrite = jest
|
|
22
|
+
.spyOn(process.stderr, 'write')
|
|
23
|
+
.mockImplementation();
|
|
24
|
+
jest.spyOn(process, 'exit').mockImplementation();
|
|
25
|
+
const undefinedWithNewLine = /^undefined\n$/;
|
|
26
|
+
|
|
27
|
+
main('autocomplete', argv);
|
|
28
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
|
|
29
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(
|
|
30
|
+
expect.not.stringMatching(undefinedWithNewLine),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
main('outline', argv);
|
|
34
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
|
|
35
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(
|
|
36
|
+
expect.not.stringMatching(undefinedWithNewLine),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
main('validate', argv);
|
|
40
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(expect.any(String));
|
|
41
|
+
expect(mockStdErrWrite).toHaveBeenLastCalledWith(
|
|
42
|
+
expect.not.stringMatching(undefinedWithNewLine),
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
describe('blinking light demo', () => {
|
|
10
|
+
it('runs', () => {
|
|
11
|
+
// This is just a place holder for now as all the existing tests have moved
|
|
12
|
+
// down into the respective package directories. In the future, this will be
|
|
13
|
+
// the home of the integration tests.
|
|
14
|
+
expect(true).toEqual(true);
|
|
15
|
+
});
|
|
16
|
+
});
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
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 { Logger, 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
|
+
switch (command) {
|
|
111
|
+
case 'server':
|
|
112
|
+
process.on('uncaughtException', error => {
|
|
113
|
+
process.stdout.write(
|
|
114
|
+
'An error was thrown from GraphQL language service: ' + String(error),
|
|
115
|
+
);
|
|
116
|
+
process.exit(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const options: { [key: string]: any } = {};
|
|
120
|
+
if (argv && argv.port) {
|
|
121
|
+
options.port = argv.port;
|
|
122
|
+
}
|
|
123
|
+
if (argv && argv.method) {
|
|
124
|
+
options.method = argv.method;
|
|
125
|
+
}
|
|
126
|
+
if (argv && argv.configDir) {
|
|
127
|
+
options.configDir = argv.configDir;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
startServer(options);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
const logger = new Logger();
|
|
133
|
+
logger.error(error);
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
default: {
|
|
137
|
+
client(command as string, argv as { [key: string]: string });
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Exit the process when stream closes from remote end.
|
|
143
|
+
process.stdin.on('close', () => {
|
|
144
|
+
process.exit(0);
|
|
145
|
+
});
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
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 } from 'graphql';
|
|
11
|
+
|
|
12
|
+
import invariant from 'assert';
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import { buildSchema, buildClientSchema } from 'graphql';
|
|
15
|
+
import {
|
|
16
|
+
getAutocompleteSuggestions,
|
|
17
|
+
getDiagnostics,
|
|
18
|
+
getOutline,
|
|
19
|
+
} from 'graphql-language-service';
|
|
20
|
+
import { Position } from 'graphql-language-service-utils';
|
|
21
|
+
import path from '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 && 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 && 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 =
|
|
60
|
+
parseInt(argv.column, 10) || lines[lines.length - 1].length;
|
|
61
|
+
const point = new Position(row, column);
|
|
62
|
+
exitCode = _getAutocompleteSuggestions(text, point, schemaPath);
|
|
63
|
+
break;
|
|
64
|
+
case 'outline':
|
|
65
|
+
exitCode = _getOutline(text);
|
|
66
|
+
break;
|
|
67
|
+
case 'validate':
|
|
68
|
+
exitCode = _getDiagnostics(filePath, text, schemaPath);
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`Unknown command '${command}'`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
process.exit(exitCode);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface AutocompleteResultsMap {
|
|
78
|
+
[i: number]: CompletionItem;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function _getAutocompleteSuggestions(
|
|
82
|
+
queryText: string,
|
|
83
|
+
point: Position,
|
|
84
|
+
schemaPath: string,
|
|
85
|
+
): EXIT_CODE {
|
|
86
|
+
invariant(
|
|
87
|
+
schemaPath,
|
|
88
|
+
'A schema path is required to provide GraphQL autocompletion',
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const schema = schemaPath ? generateSchema(schemaPath) : null;
|
|
93
|
+
const resultArray = schema
|
|
94
|
+
? getAutocompleteSuggestions(schema, queryText, point)
|
|
95
|
+
: [];
|
|
96
|
+
const resultObject: AutocompleteResultsMap = resultArray.reduce(
|
|
97
|
+
(prev: AutocompleteResultsMap, cur, index) => {
|
|
98
|
+
prev[index] = cur;
|
|
99
|
+
return prev;
|
|
100
|
+
},
|
|
101
|
+
{},
|
|
102
|
+
);
|
|
103
|
+
process.stdout.write(JSON.stringify(resultObject, null, 2));
|
|
104
|
+
return GRAPHQL_SUCCESS_CODE;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
process.stderr.write((error?.stack ?? String(error)) + '\n');
|
|
107
|
+
return GRAPHQL_FAILURE_CODE;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface DiagnosticResultsMap {
|
|
112
|
+
[i: number]: Diagnostic;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function _getDiagnostics(
|
|
116
|
+
_filePath: string,
|
|
117
|
+
queryText: string,
|
|
118
|
+
schemaPath?: string,
|
|
119
|
+
): EXIT_CODE {
|
|
120
|
+
try {
|
|
121
|
+
// `schema` is not strictly requied as GraphQL diagnostics may still notify
|
|
122
|
+
// whether the query text is syntactically valid.
|
|
123
|
+
const schema = schemaPath ? generateSchema(schemaPath) : null;
|
|
124
|
+
const resultArray = getDiagnostics(queryText, schema);
|
|
125
|
+
const resultObject: DiagnosticResultsMap = resultArray.reduce(
|
|
126
|
+
(prev: DiagnosticResultsMap, cur, index) => {
|
|
127
|
+
prev[index] = cur;
|
|
128
|
+
return prev;
|
|
129
|
+
},
|
|
130
|
+
{},
|
|
131
|
+
);
|
|
132
|
+
process.stdout.write(JSON.stringify(resultObject, null, 2));
|
|
133
|
+
return GRAPHQL_SUCCESS_CODE;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
process.stderr.write((error?.stack ?? String(error)) + '\n');
|
|
136
|
+
return GRAPHQL_FAILURE_CODE;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function _getOutline(queryText: string): EXIT_CODE {
|
|
141
|
+
try {
|
|
142
|
+
const outline = getOutline(queryText);
|
|
143
|
+
if (outline) {
|
|
144
|
+
process.stdout.write(JSON.stringify(outline, null, 2));
|
|
145
|
+
} else {
|
|
146
|
+
throw Error('Error parsing or no outline tree found');
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
process.stderr.write((error?.stack ?? String(error)) + '\n');
|
|
150
|
+
return GRAPHQL_FAILURE_CODE;
|
|
151
|
+
}
|
|
152
|
+
return GRAPHQL_SUCCESS_CODE;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function ensureText(queryText: string, filePath: string): string {
|
|
156
|
+
let text = queryText;
|
|
157
|
+
// Always honor text argument over filePath.
|
|
158
|
+
// If text isn't available, try reading from the filePath.
|
|
159
|
+
if (!text) {
|
|
160
|
+
try {
|
|
161
|
+
text = fs.readFileSync(filePath, 'utf8');
|
|
162
|
+
} catch (error) {
|
|
163
|
+
throw new Error(error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return text;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function generateSchema(schemaPath: string): GraphQLSchema {
|
|
170
|
+
const schemaDSL = fs.readFileSync(schemaPath, 'utf8');
|
|
171
|
+
const schemaFileExt = path.extname(schemaPath);
|
|
172
|
+
switch (schemaFileExt) {
|
|
173
|
+
case '.graphql':
|
|
174
|
+
return buildSchema(schemaDSL);
|
|
175
|
+
case '.json':
|
|
176
|
+
return buildClientSchema(JSON.parse(schemaDSL));
|
|
177
|
+
default:
|
|
178
|
+
throw new Error('Unsupported schema file extention');
|
|
179
|
+
}
|
|
180
|
+
}
|