Skip to content

Commit

Permalink
chore: more conversion work, start stripping out flow
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Dec 21, 2019
1 parent 2d6a7b4 commit 7e771ed
Show file tree
Hide file tree
Showing 23 changed files with 317 additions and 205 deletions.
29 changes: 2 additions & 27 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
[ignore]
.*/__mocks__/.*
.*/__tests__/.*
.*/coverage/.*
.*/dist/.*
.*/resources/.*
.*/node_modules/.*
.*/node_modules/conventional-changelog-core/*
.*/node_modules/graphql/error/GraphQLError.js.flow
!<PROJECT_ROOT>/node_modules/chai
!<PROJECT_ROOT>/node_modules/mocha
!<PROJECT_ROOT>/node_modules/graphql
!<PROJECT_ROOT>/node_modules/iterall
!<PROJECT_ROOT>/node_modules/codemirror
!<PROJECT_ROOT>/node_modules/codemirror-graphql
!<PROJECT_ROOT>/node_modules/graphql-language-service-interface
!<PROJECT_ROOT>/node_modules/graphql-language-service-server
!<PROJECT_ROOT>/node_modules/graphql-language-service-parser
!<PROJECT_ROOT>/node_modules/graphql-language-service-types
!<PROJECT_ROOT>/node_modules/graphql-language-service-utils
!<PROJECT_ROOT>/node_modules/glob
!<PROJECT_ROOT>/node_modules/@babel/parser
!<PROJECT_ROOT>/node_modules/vscode-languageserver
!<PROJECT_ROOT>/node_modules/vscode-jsonrpc
!<PROJECT_ROOT>/node_modules/vscode-languageserver-types
!<PROJECT_ROOT>/node_modules/nullthrows
!<PROJECT_ROOT>/node_modules/fb-watchman
!<PROJECT_ROOT>/node_modules/yargs
.*/*/.*
!*.flow.js

[include]

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ module.exports = {
'!**/resources/**',
'!**/examples/**',
'!**/codemirror-graphql/**',
'!**/graphql-language-service-types/**',
],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"build": "yarn run build-clean && yarn build-ts && yarn build-js",
"build-js": "lerna run build --scope codemirror-graphql --scope graphql-language-service-server --scope graphiql --scope graphql-language-service",
"build-js": "lerna run build --scope codemirror-graphql --scope graphiql",
"build-ts": "tsc --build",
"build-clean": "tsc --build --clean && rimraf '{packages,examples}/**/{dist,esm,bundle}' && lerna run build-clean --parallel",
"build-demo": "lerna run build-demo",
Expand Down Expand Up @@ -56,6 +56,7 @@
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@commitlint/config-lerna-scopes": "^8.1.0",
"@types/fetch-mock": "^7.3.2",
"@types/jest": "^24.0.18",
"@typescript-eslint/parser": "^2.3.0",
"babel-eslint": "^10.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class GraphQLLanguageService {
query: string,
position: Position,
filePath: Uri,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const projectConfig = this.getConfigForURI(filePath);

let ast;
Expand Down Expand Up @@ -269,7 +269,7 @@ export class GraphQLLanguageService {
node: NamedTypeNode,
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const objectTypeDefinitions = await this._graphQLCache.getObjectTypeDefinitions(
projectConfig,
);
Expand Down Expand Up @@ -313,7 +313,7 @@ export class GraphQLLanguageService {
node: FragmentSpreadNode,
filePath: Uri,
projectConfig: GraphQLProjectConfig,
): Promise<DefinitionQueryResult | null | undefined> {
): Promise<DefinitionQueryResult | null> {
const fragmentDefinitions = await this._graphQLCache.getFragmentDefinitions(
projectConfig,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ describe('getAutocompleteSuggestions', () => {
// Below should provide root field names
expect(testSuggestions(' {}', new Position(0, 2))).toEqual([
{ label: '__typename', detail: 'String!' },
{
detail: 'TestType',
label: 'deprecatedField',
},
{ label: 'droid', detail: 'Droid' },
{ label: 'hero', detail: 'Character' },
{ label: 'human', detail: 'Human' },
Expand All @@ -121,6 +125,10 @@ query name {
),
).toEqual([
{ label: '__typename', detail: 'String!' },
{
detail: 'TestType',
label: 'deprecatedField',
},
{ label: 'droid', detail: 'Droid' },
{ label: 'hero', detail: 'Character' },
{ label: 'human', detail: 'Human' },
Expand All @@ -132,6 +140,10 @@ query name {
const result = testSuggestions('{ ', new Position(0, 2));
expect(result).toEqual([
{ label: '__typename', detail: 'String!' },
{
detail: 'TestType',
label: 'deprecatedField',
},
{ label: 'droid', detail: 'Droid' },
{ label: 'hero', detail: 'Character' },
{ label: 'human', detail: 'Human' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function getDefinitionState(
let definitionState;

// TODO - couldn't figure this one out
// @ts-ignore
forEachState(tokenState, (state: State): AllTypeInfo | null | undefined => {
forEachState(tokenState, (state: State): void => {
switch (state.kind) {
case 'Query':
case 'ShortQuery':
Expand Down
16 changes: 9 additions & 7 deletions packages/graphql-language-service-interface/src/getDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import {
} from 'graphql-language-service-types';

import { locToRange, offsetToPosition } from 'graphql-language-service-utils';
import invariant from 'assert';
import assert from 'assert';

export const LANGUAGE = 'GraphQL';

function getRange(text: string, node: ASTNode): Range {
const location = node.loc as Location;
invariant(location, 'Expected ASTNode to have a location.');
assert(location, 'Expected ASTNode to have a location.');
return locToRange(text, location);
}

function getPosition(text: string, node: ASTNode): Position {
const location = node.loc as Location;
invariant(location, 'Expected ASTNode to have a location.');
assert(location, 'Expected ASTNode to have a location.');
return offsetToPosition(text, location.start);
}

Expand Down Expand Up @@ -111,14 +111,16 @@ function getDefinitionForFragmentDefinition(
definition: FragmentDefinitionNode | OperationDefinitionNode,
): Definition {
const name = definition.name;
invariant(name, 'Expected ASTNode to have a Name.');
if (!name) {
throw Error('Expected ASTNode to have a Name.');
}

return {
path,
position: getPosition(text, definition),
range: getRange(text, definition),
// @ts-ignore
// TODO: doesnt seem to pick up the inference
// from invariant() exception logic
// from assert() exception logic
name: name.value || '',
language: LANGUAGE,
// This is a file inside the project root, good enough for now
Expand All @@ -132,7 +134,7 @@ function getDefinitionForNodeDefinition(
definition: TypeDefinitionNode,
): Definition {
const name = definition.name;
invariant(name, 'Expected ASTNode to have a Name.');
assert(name, 'Expected ASTNode to have a Name.');
return {
path,
position: getPosition(text, definition),
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-language-service-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"typings": "esm/index.d.ts",
"scripts": {
"test": "node ../../resources/runTests.js",
"build": "yarn build-js && yarn build-esm && yarn build-flow",
"build-js": "node ../../resources/buildJs.js",
"build-esm": "cross-env ESM=true babel src --root-mode upward --ignore **/__tests__/**,**/__mocks__/** --out-dir esm",
"build": "yarn build-js && yarn build-flow",
"build-js": "rimraf dist && tsc",
"build-esm": "rimraf esm && tsc -p tsconfig.esm",
"build-flow": "node ../../resources/buildFlow.js"
},
"peerDependencies": {
Expand Down
70 changes: 42 additions & 28 deletions packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ export async function getGraphQLCache(
return new GraphQLCache(configDir, graphQLConfig);
}

export type ReadGraphQLDocumentResponse = {
filePath: Uri;
content: string;
asts: Array<DocumentNode>;
queries: Array<CachedContent>;
};

export class GraphQLCache implements GraphQLCacheInterface {
_configDir: Uri;
_graphQLFileListCache: Map<Uri, Map<string, GraphQLFileInfo>>;
Expand All @@ -93,7 +86,7 @@ export class GraphQLCache implements GraphQLCacheInterface {

getFragmentDependencies = async (
query: string,
fragmentDefinitions?: Map<string, FragmentInfo>,
fragmentDefinitions?: Map<string, FragmentInfo> | null,
): Promise<FragmentInfo[]> => {
// If there isn't context for fragment references,
// return an empty array.
Expand Down Expand Up @@ -683,15 +676,14 @@ export class GraphQLCache implements GraphQLCacheInterface {

if (endpointInfo && endpointKey) {
const { endpoint } = endpointInfo;

schemaCacheKey = endpointKey;

// Maybe use cache
if (this._schemaMap.has(schemaCacheKey)) {
schema = this._schemaMap.get(schemaCacheKey);
return schema && queryHasExtensions
? this._extendSchema(schema, schemaPath, schemaCacheKey)
: schema;
: schema || null;
}

// Read schema from network
Expand All @@ -708,9 +700,11 @@ export class GraphQLCache implements GraphQLCacheInterface {
// Maybe use cache
if (this._schemaMap.has(schemaCacheKey)) {
schema = this._schemaMap.get(schemaCacheKey);
return schema && queryHasExtensions
? this._extendSchema(schema, schemaPath, schemaCacheKey)
: schema;
if (schema) {
return queryHasExtensions
? this._extendSchema(schema, schemaPath, schemaCacheKey)
: schema;
}
}

// Read from disk
Expand Down Expand Up @@ -823,15 +817,15 @@ export class GraphQLCache implements GraphQLCacheInterface {
queue.push(fileInfo);
}
})
.then((response: ReadGraphQLDocumentResponse) =>
responses.push({
asts: response.asts,
content: response.content,
filePath: response.filePath,
mtime: fileInfo.mtime,
size: fileInfo.size,
}),
),
.then((response: GraphQLFileInfo | void) => {
if (response) {
responses.push({
...response,
mtime: fileInfo.mtime,
size: fileInfo.size,
});
}
}),
);
await Promise.all(promises); // eslint-disable-line no-await-in-loop
}
Expand Down Expand Up @@ -903,9 +897,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
* Returns a Promise to read a GraphQL file and return a GraphQL metadata
* including a parsed AST.
*/
promiseToReadGraphQLFile = (
filePath: Uri,
): Promise<ReadGraphQLDocumentResponse> => {
promiseToReadGraphQLFile = (filePath: Uri): Promise<GraphQLFileInfo> => {
return new Promise((resolve, reject) =>
fs.readFile(filePath, 'utf8', (error, content) => {
if (error) {
Expand All @@ -920,7 +912,14 @@ export class GraphQLCache implements GraphQLCacheInterface {
queries = getQueryAndRange(content, filePath);
if (queries.length === 0) {
// still resolve with an empty ast
resolve({ filePath, content, asts: [], queries: [] });
resolve({
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
});
return;
}

Expand All @@ -932,14 +931,29 @@ export class GraphQLCache implements GraphQLCacheInterface {
}),
),
);
resolve({
filePath,
content,
asts,
queries,
mtime: 0,
size: 0,
});
} catch (_) {
// If query has syntax errors, go ahead and still resolve
// the filePath and the content, but leave ast empty.
resolve({ filePath, content, asts: [], queries: [] });
resolve({
filePath,
content,
asts: [],
queries: [],
mtime: 0,
size: 0,
});
return;
}
}
resolve({ filePath, content, asts, queries });
resolve({ filePath, content, asts, queries, mtime: 0, size: 0 });
}),
);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-language-service-server/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import fs from 'fs';
import os from 'os';
import { join } from 'path';

const SEVERITY = {
const SEVERITY: { [key: string]: string } = {
ERROR: 'ERROR',
WARNING: 'WARNING',
INFO: 'INFO',
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Logger implements VSCodeLogger {

const logMessage = `${timestamp} [${severity}] (pid: ${pid}) graphql-language-service-usage-logs: ${message}\n\n`;
// write to the file in tmpdir
fs.appendFile(this._logFilePath, logMessage, error => {});
fs.appendFile(this._logFilePath, logMessage, _error => {});
}
}

Expand Down
Loading

0 comments on commit 7e771ed

Please sign in to comment.