10
10
import { join } from 'node:path' ;
11
11
12
12
import { GraphQLConfig } from 'graphql-config' ;
13
- import { GraphQLLanguageService } from '../GraphQLLanguageService' ;
13
+ import {
14
+ EXTENSION_NAME ,
15
+ GraphQLLanguageService ,
16
+ } from '../GraphQLLanguageService' ;
14
17
import { SymbolKind } from 'vscode-languageserver-protocol' ;
15
18
import { Position } from 'graphql-language-service' ;
16
19
import { NoopLogger } from '../Logger' ;
17
20
18
21
const MOCK_CONFIG = {
19
22
filepath : join ( __dirname , '.graphqlrc.yml' ) ,
20
23
config : {
21
- schema : './__schema__/StarWarsSchema.graphql' ,
22
- documents : [ './queries/**' , '**/*.graphql' ] ,
24
+ projects : {
25
+ default : {
26
+ schema : './__schema__/StarWarsSchema.graphql' ,
27
+ documents : [ './queries/**' , '**/*.graphql' ] ,
28
+ } ,
29
+ another : {
30
+ schema : 'schema { query: Query } type Query { test: String }' ,
31
+ documents : [ './queries/**/*.ts' , './somewhere/**/*.ts' ] ,
32
+ extensions : {
33
+ [ EXTENSION_NAME ] : {
34
+ gqlTagOptions : {
35
+ annotationSuffix : 'test' ,
36
+ } ,
37
+ } ,
38
+ } ,
39
+ } ,
40
+ } ,
23
41
} ,
24
42
} ;
25
43
@@ -31,7 +49,7 @@ describe('GraphQLLanguageService', () => {
31
49
} ,
32
50
33
51
getGraphQLConfig ( ) {
34
- return new GraphQLConfig ( MOCK_CONFIG , [ ] ) ;
52
+ return new GraphQLConfig ( MOCK_CONFIG , [ ( ) => ( { name : EXTENSION_NAME } ) ] ) ;
35
53
} ,
36
54
37
55
getProjectForFile ( uri : string ) {
@@ -221,4 +239,35 @@ describe('GraphQLLanguageService', () => {
221
239
expect ( result [ 1 ] . location . range . end . line ) . toEqual ( 4 ) ;
222
240
expect ( result [ 1 ] . location . range . end . character ) . toEqual ( 5 ) ;
223
241
} ) ;
242
+
243
+ it ( 'finds the correct project for the given query' , ( ) => {
244
+ const getProjectName = ( query : string , path : string ) =>
245
+ languageService . getProjectForQuery ( query , path ) ?. name ;
246
+
247
+ const QUERY_NO_SUFFIX = '#graphql\n query { test }' ;
248
+ const QUERY_TEST_SUFFIX = '#graphql:test\n query { test }' ;
249
+
250
+ const pathThatMatchesBothProjects = './queries/test.ts' ;
251
+ const pathThatMatchesOnlyProjectAnother = './somewhere/test.ts' ;
252
+
253
+ // Matches path for both projects:
254
+ // #graphql => default
255
+ expect (
256
+ getProjectName ( QUERY_NO_SUFFIX , pathThatMatchesBothProjects ) ,
257
+ ) . toEqual ( 'default' ) ;
258
+ // #graphql:test => another
259
+ expect (
260
+ getProjectName ( QUERY_TEST_SUFFIX , pathThatMatchesBothProjects ) ,
261
+ ) . toEqual ( 'another' ) ;
262
+
263
+ // Only matches path for project 'another':
264
+ // #graphql => undefined
265
+ expect (
266
+ getProjectName ( QUERY_NO_SUFFIX , pathThatMatchesOnlyProjectAnother ) ,
267
+ ) . toEqual ( undefined ) ;
268
+ // #graphql:test => another
269
+ expect (
270
+ getProjectName ( QUERY_TEST_SUFFIX , pathThatMatchesOnlyProjectAnother ) ,
271
+ ) . toEqual ( 'another' ) ;
272
+ } ) ;
224
273
} ) ;
0 commit comments