Skip to content

Commit

Permalink
test for field argument definition lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed May 18, 2024
1 parent 6ac0086 commit 11245e2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
getASTNodeAtPosition,
getTokenAtPosition,
getTypeInfo,
DefinitionQueryResponse,
getDefinitionQueryResultForArgument,
} from 'graphql-language-service';

import type { GraphQLCache } from './GraphQLCache';
Expand All @@ -57,10 +59,6 @@ import {
SymbolInformation,
SymbolKind,
} from 'vscode-languageserver-types';
import {
DefinitionQueryResponse,
getDefinitionQueryResultForArgument,
} from 'graphql-language-service/src/interface';

const KIND_TO_SYMBOL_KIND: { [key: string]: SymbolKind } = {
[Kind.FIELD]: SymbolKind.Field,
Expand Down Expand Up @@ -488,10 +486,7 @@ export class GraphQLLanguageService {
const typeInfo = getTypeInfo(schema!, token.state);
const fieldName = typeInfo.fieldDef?.name;
const argumentName = typeInfo.argDef?.name;

if (typeInfo && fieldName && argumentName) {
const parentTypeName = (typeInfo.parentType as any).toString();

const objectTypeDefinitions =
await this._graphQLCache.getObjectTypeDefinitions(projectConfig);

Expand All @@ -501,7 +496,8 @@ export class GraphQLLanguageService {
return getDefinitionQueryResultForArgument(
argumentName,
fieldName,
parentTypeName,
// @ts-expect-error - typeInfo is not typed correctly
typeInfo.argDef?.type?.name,
dependencies,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GraphQLLanguageService } from '../GraphQLLanguageService';
import { SymbolKind } from 'vscode-languageserver-protocol';
import { Position } from 'graphql-language-service';
import { NoopLogger } from '../Logger';
import { GraphQLEnumType } from 'graphql';

const MOCK_CONFIG = {
filepath: join(__dirname, '.graphqlrc.yml'),
Expand Down Expand Up @@ -71,6 +72,16 @@ describe('GraphQLLanguageService', () => {
start: 293,
end: 335,
},
arguments: [
{
name: { value: 'arg' },
loc: {
start: 293,
end: 335,
},
type: GraphQLEnumType,
},
],
},
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('MessageProcessor with config', () => {
// and add a fragments.ts file, watched
await project.addFile(
'fragments.ts',
'\n\n\nexport const fragment = gql`\n\n fragment T on Test { isTest }\n`',
'\n\n\nexport const fragment = gql`\n\n fragment T on Test { isTest } \n query { hasArgs(string: "") }\n`',
true,
);

Expand All @@ -445,6 +445,12 @@ describe('MessageProcessor with config', () => {
character: 31,
},
});
const defsForArgs = await project.lsp.handleDefinitionRequest({
textDocument: { uri: project.uri('fragments.ts') },
position: { character: 19, line: 6 },
});

expect(defsForArgs[0].uri).toEqual(URI.parse(genSchemaPath).toString());
expect(project.lsp._logger.error).not.toHaveBeenCalled();
project.lsp.handleShutdownRequest();
});
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql-language-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
getDefinitionQueryResultForFragmentSpread,
getDefinitionQueryResultForNamedType,
getDefinitionQueryResultForField,
getDefinitionQueryResultForArgument,
getDefinitionState,
getDiagnostics,
getFieldDef,
Expand All @@ -37,6 +38,7 @@ export {
SeverityEnum,
DIAGNOSTIC_SEVERITY,
DefinitionQueryResult,
DefinitionQueryResponse,
canUseDirective,
SuggestionCommand,
AutocompleteSuggestionOptions,
Expand Down
10 changes: 2 additions & 8 deletions packages/graphql-language-service/src/interface/getDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,16 @@ export async function getDefinitionQueryResultForArgument(
typeName: string,
dependencies: Array<ObjectTypeInfo>,
): Promise<DefinitionQueryResult> {
const defNodes = dependencies.filter(
dependencies.filter(
({ definition }) => definition.name && definition.name.value === typeName,
);

if (defNodes.length === 0) {
throw new Error(`Definition not found for GraphQL type ${typeName}`);
}

const definitions: Array<Definition> = [];

for (const { filePath, content, definition } of defNodes) {
for (const { filePath, content, definition } of dependencies) {
const argDefinition = (definition as ObjectTypeDefinitionNode).fields
?.find(item => item.name.value === fieldName)
?.arguments?.find(item => item.name.value === argumentName);

if (argDefinition == null) {
continue;
}
Expand All @@ -154,7 +149,6 @@ export async function getDefinitionQueryResultForArgument(
),
);
}

return {
definitions,
// TODO: seems like it's not using
Expand Down

0 comments on commit 11245e2

Please sign in to comment.