Skip to content

Commit

Permalink
fix: make sure that custom parser is used if passed to process (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Mar 23, 2020
1 parent 6339ba7 commit 5e098a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 10 additions & 3 deletions packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {

export async function getGraphQLCache(
configDir: Uri,
parser: typeof parseDocument,
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>,
config?: GraphQLConfig,
): Promise<GraphQLCacheInterface> {
Expand All @@ -64,7 +65,7 @@ export async function getGraphQLCache(
graphQLConfig = await extension(graphQLConfig);
}
}
return new GraphQLCache(configDir, graphQLConfig);
return new GraphQLCache(configDir, graphQLConfig, parser);
}

export class GraphQLCache implements GraphQLCacheInterface {
Expand All @@ -75,15 +76,21 @@ export class GraphQLCache implements GraphQLCacheInterface {
_typeExtensionMap: Map<Uri, number>;
_fragmentDefinitionsCache: Map<Uri, Map<string, FragmentInfo>>;
_typeDefinitionsCache: Map<Uri, Map<string, ObjectTypeInfo>>;
_parser: typeof parseDocument;

constructor(configDir: Uri, graphQLConfig: GraphQLConfig) {
constructor(
configDir: Uri,
graphQLConfig: GraphQLConfig,
parser: typeof parseDocument,
) {
this._configDir = configDir;
this._graphQLConfig = graphQLConfig;
this._graphQLFileListCache = new Map();
this._schemaMap = new Map();
this._fragmentDefinitionsCache = new Map();
this._typeDefinitionsCache = new Map();
this._typeExtensionMap = new Map();
this._parser = parser;
}

getGraphQLConfig = (): GraphQLConfig => this._graphQLConfig;
Expand Down Expand Up @@ -797,7 +804,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
let queries: CachedContent[] = [];
if (content.trim().length !== 0) {
try {
queries = parseDocument(content, filePath);
queries = this._parser(content, filePath);
if (queries.length === 0) {
// still resolve with an empty ast
resolve({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class MessageProcessor {
this._extensions = extensions;
this._fileExtensions = fileExtensions;
this._graphQLConfig = config;
this._parser = parser || parseDocument;
this._parser = parser ?? parseDocument;
}

async handleInitializeRequest(
Expand Down Expand Up @@ -118,6 +118,7 @@ export class MessageProcessor {

this._graphQLCache = await getGraphQLCache(
rootPath,
this._parser,
this._extensions,
this._graphQLConfig,
);
Expand Down Expand Up @@ -160,7 +161,7 @@ export class MessageProcessor {
if ('text' in textDocument && textDocument.text) {
// textDocument/didSave does not pass in the text content.
// Only run the below function if text is passed in.
contents = parseDocument(textDocument.text, uri, this._fileExtensions);
contents = this._parser(textDocument.text, uri, this._fileExtensions);

this._invalidateCache(textDocument, uri, contents);
} else {
Expand Down Expand Up @@ -230,7 +231,7 @@ export class MessageProcessor {

// If it's a .js file, try parsing the contents to see if GraphQL queries
// exist. If not found, delete from the cache.
const contents = parseDocument(
const contents = this._parser(
contentChange.text,
uri,
this._fileExtensions,
Expand Down Expand Up @@ -449,7 +450,7 @@ export class MessageProcessor {
) {
const uri = change.uri;
const text: string = readFileSync(new URL(uri).pathname).toString();
const contents = parseDocument(text, uri, this._fileExtensions);
const contents = this._parser(text, uri, this._fileExtensions);

this._updateFragmentDefinition(uri, contents);
this._updateObjectTypeDefinition(uri, contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function wihtoutASTNode(definition: any) {
describe('GraphQLCache', () => {
const configDir = __dirname;
let graphQLRC;
let cache = new GraphQLCache(configDir, graphQLRC);
let cache = new GraphQLCache(configDir, graphQLRC, parseDocument);

beforeEach(async () => {
graphQLRC = await loadConfig({ rootDir: configDir });
cache = new GraphQLCache(configDir, graphQLRC);
cache = new GraphQLCache(configDir, graphQLRC, parseDocument);
});

afterEach(() => {
Expand All @@ -52,7 +52,11 @@ describe('GraphQLCache', () => {
};
};
const extensions = [extension];
const cacheWithExtensions = await getGraphQLCache(configDir, extensions);
const cacheWithExtensions = await getGraphQLCache(
configDir,
parseDocument,
extensions,
);
const config = cacheWithExtensions.getGraphQLConfig();
expect('extension' in config).toBe(true);
expect((config as any).extension).toBe('extension-used');
Expand Down

0 comments on commit 5e098a4

Please sign in to comment.