Skip to content

Commit

Permalink
use promise version of fs.readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Mar 6, 2023
1 parent 3dff003 commit 5829518
Showing 1 changed file with 44 additions and 50 deletions.
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.
return {
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
};
}
}
return { filePath, content, asts, queries, mtime: 0, size: 0 };
};
}

0 comments on commit 5829518

Please sign in to comment.