Skip to content

Commit

Permalink
prevent display of "unexpected EOF" parser errors
Browse files Browse the repository at this point in the history
this should work in most cases, except for cases where there are only comments and no actual graphql ops, fragments or SDL, but it's hard to come up with an efficient pass. this takes care of the situation where, when authoring a query like `gql\`|\``
  • Loading branch information
acao committed May 2, 2022
1 parent e189a88 commit 6db2844
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-oranges-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-server': patch
---

Stop reporting unnecessary EOF errors when authoring new queries
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,24 @@ export class GraphQLLanguageService {
}

public async getDiagnostics(
query: string,
document: string,
uri: Uri,
isRelayCompatMode?: boolean,
): Promise<Array<Diagnostic>> {
// Perform syntax diagnostics first, as this doesn't require
// schema/fragment definitions, even the project configuration.
let queryHasExtensions = false;
let documentHasExtensions = false;
const projectConfig = this.getConfigForURI(uri);
if (!projectConfig) {
// skip validation when there's nothing to validate, prevents noisy unexpected EOF errors
if (!projectConfig || !document || document.trim().length < 4) {
return [];
}
const { schema: schemaPath, name: projectName, extensions } = projectConfig;

try {
const queryAST = parse(query);
const documentAST = parse(document);
if (!schemaPath || uri !== schemaPath) {
queryHasExtensions = queryAST.definitions.some(definition => {
documentHasExtensions = documentAST.definitions.some(definition => {
switch (definition.kind) {
case OBJECT_TYPE_DEFINITION:
case INTERFACE_TYPE_DEFINITION:
Expand All @@ -152,7 +153,7 @@ export class GraphQLLanguageService {
});
}
} catch (error) {
const range = getRange(error.locations[0], query);
const range = getRange(error.locations[0], document);
return [
{
severity: DIAGNOSTIC_SEVERITY.Error,
Expand All @@ -164,13 +165,13 @@ export class GraphQLLanguageService {
}

// If there's a matching config, proceed to prepare to run validation
let source = query;
let source = document;
const fragmentDefinitions = await this._graphQLCache.getFragmentDefinitions(
projectConfig,
);

const fragmentDependencies = await this._graphQLCache.getFragmentDependencies(
query,
document,
fragmentDefinitions,
);

Expand Down Expand Up @@ -204,7 +205,7 @@ export class GraphQLLanguageService {
}
const schema = await this._graphQLCache.getSchema(
projectName,
queryHasExtensions,
documentHasExtensions,
);

if (!schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ describe('GraphQLLanguageService', () => {
);
});

it('avoids reporting validation errors when not enough characters are present', async () => {
const diagnostics = await languageService.getDiagnostics(
' \n \n typ\n\n',
'./queries/testQuery.graphql',
);
expect(diagnostics.length).toEqual(0);
});

it('still reports errors on empty anonymous op', async () => {
const diagnostics = await languageService.getDiagnostics(
' \n {\n \n}\n\n',
'./queries/testQuery.graphql',
);
expect(diagnostics.length).toEqual(1);
expect(diagnostics[0].message).toEqual(
'Syntax Error: Expected Name, found "}".',
);
});

it('runs definition service as expected', async () => {
const definitionQueryResult = await languageService.getDefinition(
'type Query { hero(episode: Episode): Character }',
Expand Down
3 changes: 1 addition & 2 deletions packages/monaco-graphql/src/GraphQLWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export class GraphQLWorker {
try {
const documentModel = this._getTextModel(uri);
const document = documentModel?.getValue();
if (!document) {
console.log('no document');
if (!document || document.trim().length < 4) {
return [];
}
const graphQLPosition = toGraphQLPosition(position);
Expand Down

0 comments on commit 6db2844

Please sign in to comment.