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

use promise version of fs.readFile #3084

Merged
merged 1 commit into from
Mar 8, 2023
Merged
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
94 changes: 44 additions & 50 deletions packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
} from 'graphql-language-service';

import * as fs from 'node:fs';
import { readFile } from 'node:fs/promises';
import { GraphQLSchema, Kind, extendSchema, parse, visit } from 'graphql';
import nullthrows from 'nullthrows';

Expand Down Expand Up @@ -789,57 +790,50 @@ export class GraphQLCache implements GraphQLCacheInterface {
* Returns a Promise to read a GraphQL file and return a GraphQL metadata
* including a parsed AST.
*/
promiseToReadGraphQLFile = (filePath: Uri): Promise<GraphQLFileInfo> => {
return new Promise((resolve, reject) =>
fs.readFile(URI.parse(filePath).fsPath, 'utf8', (error, content) => {
if (error) {
reject(error);
return;
}

const asts: DocumentNode[] = [];
let queries: CachedContent[] = [];
if (content.trim().length !== 0) {
try {
queries = this._parser(content, filePath);
if (queries.length === 0) {
// still resolve with an empty ast
resolve({
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
});
return;
}
promiseToReadGraphQLFile = async (
filePath: Uri,
): Promise<GraphQLFileInfo> => {
const content = await readFile(URI.parse(filePath).fsPath, 'utf8');

queries.forEach(({ query }) => asts.push(parse(query)));
resolve({
filePath,
content,
asts,
queries,
mtime: 0,
size: 0,
});
} catch {
// If query has syntax errors, go ahead and still resolve
// the filePath and the content, but leave ast empty.
resolve({
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
});
return;
}
const asts: DocumentNode[] = [];
let queries: CachedContent[] = [];
if (content.trim().length !== 0) {
try {
queries = this._parser(content, filePath);
if (queries.length === 0) {
// still resolve with an empty ast
return {
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
};
}
resolve({ filePath, content, asts, queries, mtime: 0, size: 0 });
}),
);

queries.forEach(({ query }) => asts.push(parse(query)));
return {
filePath,
content,
asts,
queries,
mtime: 0,
size: 0,
};
} catch {
// If query has syntax errors, go ahead and still resolve
// the filePath and the content, but leave ast empty.
Comment on lines +825 to +826
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If query has syntax errors, go ahead and still resolve
// the filePath and the content, but leave ast empty.
// If query has syntax errors, resolve the filePath
// and the content anyway, but leave ast empty.

return {
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
};
}
}
return { filePath, content, asts, queries, mtime: 0, size: 0 };
};
}