Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.

WIP: Allow passing ParseOptions to parse() with GraphQL Language Server #242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions packages/interface/src/GraphQLLanguageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class GraphQLLanguageService {
const projectConfig = this._graphQLConfig.getConfigForFile(uri);
const schemaPath = projectConfig.schemaPath;
try {
const queryAST = parse(query);
const queryAST = parse(query, projectConfig.parseOptions);
if (!schemaPath || uri !== schemaPath) {
queryHasExtensions = queryAST.definitions.some(definition => {
switch (definition.kind) {
Expand Down Expand Up @@ -121,6 +121,7 @@ export class GraphQLLanguageService {
const fragmentDependencies = await this._graphQLCache.getFragmentDependencies(
query,
fragmentDefinitions,
projectConfig,
);
const dependenciesSource = fragmentDependencies.reduce(
(prev, cur) => `${prev} ${print(cur.definition)}`,
Expand All @@ -131,7 +132,7 @@ export class GraphQLLanguageService {

let validationAst = null;
try {
validationAst = parse(source);
validationAst = parse(source, projectConfig.parseOptions);
} catch (error) {
// the query string is already checked to be parsed properly - errors
// from this parse must be from corrupted fragment dependencies.
Expand All @@ -154,7 +155,7 @@ export class GraphQLLanguageService {
}

const schema = await this._graphQLCache
.getSchema(projectConfig.projectName, queryHasExtensions)
.getSchema(projectConfig, queryHasExtensions)
.catch(() => null);

if (!schema) {
Expand All @@ -171,7 +172,7 @@ export class GraphQLLanguageService {
): Promise<Array<CompletionItem>> {
const projectConfig = this._graphQLConfig.getConfigForFile(filePath);
const schema = await this._graphQLCache
.getSchema(projectConfig.projectName)
.getSchema(projectConfig)
.catch(() => null);

if (schema) {
Expand All @@ -187,7 +188,7 @@ export class GraphQLLanguageService {
): Promise<Hover.contents> {
const projectConfig = this._graphQLConfig.getConfigForFile(filePath);
const schema = await this._graphQLCache
.getSchema(projectConfig.projectName)
.getSchema(projectConfig)
.catch(() => null);

if (schema) {
Expand All @@ -205,7 +206,7 @@ export class GraphQLLanguageService {

let ast;
try {
ast = parse(query);
ast = parse(query, projectConfig.parseOptions);
} catch (error) {
return null;
}
Expand Down
39 changes: 21 additions & 18 deletions packages/server/src/GraphQLCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
getFragmentDependencies = async (
query: string,
fragmentDefinitions: ?Map<string, FragmentInfo>,
projectConfig: GraphQLProjectConfig,
): Promise<Array<FragmentInfo>> => {
// If there isn't context for fragment references,
// return an empty array.
Expand All @@ -93,7 +94,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
// Return an empty array.
let parsedQuery;
try {
parsedQuery = parse(query);
parsedQuery = parse(query, projectConfig.parseOptions);
} catch (error) {
return [];
}
Expand Down Expand Up @@ -183,6 +184,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
getObjectTypeDependencies = async (
query: string,
objectTypeDefinitions: ?Map<string, ObjectTypeInfo>,
projectConfig: GraphQLProjectConfig,
): Promise<Array<ObjectTypeInfo>> => {
// If there isn't context for object type references,
// return an empty array.
Expand All @@ -193,7 +195,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
// Return an empty array.
let parsedQuery;
try {
parsedQuery = parse(query);
parsedQuery = parse(query, projectConfig.parseOptions);
} catch (error) {
return [];
}
Expand Down Expand Up @@ -320,7 +322,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
return;
}

const fileAndContent = await this.promiseToReadGraphQLFile(filePath);
const fileAndContent = await this.promiseToReadGraphQLFile(filePath, projectConfig);
graphQLFileMap.set(filePath, {
...fileAndContent,
size,
Expand All @@ -337,6 +339,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
{size, mtime},
filePath,
exists,
projectConfig,
),
);
}
Expand Down Expand Up @@ -413,9 +416,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
metrics: {size: number, mtime: number},
filePath: Uri,
exists: boolean,
projectConfig: GraphQLProjectConfig,
): Promise<Map<Uri, GraphQLFileInfo>> {
const fileAndContent = exists
? await this.promiseToReadGraphQLFile(filePath)
? await this.promiseToReadGraphQLFile(filePath, projectConfig)
: null;
const graphQLFileInfo = {...fileAndContent, ...metrics};

Expand All @@ -438,11 +442,12 @@ export class GraphQLCache implements GraphQLCacheInterface {
rootDir: Uri,
filePath: Uri,
contents: Array<CachedContent>,
projectConfig: GraphQLProjectConfig,
): Promise<void> {
const cache = this._fragmentDefinitionsCache.get(rootDir);
const asts = contents.map(({query}) => {
try {
return {ast: parse(query), query};
return {ast: parse(query, projectConfig.parseOptions), query};
} catch (error) {
return {ast: null, query};
}
Expand Down Expand Up @@ -475,9 +480,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
rootDir: Uri,
filePath: Uri,
exists: boolean,
projectConfig: GraphQLProjectConfig,
): Promise<void> {
const fileAndContent = exists
? await this.promiseToReadGraphQLFile(filePath)
? await this.promiseToReadGraphQLFile(filePath, projectConfig)
: null;
// In the case of fragment definitions, the cache could just map the
// definition name to the parsed ast, whether or not it existed
Expand All @@ -489,19 +495,20 @@ export class GraphQLCache implements GraphQLCacheInterface {
cache.delete(filePath);
}
} else if (fileAndContent && fileAndContent.queries) {
this.updateFragmentDefinition(rootDir, filePath, fileAndContent.queries);
this.updateFragmentDefinition(rootDir, filePath, fileAndContent.queries, projectConfig);
}
}

async updateObjectTypeDefinition(
rootDir: Uri,
filePath: Uri,
contents: Array<CachedContent>,
projectConfig: GraphQLProjectConfig,
): Promise<void> {
const cache = this._typeDefinitionsCache.get(rootDir);
const asts = contents.map(({query}) => {
try {
return {ast: parse(query), query};
return {ast: parse(query, projectConfig.parseOptions), query};
} catch (error) {
return {ast: null, query};
}
Expand Down Expand Up @@ -538,6 +545,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
rootDir: Uri,
filePath: Uri,
exists: boolean,
projectConfig: GraphQLProjectConfig,
): Promise<void> {
const fileAndContent = exists
? await this.promiseToReadGraphQLFile(filePath)
Expand Down Expand Up @@ -623,16 +631,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
}

getSchema = async (
appName: ?string,
projectConfig: GraphQLProjectConfig,
queryHasExtensions?: ?boolean = false,
): Promise<?GraphQLSchema> => {
const projectConfig = this._graphQLConfig.getProjectConfig(appName);

if (!projectConfig) {
return null;
}

const projectName = appName || 'undefinedName';
const projectName = projectConfig.projectName || 'undefinedName';
const schemaPath = projectConfig.schemaPath;
const endpointInfo = this._getDefaultEndpoint(projectConfig);

Expand Down Expand Up @@ -678,7 +680,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
const customDirectives = projectConfig.extensions.customDirectives;
if (customDirectives && schema) {
const directivesSDL = customDirectives.join('\n\n');
schema = extendSchema(schema, parse(directivesSDL));
schema = extendSchema(schema, parse(directivesSDL, projectConfig.parseOptions));
}

if (!schema) {
Expand Down Expand Up @@ -826,6 +828,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
*/
promiseToReadGraphQLFile = (
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<{
filePath: Uri,
content: string,
Expand All @@ -850,7 +853,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
return;
}

queries.forEach(({query}) => asts.push(parse(query)));
queries.forEach(({query}) => asts.push(parse(query, projectConfig.parseOptions)));
} catch (_) {
// If query has syntax errors, go ahead and still resolve
// the filePath and the content, but leave ast empty.
Expand Down
12 changes: 9 additions & 3 deletions packages/types/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface GraphQLCache {
getObjectTypeDependencies: (
query: string,
fragmentDefinitions: ?Map<string, ObjectTypeInfo>,
projectConfig: GraphQLProjectConfig,
) => Promise<Array<ObjectTypeInfo>>;

getObjectTypeDependenciesForAST: (
Expand All @@ -95,24 +96,27 @@ export interface GraphQLCache {
) => Promise<Array<ObjectTypeInfo>>;

getObjectTypeDefinitions: (
graphQLConfig: GraphQLProjectConfig,
projectConfig: GraphQLProjectConfig,
) => Promise<Map<string, ObjectTypeInfo>>;

+updateObjectTypeDefinition: (
rootDir: Uri,
filePath: Uri,
contents: Array<CachedContent>,
projectConfig: GraphQLProjectConfig,
) => Promise<void>;

+updateObjectTypeDefinitionCache: (
rootDir: Uri,
filePath: Uri,
exists: boolean,
projectConfig: GraphQLProjectConfig,
) => Promise<void>;

getFragmentDependencies: (
query: string,
fragmentDefinitions: ?Map<string, FragmentInfo>,
projectConfig: GraphQLProjectConfig,
) => Promise<Array<FragmentInfo>>;

getFragmentDependenciesForAST: (
Expand All @@ -121,23 +125,25 @@ export interface GraphQLCache {
) => Promise<Array<FragmentInfo>>;

getFragmentDefinitions: (
graphQLConfig: GraphQLProjectConfig,
projectConfig: GraphQLProjectConfig,
) => Promise<Map<string, FragmentInfo>>;

+updateFragmentDefinition: (
rootDir: Uri,
filePath: Uri,
contents: Array<CachedContent>,
projectConfig: GraphQLProjectConfig,
) => Promise<void>;

+updateFragmentDefinitionCache: (
rootDir: Uri,
filePath: Uri,
exists: boolean,
projectConfig: GraphQLProjectConfig,
) => Promise<void>;

getSchema: (
appName: ?string,
projectConfig: GraphQLProjectConfig,
queryHasExtensions?: ?boolean,
) => Promise<?GraphQLSchema>;

Expand Down