Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: repair CLI, handle all schema and LSP errors #1482

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export class GraphQLLanguageService {
// schema/fragment definitions, even the project configuration.
let queryHasExtensions = false;
const projectConfig = this.getConfigForURI(uri);
if (!projectConfig) {
return [];
}
const { schema: schemaPath, name: projectName, extensions } = projectConfig;

try {
Expand Down Expand Up @@ -208,9 +211,10 @@ export class GraphQLLanguageService {
}
/* eslint-enable no-implicit-coercion */
}
const schema = await this._graphQLCache
.getSchema(projectName, queryHasExtensions)
.catch(() => null);
const schema = await this._graphQLCache.getSchema(
projectName,
queryHasExtensions,
);

if (!schema) {
return [];
Expand Down
65 changes: 48 additions & 17 deletions packages/graphql-language-service-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ import {
import { Logger } from './Logger';
import { parseDocument } from './parseDocument';

type Options = {
// port for the LSP server to run on
export type ServerOptions = {
// port for the LSP server to run on. required if using method socket
port?: number;
// socket, streams, or node (ipc). if socket, port is required
// socket, streams, or node (ipc)
method?: 'socket' | 'stream' | 'node';
// the directory where graphql-config is found
configDir?: string;
Expand All @@ -58,17 +58,17 @@ type Options = {
config?: GraphQLConfig;
parser?: typeof parseDocument;
};
('graphql-language-service-types');

/**
* startServer - initialize LSP server with options
*
* @param options {Options} server initialization methods
* @param options {ServerOptions} server initialization methods
* @returns {Promise<void>}
*/
export default async function startServer(options: Options): Promise<void> {
export default async function startServer(
options: ServerOptions,
): Promise<void> {
const logger = new Logger();

if (options && options.method) {
let reader;
let writer;
Expand All @@ -94,15 +94,14 @@ export default async function startServer(options: Options): Promise<void> {
socket.close();
process.exit(0);
});
const connection = createMessageConnection(reader, writer, logger);
addHandlers(
connection,
const serverWithHandlers = initializeHandlers({
reader,
writer,
logger,
options.configDir,
options?.extensions ?? [],
options.config,
);
connection.listen();
options,
});

serverWithHandlers.listen();
})
.listen(port);
return;
Expand All @@ -116,17 +115,49 @@ export default async function startServer(options: Options): Promise<void> {
writer = new IPCMessageWriter(process);
break;
}

try {
const serverWithHandlers = initializeHandlers({
reader,
writer,
logger,
options,
});
return serverWithHandlers.listen();
} catch (err) {
logger.error('There was a Graphql LSP handler exception:');
logger.error(err);
}
}
}

function initializeHandlers({
reader,
writer,
logger,
options = {},
}: {
reader: SocketMessageReader | StreamMessageReader | IPCMessageReader;
writer: SocketMessageWriter | StreamMessageWriter | IPCMessageWriter;
logger: Logger;
options: ServerOptions;
}): MessageConnection {
try {
const connection = createMessageConnection(reader, writer, logger);
addHandlers(
connection,
logger,
options.configDir,
options?.extensions ?? [],
options?.extensions || [],
options.config,
options.parser,
options.fileExtensions,
);
connection.listen();
return connection;
} catch (err) {
logger.error('There was an error initializing the server connection');
logger.error(err);
process.exit(1);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/graphql-language-service/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const { argv } = yargs
'Can be one of: stream, node, socket.\n' +
'Will default to use a node IPC channel for communication.\n',
type: 'string',
default: 'node',
})
.option('p', {
alias: 'port',
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-language-service/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"composite": true,
"rootDir": "./src",
"outDir": "./dist"
"outDir": "./dist",
"target": "ES2017"
},
"references": [
{
Expand Down