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

Feature go to definition SDL - input, enum, type #237

Merged
merged 8 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Partial support for [Microsoft's Language Server Protocol](https://github.com/Mi
Currently supported features include:
- Diagnostics (GraphQL syntax linting/validations) (**spec-compliant**)
- Autocomplete suggestions (**spec-compliant**)
- Hyperlink to fragment definitions (**spec-compliant**)
- Hyperlink to fragment definitions and named types (type, input, enum) definitions (**spec-compliant**)
- Outline view support for queries


Expand Down
56 changes: 56 additions & 0 deletions packages/interface/src/GraphQLLanguageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
FragmentSpreadNode,
FragmentDefinitionNode,
OperationDefinitionNode,
TypeDefinitionNode,
NamedTypeNode,
} from 'graphql';
import type {
CompletionItem,
Expand Down Expand Up @@ -43,6 +45,7 @@ import {
DIRECTIVE_DEFINITION,
FRAGMENT_SPREAD,
OPERATION_DEFINITION,
NAMED_TYPE,
} from 'graphql/language/kinds';

import {parse, print} from 'graphql';
Expand All @@ -52,6 +55,7 @@ import {validateQuery, getRange, SEVERITY} from './getDiagnostics';
import {
getDefinitionQueryResultForFragmentSpread,
getDefinitionQueryResultForDefinitionNode,
getDefinitionQueryResultForNamedType,
} from './getDefinition';
import {getASTNodeAtPosition} from 'graphql-language-service-utils';

Expand Down Expand Up @@ -224,11 +228,63 @@ export class GraphQLLanguageService {
query,
(node: FragmentDefinitionNode | OperationDefinitionNode),
);
case NAMED_TYPE:
return this._getDefinitionForNamedType(
query,
ast,
node,
filePath,
projectConfig,
);
}
}
return null;
}

async _getDefinitionForNamedType(
query: string,
ast: DocumentNode,
node: NamedTypeNode,
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<?DefinitionQueryResult> {
const objectTypeDefinitions = await this._graphQLCache.getObjectTypeDefinitions(
projectConfig,
);

const dependencies = await this._graphQLCache.getObjectTypeDependenciesForAST(
ast,
objectTypeDefinitions,
);

const localObjectTypeDefinitions = ast.definitions.filter(
definition =>
definition.kind === OBJECT_TYPE_DEFINITION ||
definition.kind === INPUT_OBJECT_TYPE_DEFINITION ||
definition.kind === ENUM_TYPE_DEFINITION,
);

const typeCastedDefs = ((localObjectTypeDefinitions: any): Array<
TypeDefinitionNode,
>);

const localOperationDefinationInfos = typeCastedDefs.map(
(definition: TypeDefinitionNode) => ({
filePath,
content: query,
definition,
}),
);

const result = await getDefinitionQueryResultForNamedType(
query,
node,
dependencies.concat(localOperationDefinationInfos),
);

return result;
}

async _getDefinitionForFragmentSpread(
query: string,
ast: DocumentNode,
Expand Down
48 changes: 47 additions & 1 deletion packages/interface/src/__tests__/GraphQLLanguageService-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,51 @@ import {GraphQLConfig} from 'graphql-config';
import {GraphQLLanguageService} from '../GraphQLLanguageService';

const MOCK_CONFIG = {
includes: ['./queries/**'],
schemaPath: './__schema__/StarWarsSchema.graphql',
includes: ['./queries/**', '**/*.graphql'],
};

describe('GraphQLLanguageService', () => {
const mockCache: any = {
getGraphQLConfig() {
return new GraphQLConfig(MOCK_CONFIG, join(__dirname, '.graphqlconfig'));
},

getObjectTypeDefinitions() {
return {
Episode: {
filePath: 'fake file path',
content: 'fake file content',
definition: {
name: {
value: 'Episode',
},
loc: {
start: 293,
end: 335,
},
},
},
};
},

getObjectTypeDependenciesForAST() {
return [
{
filePath: 'fake file path',
content: 'fake file content',
definition: {
name: {
value: 'Episode',
},
loc: {
start: 293,
end: 335,
},
},
},
];
},
};

let languageService;
Expand All @@ -38,4 +75,13 @@ describe('GraphQLLanguageService', () => {
);
expect(diagnostics.length).to.equal(1);
});

it('runs definition service as expected', async () => {
const definitionQueryResult = await languageService.getDefinition(
'type Query { hero(episode: Episode): Character }',
{line: 0, character: 28},
'./queries/definitionQuery.graphql',
);
expect(definitionQueryResult.definitions.length).to.equal(1);
});
});
41 changes: 39 additions & 2 deletions packages/interface/src/__tests__/getDefinition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,46 @@
import {expect} from 'chai';
import {describe, it} from 'mocha';
import {parse} from 'graphql';
import {getDefinitionQueryResultForFragmentSpread} from '../getDefinition';
import {
getDefinitionQueryResultForFragmentSpread,
getDefinitionQueryResultForNamedType,
} from '../getDefinition';

describe('getDefinition', () => {
describe('getDefinitionQueryResultForNamedType', () => {
it('returns correct Position', async () => {
const query = `type Query {
hero(episode: Episode): Character
}

type Episode {
id: ID!
}
`;
const parsedQuery = parse(query);
const namedTypeDefinition = parsedQuery.definitions[0].fields[0].type;

const result = await getDefinitionQueryResultForNamedType(
query,
{
...namedTypeDefinition,
},
[
{
file: 'someFile',
content: query,
definition: {
...namedTypeDefinition,
},
},
],
);
expect(result.definitions.length).to.equal(1);
expect(result.definitions[0].position.line).to.equal(1);
expect(result.definitions[0].position.character).to.equal(32);
});
});

describe('getDefinitionQueryResultForFragmentSpread', () => {
it('returns correct Position', async () => {
const query = `query A {
Expand All @@ -39,7 +76,7 @@ describe('getDefinition', () => {
);
expect(result.definitions.length).to.equal(1);
expect(result.definitions[0].position.line).to.equal(1);
expect(result.definitions[0].position.character).to.equal(15);
expect(result.definitions[0].position.character).to.equal(6);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Query { hero(episode: Episode): Character }
46 changes: 45 additions & 1 deletion packages/interface/src/getDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
FragmentSpreadNode,
FragmentDefinitionNode,
OperationDefinitionNode,
NamedTypeNode,
TypeDefinitionNode,
} from 'graphql';
import type {
Definition,
Expand All @@ -21,6 +23,7 @@ import type {
Position,
Range,
Uri,
ObjectTypeInfo,
} from 'graphql-language-service-types';
import {locToRange, offsetToPosition} from 'graphql-language-service-utils';
import invariant from 'assert';
Expand All @@ -39,6 +42,29 @@ function getPosition(text: string, node: ASTNode): Position {
return offsetToPosition(text, location.start);
}

export async function getDefinitionQueryResultForNamedType(
text: string,
node: NamedTypeNode,
dependencies: Array<ObjectTypeInfo>,
): Promise<DefinitionQueryResult> {
const name = node.name.value;
const defNodes = dependencies.filter(
({definition}) => definition.name && definition.name.value === name,
);
if (defNodes.length === 0) {
process.stderr.write(`Definition not found for GraphQL type ${name}`);
return {queryRange: [], definitions: []};
}
const definitions: Array<Definition> = defNodes.map(
({filePath, content, definition}) =>
getDefinitionForNodeDefinition(filePath || '', content, definition),
);
return {
definitions,
queryRange: definitions.map(_ => getRange(text, node)),
};
}

export async function getDefinitionQueryResultForFragmentSpread(
text: string,
fragment: FragmentSpreadNode,
Expand Down Expand Up @@ -82,7 +108,25 @@ function getDefinitionForFragmentDefinition(
invariant(name, 'Expected ASTNode to have a Name.');
return {
path,
position: getPosition(text, name),
position: getPosition(text, definition),
range: getRange(text, definition),
name: name.value || '',
language: LANGUAGE,
// This is a file inside the project root, good enough for now
projectRoot: path,
};
}

function getDefinitionForNodeDefinition(
path: Uri,
text: string,
definition: TypeDefinitionNode,
): Definition {
const name = definition.name;
invariant(name, 'Expected ASTNode to have a Name.');
return {
path,
position: getPosition(text, definition),
range: getRange(text, definition),
name: name.value || '',
language: LANGUAGE,
Expand Down
Loading