Skip to content

Commit b20c7ed

Browse files
committed
Test queries for multiple schemas in the same file
1 parent ecd29f8 commit b20c7ed

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

packages/graphql-language-service-server/src/__tests__/GraphQLLanguageService-test.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,34 @@
1010
import { join } from 'node:path';
1111

1212
import { GraphQLConfig } from 'graphql-config';
13-
import { GraphQLLanguageService } from '../GraphQLLanguageService';
13+
import {
14+
EXTENSION_NAME,
15+
GraphQLLanguageService,
16+
} from '../GraphQLLanguageService';
1417
import { SymbolKind } from 'vscode-languageserver-protocol';
1518
import { Position } from 'graphql-language-service';
1619
import { NoopLogger } from '../Logger';
1720

1821
const MOCK_CONFIG = {
1922
filepath: join(__dirname, '.graphqlrc.yml'),
2023
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+
},
2341
},
2442
};
2543

@@ -31,7 +49,7 @@ describe('GraphQLLanguageService', () => {
3149
},
3250

3351
getGraphQLConfig() {
34-
return new GraphQLConfig(MOCK_CONFIG, []);
52+
return new GraphQLConfig(MOCK_CONFIG, [() => ({ name: EXTENSION_NAME })]);
3553
},
3654

3755
getProjectForFile(uri: string) {
@@ -221,4 +239,35 @@ describe('GraphQLLanguageService', () => {
221239
expect(result[1].location.range.end.line).toEqual(4);
222240
expect(result[1].location.range.end.character).toEqual(5);
223241
});
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+
});
224273
});

0 commit comments

Comments
 (0)