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

fix: make sure that custom parser is used if passed to process #1438

Merged
merged 2 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -56,6 +56,7 @@ export async function getGraphQLCache(
configDir: Uri,
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>,
config?: GraphQLConfig,
parser?: typeof parseDocument,
): Promise<GraphQLCacheInterface> {
let graphQLConfig =
config ?? ((await loadConfig({ rootDir: configDir })) as GraphQLConfig);
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 || parseDocument;
zth marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -120,6 +120,7 @@ export class MessageProcessor {
rootPath,
this._extensions,
this._graphQLConfig,
this._parser,
);
this._languageService = new GraphQLLanguageService(this._graphQLCache);

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