diff --git a/examples/cm6-graphql-parcel/package.json b/examples/cm6-graphql-parcel/package.json index a3fc8518106..f09b66dca99 100644 --- a/examples/cm6-graphql-parcel/package.json +++ b/examples/cm6-graphql-parcel/package.json @@ -26,7 +26,7 @@ "@codemirror/language": "^0.20.0", "codemirror-graphql": "file:../../packages/codemirror-graphql", "graphql": "^16.4.0", - "typescript": "^4.1.3" + "typescript": "^4.6.3" }, "devDependencies": { "parcel-bundler": "^1.12.4", diff --git a/examples/graphiql-parcel/package.json b/examples/graphiql-parcel/package.json index e7dd26eb3fa..4585e3dfc0d 100644 --- a/examples/graphiql-parcel/package.json +++ b/examples/graphiql-parcel/package.json @@ -26,7 +26,7 @@ "graphql": "^16.4.0", "react": "^17.0.2", "react-dom": "^17.0.2", - "typescript": "^3.4.4" + "typescript": "^4.6.3" }, "devDependencies": { "parcel": "^2.5.0", diff --git a/examples/monaco-graphql-webpack/package.json b/examples/monaco-graphql-webpack/package.json index e8ee6b58b1f..690015a28b7 100644 --- a/examples/monaco-graphql-webpack/package.json +++ b/examples/monaco-graphql-webpack/package.json @@ -35,7 +35,7 @@ "monaco-editor-webpack-plugin": "^5.0.0", "react-dom": "^17.0.2", "style-loader": "^1.1.3", - "typescript": "^4.1.3", + "typescript": "^4.6.3", "webpack": "4.42.1", "webpack-cli": "^3.3.11", "webpack-dev-server": "^3.10.3", diff --git a/package.json b/package.json index 68e6d15b0db..2f8446adef0 100644 --- a/package.json +++ b/package.json @@ -140,8 +140,11 @@ "serverless-http": "^2.7.0", "ts-jest": "^25.3.1", "typedoc": "^0.19.2", - "typescript": "^4.1.3", + "typescript": "^4.6.3", "whatwg-url": "^8.4.0", "wsrun": "^5.2.4" + }, + "resolutions": { + "graphql": "17.0.0-alpha.1" } } diff --git a/packages/codemirror-graphql/src/utils/jsonParse.ts b/packages/codemirror-graphql/src/utils/jsonParse.ts index b266a709789..92fded83cbf 100644 --- a/packages/codemirror-graphql/src/utils/jsonParse.ts +++ b/packages/codemirror-graphql/src/utils/jsonParse.ts @@ -155,8 +155,18 @@ function expect(str: string) { throw syntaxError(`Expected ${str} but found ${found}.`); } +type SyntaxErrorPosition = { start: number; end: number }; + +export class JSONSyntaxError extends Error { + readonly position: SyntaxErrorPosition; + constructor(message: string, position: SyntaxErrorPosition) { + super(message); + this.position = position; + } +} + function syntaxError(message: string) { - return { message, start, end }; + return new JSONSyntaxError(message, { start, end }); } function skip(k: string) { diff --git a/packages/codemirror-graphql/src/variables/lint.ts b/packages/codemirror-graphql/src/variables/lint.ts index ce6926c2687..11acd4ec725 100644 --- a/packages/codemirror-graphql/src/variables/lint.ts +++ b/packages/codemirror-graphql/src/variables/lint.ts @@ -18,6 +18,7 @@ import { } from 'graphql'; import jsonParse, { + JSONSyntaxError, ParseArrayOutput, ParseObjectOutput, ParseValueOutput, @@ -57,11 +58,11 @@ CodeMirror.registerHelper( let ast; try { ast = jsonParse(text); - } catch (syntaxError) { - if (syntaxError.stack) { - throw syntaxError; + } catch (error) { + if (error instanceof JSONSyntaxError) { + return [lintError(editor, error.position, error.message)]; } - return [lintError(editor, syntaxError, syntaxError.message)]; + throw error; } // If there are not yet known variables, do nothing. diff --git a/packages/graphiql-2-rfc-context/package.json b/packages/graphiql-2-rfc-context/package.json index d50e21f5957..23f8723154f 100644 --- a/packages/graphiql-2-rfc-context/package.json +++ b/packages/graphiql-2-rfc-context/package.json @@ -102,7 +102,7 @@ "start-server-and-test": "^1.10.11", "style-loader": "^1.1.3", "ts-loader": "^7.0.0", - "typescript": "^4.1.3", + "typescript": "^4.6.3", "webpack": "4.42.1", "webpack-bundle-analyzer": "^3.6.1", "webpack-cli": "^3.3.11", diff --git a/packages/graphiql/package.json b/packages/graphiql/package.json index c569c50b172..6f619f7778e 100644 --- a/packages/graphiql/package.json +++ b/packages/graphiql/package.json @@ -98,7 +98,7 @@ "style-loader": "^1.1.3", "subscriptions-transport-ws": "0.11.0", "ts-loader": "^7.0.0", - "typescript": "^4.1.3", + "typescript": "^4.6.3", "webpack": "^4.42.1", "webpack-bundle-analyzer": "^3.6.1", "webpack-cli": "^3.3.11", diff --git a/packages/graphiql/src/components/ToolbarButton.tsx b/packages/graphiql/src/components/ToolbarButton.tsx index b3043503996..45d32da5671 100644 --- a/packages/graphiql/src/components/ToolbarButton.tsx +++ b/packages/graphiql/src/components/ToolbarButton.tsx @@ -49,7 +49,11 @@ export class ToolbarButton extends React.Component< this.props.onClick(); this.setState({ error: null }); } catch (error) { - this.setState({ error }); + if (error instanceof Error) { + this.setState({ error }); + return; + } + throw error; } }; } diff --git a/packages/graphql-language-service-cli/src/cli.ts b/packages/graphql-language-service-cli/src/cli.ts index 61c3ec8e31e..f357f34bda9 100644 --- a/packages/graphql-language-service-cli/src/cli.ts +++ b/packages/graphql-language-service-cli/src/cli.ts @@ -130,7 +130,7 @@ switch (command) { startServer(options); } catch (error) { const logger = new Logger(); - logger.error(error); + logger.error(String(error)); } break; default: { diff --git a/packages/graphql-language-service-cli/src/client.ts b/packages/graphql-language-service-cli/src/client.ts index 5861ffd0a03..1b161279695 100644 --- a/packages/graphql-language-service-cli/src/client.ts +++ b/packages/graphql-language-service-cli/src/client.ts @@ -79,6 +79,14 @@ interface AutocompleteResultsMap { [i: number]: CompletionItem; } +function formatUnknownError(error: unknown) { + let message: string | undefined; + if (error instanceof Error) { + message = error.stack; + } + return message ?? String(error); +} + function _getAutocompleteSuggestions( queryText: string, point: Position, @@ -104,7 +112,7 @@ function _getAutocompleteSuggestions( process.stdout.write(JSON.stringify(resultObject, null, 2)); return GRAPHQL_SUCCESS_CODE; } catch (error) { - process.stderr.write((error?.stack ?? String(error)) + '\n'); + process.stderr.write(formatUnknownError(error) + '\n'); return GRAPHQL_FAILURE_CODE; } } @@ -133,7 +141,7 @@ function _getDiagnostics( process.stdout.write(JSON.stringify(resultObject, null, 2)); return GRAPHQL_SUCCESS_CODE; } catch (error) { - process.stderr.write((error?.stack ?? String(error)) + '\n'); + process.stderr.write(formatUnknownError(error) + '\n'); return GRAPHQL_FAILURE_CODE; } } @@ -147,7 +155,7 @@ function _getOutline(queryText: string): EXIT_CODE { throw Error('Error parsing or no outline tree found'); } } catch (error) { - process.stderr.write((error?.stack ?? String(error)) + '\n'); + process.stderr.write(formatUnknownError(error) + '\n'); return GRAPHQL_FAILURE_CODE; } return GRAPHQL_SUCCESS_CODE; @@ -161,7 +169,7 @@ function ensureText(queryText: string, filePath: string): string { try { text = fs.readFileSync(filePath, 'utf8'); } catch (error) { - throw new Error(error); + throw new Error(String(error)); } } return text; diff --git a/packages/graphql-language-service-server/src/GraphQLLanguageService.ts b/packages/graphql-language-service-server/src/GraphQLLanguageService.ts index 709f9ae8446..e8048a2fc99 100644 --- a/packages/graphql-language-service-server/src/GraphQLLanguageService.ts +++ b/packages/graphql-language-service-server/src/GraphQLLanguageService.ts @@ -14,6 +14,7 @@ import { TypeDefinitionNode, NamedTypeNode, ValidationRule, + GraphQLError, } from 'graphql'; import { @@ -153,15 +154,22 @@ export class GraphQLLanguageService { }); } } catch (error) { - const range = getRange(error.locations[0], document); - return [ - { - severity: DIAGNOSTIC_SEVERITY.Error, - message: error.message, - source: 'GraphQL: Syntax', - range, - }, - ]; + if (error instanceof GraphQLError) { + const range = getRange( + error.locations?.[0] ?? { column: 0, line: 0 }, + document, + ); + return [ + { + severity: DIAGNOSTIC_SEVERITY.Error, + message: error.message, + source: 'GraphQL: Syntax', + range, + }, + ]; + } + + throw error; } // If there's a matching config, proceed to prepare to run validation diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index dd0a4ee9128..7ecca494cf2 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -256,7 +256,7 @@ export class MessageProcessor { } } } catch (err) { - this._logger.warn(err); + this._logger.warn(String(err)); } // Here, we set the workspace settings in memory, @@ -834,7 +834,7 @@ export class MessageProcessor { await this._updateObjectTypeDefinition(uri, contents); } } catch (err) { - this._logger.error(err); + this._logger.error(String(err)); } } async _cacheSchemaFile( @@ -1002,7 +1002,7 @@ export class MessageProcessor { } } } catch (err) { - this._logger.error(err); + this._logger.error(String(err)); } } /** @@ -1043,7 +1043,7 @@ export class MessageProcessor { this._logger.error( `invalid/unknown file in graphql config documents entry:\n '${project.documents}'`, ); - this._logger.error(err); + this._logger.error(String(err)); } } /** diff --git a/packages/graphql-language-service-server/src/findGraphQLTags.ts b/packages/graphql-language-service-server/src/findGraphQLTags.ts index d0b80cb4950..370d96e6605 100644 --- a/packages/graphql-language-service-server/src/findGraphQLTags.ts +++ b/packages/graphql-language-service-server/src/findGraphQLTags.ts @@ -95,7 +95,7 @@ export function findGraphQLTags( logger.error( `Could not parse the ${type} file at ${uri} to extract the graphql tags:`, ); - logger.error(error); + logger.error(String(error)); return []; } const ast = parsedAST!; diff --git a/packages/graphql-language-service-server/src/startServer.ts b/packages/graphql-language-service-server/src/startServer.ts index 58ef94b3e9f..9bc8d8d2d47 100644 --- a/packages/graphql-language-service-server/src/startServer.ts +++ b/packages/graphql-language-service-server/src/startServer.ts @@ -211,7 +211,7 @@ export default async function startServer( serverWithHandlers.listen(); } catch (err) { logger.error('There was a Graphql LSP handler exception:'); - logger.error(err); + logger.error(String(err)); } } } @@ -236,7 +236,7 @@ async function initializeHandlers({ return connection; } catch (err) { logger.error('There was an error initializing the server connection'); - logger.error(err); + logger.error(String(err)); process.exit(1); } } diff --git a/packages/graphql-language-service/package.json b/packages/graphql-language-service/package.json index 3a96fae0bf3..6cd3f107a40 100644 --- a/packages/graphql-language-service/package.json +++ b/packages/graphql-language-service/package.json @@ -44,7 +44,7 @@ "lodash": "^4.17.15", "platform": "^1.3.5", "ts-node": "^8.10.2", - "typescript": "^4.1.3" + "typescript": "^4.6.3" }, "scripts": { "benchmark": "ts-node benchmark/index.ts" diff --git a/packages/graphql-language-service/src/interface/getDiagnostics.ts b/packages/graphql-language-service/src/interface/getDiagnostics.ts index 52f84bc88fc..0975fb795c5 100644 --- a/packages/graphql-language-service/src/interface/getDiagnostics.ts +++ b/packages/graphql-language-service/src/interface/getDiagnostics.ts @@ -81,15 +81,22 @@ export function getDiagnostics( try { ast = parse(query); } catch (error) { - const range = getRange(error.locations[0], query); - return [ - { - severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity, - message: error.message, - source: 'GraphQL: Syntax', - range, - }, - ]; + if (error instanceof GraphQLError) { + const range = getRange( + error.locations?.[0] ?? { line: 0, column: 0 }, + query, + ); + + return [ + { + severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity, + message: error.message, + source: 'GraphQL: Syntax', + range, + }, + ]; + } + throw error; } return validateQuery(ast, schema, customRules, isRelayCompatMode); diff --git a/packages/graphql-language-service/src/parser/Rules.ts b/packages/graphql-language-service/src/parser/Rules.ts index 016eba2f389..f253a691863 100644 --- a/packages/graphql-language-service/src/parser/Rules.ts +++ b/packages/graphql-language-service/src/parser/Rules.ts @@ -196,7 +196,7 @@ export const ParseRules: { [name: string]: ParseRule } = { StringValue: [ { style: 'string', - match: token => token.kind === 'String', + match: (token: Token) => token.kind === 'String', update(state: State, token: Token) { if (token.value.startsWith('"""')) { state.inBlockstring = !token.value.slice(3).endsWith('"""'); diff --git a/yarn.lock b/yarn.lock index 17ae27ad3c4..1192112d198 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12457,15 +12457,10 @@ graphql-ws@^5.4.1, graphql-ws@^5.5.5: resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.5.5.tgz#f375486d3f196e2a2527b503644693ae3a8670a9" integrity sha512-hvyIS71vs4Tu/yUYHPvGXsTgo0t3arU820+lT5VjZS2go0ewp2LqyCgxEN56CzOG7Iys52eRhHBiD1gGRdiQtw== -graphql@16.0.0-experimental-stream-defer.5: - version "16.0.0-experimental-stream-defer.5" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.0.0-experimental-stream-defer.5.tgz#d668566fd33053a054dc5367c38c20a4ac4e4224" - integrity sha512-bluMjYpxh3a1lwZuNP+FAaEDMWzccVhkv+STcw0ckB2EPtLRTYUdXQhF9YBbUHd3tZSAR7LXzsxIw2GZXhg5rw== - -graphql@^16.1.0, graphql@^16.4.0: - version "16.5.0" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.5.0.tgz#41b5c1182eaac7f3d47164fb247f61e4dfb69c85" - integrity sha512-qbHgh8Ix+j/qY+a/ZcJnFQ+j8ezakqPiHwPiZhV/3PgGlgf96QMBB5/f2rkiC9sgLoy/xvT6TSiaf2nTHJh5iA== +graphql@16.0.0-experimental-stream-defer.5, graphql@17.0.0-alpha.1, graphql@^16.1.0, graphql@^16.4.0: + version "17.0.0-alpha.1" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-17.0.0-alpha.1.tgz#e20c7ebd2c093274a01ccd7c37982f99a84dfe94" + integrity sha512-D59afb/GOC2ie47EONn574mVU5mjUPOyplPGME5US0rZBEIPLbzFdp8OBWX6uo9PqySDMnEjYp0OtfKjk0wjig== growly@^1.3.0: version "1.3.0" @@ -21533,11 +21528,6 @@ typedoc@^0.19.2: shelljs "^0.8.4" typedoc-default-themes "^0.11.4" -typescript@^4.1.3: - version "4.1.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" - integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg== - typescript@^4.6.3: version "4.6.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9"