Skip to content

Commit

Permalink
feat: export more functions (#1264)
Browse files Browse the repository at this point in the history
* add more type aliases
* export more functions

Co-authored-by: Rikki Schulte <[email protected]>
  • Loading branch information
yoshiakis and acao authored Apr 4, 2020
1 parent c57a374 commit bd01fdd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
TypeNameMetaFieldDef,
} from 'graphql/type/introspection';
import {
CompletionItem,
ContextToken,
CompletionItemBase,
ContextTokenUnion,
State,
AllTypeInfo,
} from 'graphql-language-service-types';
Expand Down Expand Up @@ -93,21 +93,21 @@ export function objectValues(object: Record<string, any>): Array<any> {
}

// Create the expected hint response given a possible list and a token
export function hintList(
token: ContextToken,
list: Array<CompletionItem>,
): Array<CompletionItem> {
export function hintList<T extends CompletionItemBase>(
token: ContextTokenUnion,
list: Array<T>,
): Array<T> {
return filterAndSortList(list, normalizeText(token.string));
}

// Given a list of hint entries and currently typed text, sort and filter to
// provide a concise list.
function filterAndSortList(
list: Array<CompletionItem>,
function filterAndSortList<T extends CompletionItemBase>(
list: Array<T>,
text: string,
): Array<CompletionItem> {
): Array<T> {
if (!text) {
return filterNonEmpty<CompletionItem>(list, entry => !entry.isDeprecated);
return filterNonEmpty<T>(list, entry => !entry.isDeprecated);
}

const byProximity = list.map(entry => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function getSuggestionsForFragmentSpread(
);
}

function getFragmentDefinitions(
export function getFragmentDefinitions(
queryText: string,
): Array<FragmentDefinitionNode> {
const fragmentDefs: FragmentDefinitionNode[] = [];
Expand Down Expand Up @@ -431,7 +431,7 @@ type callbackFnType = (
index: number,
) => void | 'BREAK';

function runOnlineParser(
export function runOnlineParser(
queryText: string,
callback: callbackFnType,
): ContextToken {
Expand Down Expand Up @@ -470,7 +470,7 @@ function runOnlineParser(
};
}

function canUseDirective(
export function canUseDirective(
state: State['prevState'],
directive: GraphQLDirective,
): boolean {
Expand Down
27 changes: 27 additions & 0 deletions packages/graphql-language-service-interface/src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import type {
Outline,
Diagnostic,
ContextToken,
ContextTokenUnion,
Position,
DefinitionQueryResult,
Uri,
GraphQLCache,
CompletionItem,
CompletionItemBase,
TypeInfo,
State,
} from 'graphql-language-service-types';
import type { ValidationRule, GraphQLSchema } from 'graphql';
import type { Hover } from 'vscode-languageserver-types';
import { GraphQLDirective } from "graphql";
import type { FragmentDefinitionNode } from "graphql";

declare export function getOutline(queryText: string): ?Outline;
declare export function getDiagnostics(
Expand Down Expand Up @@ -61,3 +67,24 @@ declare export class GraphQLLanguageService {
filePath: Uri,
): Promise<Array<CompletionItem>>;
}
declare export function canUseDirective(
state: $PropertyType<State, "prevState">,
directive: GraphQLDirective
): boolean;
declare export function getDefinitionState(
tokenState: State
): State | null | void;
declare export function getFragmentDefinitions(
queryText: string
): Array<FragmentDefinitionNode>;
declare export function getTypeInfo(
schema: GraphQLSchema,
tokenState: State
): TypeInfo;
declare export function hintList<T: CompletionItemBase>(
token: ContextTokenUnion,
list: Array<T>
): Array<T>;
declare export function objectValues(object: {
[key: string]: any,
}): Array<any>;
7 changes: 6 additions & 1 deletion packages/graphql-language-service-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export {
hintList,
} from './autocompleteUtils';

export { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
export {
canUseDirective,
getAutocompleteSuggestions,
getFragmentDefinitions,
getTypeInfo,
} from './getAutocompleteSuggestions';

export {
LANGUAGE,
Expand Down
23 changes: 23 additions & 0 deletions packages/graphql-language-service-types/src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ export type ContextToken = {
style: string,
};

export type ContextTokenForCodeMirror = {
start: number,
end: number,
string: string,
type: string | null,
state: State,
};

export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror;

export type TypeInfo = {
type: ?GraphQLType,
parentType: ?GraphQLType,
Expand Down Expand Up @@ -270,6 +280,19 @@ export type CompletionItem = {
deprecationReason?: ?string,
};

export type CompletionItemBase = {
label: string,
isDeprecated?: boolean,
};

export type CompletionItemForCodeMirror = {
label: string,
type?: GraphQLType,
documentation?: ?string,
isDeprecated?: boolean,
deprecationReason?: ?string,
};

// Below are basically a copy-paste from Nuclide rpc types for definitions.

// Definitions/hyperlink
Expand Down
23 changes: 23 additions & 0 deletions packages/graphql-language-service-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ export type ContextToken = {
style: string;
};

export type ContextTokenForCodeMirror = {
start: number;
end: number;
string: string;
type: string | null;
state: State;
};

export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror;

export type AllTypeInfo = {
type: Maybe<GraphQLType>;
parentType: Maybe<GraphQLType>;
Expand Down Expand Up @@ -282,11 +292,24 @@ export type ObjectTypeInfo = {

export type Diagnostic = DiagnosticType;

export type CompletionItemBase = {
label: string;
isDeprecated?: boolean;
};

export type CompletionItem = CompletionItemType & {
isDeprecated?: boolean;
deprecationReason?: Maybe<string>;
};

export type CompletionItemForCodeMirror = {
label: string;
type?: GraphQLType;
documentation: string | null | undefined;
isDeprecated?: boolean;
deprecationReason: string | null | undefined;
};

// Below are basically a copy-paste from Nuclide rpc types for definitions.

// Definitions/hyperlink
Expand Down

0 comments on commit bd01fdd

Please sign in to comment.