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

Commit 818219d

Browse files
committed
WIP: Allow passing ParseOptions to parse() with GraphQL Language Server
I want to pass some ParseOptions. The obvious way to do this is to specify it in `.graphqlconfig` and make graphql-language-service pick this up and respect it. This is only a start. I haven't figured out the cleanest way to pass this to `GraphQLCache`. That will come next.
1 parent 0ac376c commit 818219d

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

packages/interface/src/GraphQLLanguageService.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class GraphQLLanguageService {
7979
const projectConfig = this._graphQLConfig.getConfigForFile(uri);
8080
const schemaPath = projectConfig.schemaPath;
8181
try {
82-
const queryAST = parse(query);
82+
const queryAST = parse(query, projectConfig.parseOptions);
8383
if (!schemaPath || uri !== schemaPath) {
8484
queryHasExtensions = queryAST.definitions.some(definition => {
8585
switch (definition.kind) {
@@ -121,6 +121,7 @@ export class GraphQLLanguageService {
121121
const fragmentDependencies = await this._graphQLCache.getFragmentDependencies(
122122
query,
123123
fragmentDefinitions,
124+
projectConfig,
124125
);
125126
const dependenciesSource = fragmentDependencies.reduce(
126127
(prev, cur) => `${prev} ${print(cur.definition)}`,
@@ -131,7 +132,7 @@ export class GraphQLLanguageService {
131132

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

156157
const schema = await this._graphQLCache
157-
.getSchema(projectConfig.projectName, queryHasExtensions)
158+
.getSchema(projectConfig, queryHasExtensions)
158159
.catch(() => null);
159160

160161
if (!schema) {
@@ -171,7 +172,7 @@ export class GraphQLLanguageService {
171172
): Promise<Array<CompletionItem>> {
172173
const projectConfig = this._graphQLConfig.getConfigForFile(filePath);
173174
const schema = await this._graphQLCache
174-
.getSchema(projectConfig.projectName)
175+
.getSchema(projectConfig)
175176
.catch(() => null);
176177

177178
if (schema) {
@@ -187,7 +188,7 @@ export class GraphQLLanguageService {
187188
): Promise<Hover.contents> {
188189
const projectConfig = this._graphQLConfig.getConfigForFile(filePath);
189190
const schema = await this._graphQLCache
190-
.getSchema(projectConfig.projectName)
191+
.getSchema(projectConfig)
191192
.catch(() => null);
192193

193194
if (schema) {
@@ -205,7 +206,7 @@ export class GraphQLLanguageService {
205206

206207
let ast;
207208
try {
208-
ast = parse(query);
209+
ast = parse(query, projectConfig.parseOptions);
209210
} catch (error) {
210211
return null;
211212
}

packages/server/src/GraphQLCache.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
8383
getFragmentDependencies = async (
8484
query: string,
8585
fragmentDefinitions: ?Map<string, FragmentInfo>,
86+
projectConfig: GraphQLProjectConfig,
8687
): Promise<Array<FragmentInfo>> => {
8788
// If there isn't context for fragment references,
8889
// return an empty array.
@@ -93,7 +94,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
9394
// Return an empty array.
9495
let parsedQuery;
9596
try {
96-
parsedQuery = parse(query);
97+
parsedQuery = parse(query, projectConfig.parseOptions);
9798
} catch (error) {
9899
return [];
99100
}
@@ -183,6 +184,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
183184
getObjectTypeDependencies = async (
184185
query: string,
185186
objectTypeDefinitions: ?Map<string, ObjectTypeInfo>,
187+
projectConfig: GraphQLProjectConfig,
186188
): Promise<Array<ObjectTypeInfo>> => {
187189
// If there isn't context for object type references,
188190
// return an empty array.
@@ -193,7 +195,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
193195
// Return an empty array.
194196
let parsedQuery;
195197
try {
196-
parsedQuery = parse(query);
198+
parsedQuery = parse(query, projectConfig.parseOptions);
197199
} catch (error) {
198200
return [];
199201
}
@@ -320,7 +322,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
320322
return;
321323
}
322324

323-
const fileAndContent = await this.promiseToReadGraphQLFile(filePath);
325+
const fileAndContent = await this.promiseToReadGraphQLFile(filePath, projectConfig);
324326
graphQLFileMap.set(filePath, {
325327
...fileAndContent,
326328
size,
@@ -337,6 +339,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
337339
{size, mtime},
338340
filePath,
339341
exists,
342+
projectConfig,
340343
),
341344
);
342345
}
@@ -413,9 +416,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
413416
metrics: {size: number, mtime: number},
414417
filePath: Uri,
415418
exists: boolean,
419+
projectConfig: GraphQLProjectConfig,
416420
): Promise<Map<Uri, GraphQLFileInfo>> {
417421
const fileAndContent = exists
418-
? await this.promiseToReadGraphQLFile(filePath)
422+
? await this.promiseToReadGraphQLFile(filePath, projectConfig)
419423
: null;
420424
const graphQLFileInfo = {...fileAndContent, ...metrics};
421425

@@ -438,11 +442,12 @@ export class GraphQLCache implements GraphQLCacheInterface {
438442
rootDir: Uri,
439443
filePath: Uri,
440444
contents: Array<CachedContent>,
445+
projectConfig: GraphQLProjectConfig,
441446
): Promise<void> {
442447
const cache = this._fragmentDefinitionsCache.get(rootDir);
443448
const asts = contents.map(({query}) => {
444449
try {
445-
return {ast: parse(query), query};
450+
return {ast: parse(query, projectConfig.parseOptions), query};
446451
} catch (error) {
447452
return {ast: null, query};
448453
}
@@ -475,9 +480,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
475480
rootDir: Uri,
476481
filePath: Uri,
477482
exists: boolean,
483+
projectConfig: GraphQLProjectConfig,
478484
): Promise<void> {
479485
const fileAndContent = exists
480-
? await this.promiseToReadGraphQLFile(filePath)
486+
? await this.promiseToReadGraphQLFile(filePath, projectConfig)
481487
: null;
482488
// In the case of fragment definitions, the cache could just map the
483489
// definition name to the parsed ast, whether or not it existed
@@ -489,19 +495,20 @@ export class GraphQLCache implements GraphQLCacheInterface {
489495
cache.delete(filePath);
490496
}
491497
} else if (fileAndContent && fileAndContent.queries) {
492-
this.updateFragmentDefinition(rootDir, filePath, fileAndContent.queries);
498+
this.updateFragmentDefinition(rootDir, filePath, fileAndContent.queries, projectConfig);
493499
}
494500
}
495501

496502
async updateObjectTypeDefinition(
497503
rootDir: Uri,
498504
filePath: Uri,
499505
contents: Array<CachedContent>,
506+
projectConfig: GraphQLProjectConfig,
500507
): Promise<void> {
501508
const cache = this._typeDefinitionsCache.get(rootDir);
502509
const asts = contents.map(({query}) => {
503510
try {
504-
return {ast: parse(query), query};
511+
return {ast: parse(query, projectConfig.parseOptions), query};
505512
} catch (error) {
506513
return {ast: null, query};
507514
}
@@ -538,6 +545,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
538545
rootDir: Uri,
539546
filePath: Uri,
540547
exists: boolean,
548+
projectConfig: GraphQLProjectConfig,
541549
): Promise<void> {
542550
const fileAndContent = exists
543551
? await this.promiseToReadGraphQLFile(filePath)
@@ -623,16 +631,10 @@ export class GraphQLCache implements GraphQLCacheInterface {
623631
}
624632

625633
getSchema = async (
626-
appName: ?string,
634+
projectConfig: GraphQLProjectConfig,
627635
queryHasExtensions?: ?boolean = false,
628636
): Promise<?GraphQLSchema> => {
629-
const projectConfig = this._graphQLConfig.getProjectConfig(appName);
630-
631-
if (!projectConfig) {
632-
return null;
633-
}
634-
635-
const projectName = appName || 'undefinedName';
637+
const projectName = projectConfig.projectName || 'undefinedName';
636638
const schemaPath = projectConfig.schemaPath;
637639
const endpointInfo = this._getDefaultEndpoint(projectConfig);
638640

@@ -678,7 +680,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
678680
const customDirectives = projectConfig.extensions.customDirectives;
679681
if (customDirectives && schema) {
680682
const directivesSDL = customDirectives.join('\n\n');
681-
schema = extendSchema(schema, parse(directivesSDL));
683+
schema = extendSchema(schema, parse(directivesSDL, projectConfig.parseOptions));
682684
}
683685

684686
if (!schema) {
@@ -826,6 +828,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
826828
*/
827829
promiseToReadGraphQLFile = (
828830
filePath: Uri,
831+
projectConfig: GraphQLProjectConfig,
829832
): Promise<{
830833
filePath: Uri,
831834
content: string,
@@ -850,7 +853,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
850853
return;
851854
}
852855

853-
queries.forEach(({query}) => asts.push(parse(query)));
856+
queries.forEach(({query}) => asts.push(parse(query, projectConfig.parseOptions)));
854857
} catch (_) {
855858
// If query has syntax errors, go ahead and still resolve
856859
// the filePath and the content, but leave ast empty.

packages/types/src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface GraphQLCache {
8787
getObjectTypeDependencies: (
8888
query: string,
8989
fragmentDefinitions: ?Map<string, ObjectTypeInfo>,
90+
projectConfig: GraphQLProjectConfig,
9091
) => Promise<Array<ObjectTypeInfo>>;
9192

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

9798
getObjectTypeDefinitions: (
98-
graphQLConfig: GraphQLProjectConfig,
99+
projectConfig: GraphQLProjectConfig,
99100
) => Promise<Map<string, ObjectTypeInfo>>;
100101

101102
+updateObjectTypeDefinition: (
102103
rootDir: Uri,
103104
filePath: Uri,
104105
contents: Array<CachedContent>,
106+
projectConfig: GraphQLProjectConfig,
105107
) => Promise<void>;
106108

107109
+updateObjectTypeDefinitionCache: (
108110
rootDir: Uri,
109111
filePath: Uri,
110112
exists: boolean,
113+
projectConfig: GraphQLProjectConfig,
111114
) => Promise<void>;
112115

113116
getFragmentDependencies: (
114117
query: string,
115118
fragmentDefinitions: ?Map<string, FragmentInfo>,
119+
projectConfig: GraphQLProjectConfig,
116120
) => Promise<Array<FragmentInfo>>;
117121

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

123127
getFragmentDefinitions: (
124-
graphQLConfig: GraphQLProjectConfig,
128+
projectConfig: GraphQLProjectConfig,
125129
) => Promise<Map<string, FragmentInfo>>;
126130

127131
+updateFragmentDefinition: (
128132
rootDir: Uri,
129133
filePath: Uri,
130134
contents: Array<CachedContent>,
135+
projectConfig: GraphQLProjectConfig,
131136
) => Promise<void>;
132137

133138
+updateFragmentDefinitionCache: (
134139
rootDir: Uri,
135140
filePath: Uri,
136141
exists: boolean,
142+
projectConfig: GraphQLProjectConfig,
137143
) => Promise<void>;
138144

139145
getSchema: (
140-
appName: ?string,
146+
projectConfig: GraphQLProjectConfig,
141147
queryHasExtensions?: ?boolean,
142148
) => Promise<?GraphQLSchema>;
143149

0 commit comments

Comments
 (0)