Skip to content

Commit 11245e2

Browse files
committed
test for field argument definition lookup
1 parent 6ac0086 commit 11245e2

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import {
4545
getASTNodeAtPosition,
4646
getTokenAtPosition,
4747
getTypeInfo,
48+
DefinitionQueryResponse,
49+
getDefinitionQueryResultForArgument,
4850
} from 'graphql-language-service';
4951

5052
import type { GraphQLCache } from './GraphQLCache';
@@ -57,10 +59,6 @@ import {
5759
SymbolInformation,
5860
SymbolKind,
5961
} from 'vscode-languageserver-types';
60-
import {
61-
DefinitionQueryResponse,
62-
getDefinitionQueryResultForArgument,
63-
} from 'graphql-language-service/src/interface';
6462

6563
const KIND_TO_SYMBOL_KIND: { [key: string]: SymbolKind } = {
6664
[Kind.FIELD]: SymbolKind.Field,
@@ -488,10 +486,7 @@ export class GraphQLLanguageService {
488486
const typeInfo = getTypeInfo(schema!, token.state);
489487
const fieldName = typeInfo.fieldDef?.name;
490488
const argumentName = typeInfo.argDef?.name;
491-
492489
if (typeInfo && fieldName && argumentName) {
493-
const parentTypeName = (typeInfo.parentType as any).toString();
494-
495490
const objectTypeDefinitions =
496491
await this._graphQLCache.getObjectTypeDefinitions(projectConfig);
497492

@@ -501,7 +496,8 @@ export class GraphQLLanguageService {
501496
return getDefinitionQueryResultForArgument(
502497
argumentName,
503498
fieldName,
504-
parentTypeName,
499+
// @ts-expect-error - typeInfo is not typed correctly
500+
typeInfo.argDef?.type?.name,
505501
dependencies,
506502
);
507503
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { GraphQLLanguageService } from '../GraphQLLanguageService';
1414
import { SymbolKind } from 'vscode-languageserver-protocol';
1515
import { Position } from 'graphql-language-service';
1616
import { NoopLogger } from '../Logger';
17+
import { GraphQLEnumType } from 'graphql';
1718

1819
const MOCK_CONFIG = {
1920
filepath: join(__dirname, '.graphqlrc.yml'),
@@ -71,6 +72,16 @@ describe('GraphQLLanguageService', () => {
7172
start: 293,
7273
end: 335,
7374
},
75+
arguments: [
76+
{
77+
name: { value: 'arg' },
78+
loc: {
79+
start: 293,
80+
end: 335,
81+
},
82+
type: GraphQLEnumType,
83+
},
84+
],
7485
},
7586
],
7687

packages/graphql-language-service-server/src/__tests__/MessageProcessor.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ describe('MessageProcessor with config', () => {
420420
// and add a fragments.ts file, watched
421421
await project.addFile(
422422
'fragments.ts',
423-
'\n\n\nexport const fragment = gql`\n\n fragment T on Test { isTest }\n`',
423+
'\n\n\nexport const fragment = gql`\n\n fragment T on Test { isTest } \n query { hasArgs(string: "") }\n`',
424424
true,
425425
);
426426

@@ -445,6 +445,12 @@ describe('MessageProcessor with config', () => {
445445
character: 31,
446446
},
447447
});
448+
const defsForArgs = await project.lsp.handleDefinitionRequest({
449+
textDocument: { uri: project.uri('fragments.ts') },
450+
position: { character: 19, line: 6 },
451+
});
452+
453+
expect(defsForArgs[0].uri).toEqual(URI.parse(genSchemaPath).toString());
448454
expect(project.lsp._logger.error).not.toHaveBeenCalled();
449455
project.lsp.handleShutdownRequest();
450456
});

packages/graphql-language-service/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
getDefinitionQueryResultForFragmentSpread,
2222
getDefinitionQueryResultForNamedType,
2323
getDefinitionQueryResultForField,
24+
getDefinitionQueryResultForArgument,
2425
getDefinitionState,
2526
getDiagnostics,
2627
getFieldDef,
@@ -37,6 +38,7 @@ export {
3738
SeverityEnum,
3839
DIAGNOSTIC_SEVERITY,
3940
DefinitionQueryResult,
41+
DefinitionQueryResponse,
4042
canUseDirective,
4143
SuggestionCommand,
4244
AutocompleteSuggestionOptions,

packages/graphql-language-service/src/interface/getDefinition.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,16 @@ export async function getDefinitionQueryResultForArgument(
127127
typeName: string,
128128
dependencies: Array<ObjectTypeInfo>,
129129
): Promise<DefinitionQueryResult> {
130-
const defNodes = dependencies.filter(
130+
dependencies.filter(
131131
({ definition }) => definition.name && definition.name.value === typeName,
132132
);
133133

134-
if (defNodes.length === 0) {
135-
throw new Error(`Definition not found for GraphQL type ${typeName}`);
136-
}
137-
138134
const definitions: Array<Definition> = [];
139135

140-
for (const { filePath, content, definition } of defNodes) {
136+
for (const { filePath, content, definition } of dependencies) {
141137
const argDefinition = (definition as ObjectTypeDefinitionNode).fields
142138
?.find(item => item.name.value === fieldName)
143139
?.arguments?.find(item => item.name.value === argumentName);
144-
145140
if (argDefinition == null) {
146141
continue;
147142
}
@@ -154,7 +149,6 @@ export async function getDefinitionQueryResultForArgument(
154149
),
155150
);
156151
}
157-
158152
return {
159153
definitions,
160154
// TODO: seems like it's not using

0 commit comments

Comments
 (0)