Skip to content

Commit

Permalink
fix: remove problematic file resolution module from webpack sco… (#1489)
Browse files Browse the repository at this point in the history
* remove problematic file resolution module from webpack scope
* resolve webpack dev build issue
* moves parser types around in a way that prevents graphql-config modules from resolving in a funky way during webpack dev builds?
  • Loading branch information
acao committed Apr 12, 2020
1 parent f19aa0d commit 8dab038
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 452 deletions.
9 changes: 1 addition & 8 deletions packages/graphiql/resources/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ const rootPath = (...args) => relPath('../', ...args);

const resultConfig = {
mode: process.env.NODE_ENV,
entry: isHMR
? [
'react-hot-loader/patch', // activate HMR for React
'webpack-dev-server/client?http://localhost:8080', // bundle the client for webpack-dev-server and connect to the provided endpoint
'webpack/hot/only-dev-server', // bundle the client for hot reloading, only- means to only hot reload for successful updates
'./cdn.ts', // the entry point of our app
]
: './cdn.ts',
entry: './cdn.ts',
context: rootPath('src'),
output: {
path: rootPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
CompletionItem,
DefinitionQueryResult,
Diagnostic,
GraphQLCache,
Uri,
Position,
Outline,
OutlineTree,
GraphQLCache,
} from 'graphql-language-service-types';

import { GraphQLConfig, GraphQLProjectConfig } from 'graphql-config';
Expand All @@ -46,11 +46,7 @@ import {

import { getOutline } from './getOutline';

import {
getASTNodeAtPosition,
requireFile,
resolveFile,
} from 'graphql-language-service-utils';
import { getASTNodeAtPosition } from 'graphql-language-service-utils';

const {
FRAGMENT_DEFINITION,
Expand Down Expand Up @@ -197,19 +193,11 @@ export class GraphQLLanguageService {
}

// Check if there are custom validation rules to be used
let customRules;
const customRulesModulePath = extensions.customValidationRules;
if (customRulesModulePath) {
/* eslint-disable no-implicit-coercion */
const rulesPath = resolveFile(customRulesModulePath);
if (rulesPath) {
const customValidationRules: (
config: GraphQLConfig,
) => ValidationRule[] = await requireFile(rulesPath);
if (customValidationRules) {
customRules = customValidationRules(this._graphQLConfig);
}
}
let customRules: ValidationRule[] | null = null;
const customValidationRules = extensions.customValidationRules;
if (customValidationRules) {
customRules = customValidationRules(this._graphQLConfig);

/* eslint-enable no-implicit-coercion */
}
const schema = await this._graphQLCache.getSchema(
Expand All @@ -221,7 +209,12 @@ export class GraphQLLanguageService {
return [];
}

return validateQuery(validationAst, schema, customRules, isRelayCompatMode);
return validateQuery(
validationAst,
schema,
customRules as ValidationRule[],
isRelayCompatMode,
);
}

public async getAutocompleteSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
} from 'graphql/type/introspection';
import {
CompletionItemBase,
ContextTokenUnion,
State,
AllTypeInfo,
} from 'graphql-language-service-types';

import { ContextTokenUnion, State } from 'graphql-language-service-parser';

// Utility for returning the state representing the Definition this token state
// is within, if any.
export function getDefinitionState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import {
State,
AllTypeInfo,
Position,
RuleKind,
RuleKinds,
} from 'graphql-language-service-types';

import {
Expand All @@ -47,7 +45,12 @@ import {
isInputType,
} from 'graphql';

import { CharacterStream, onlineParser } from 'graphql-language-service-parser';
import {
CharacterStream,
onlineParser,
RuleKind,
RuleKinds,
} from 'graphql-language-service-parser';

import {
forEachState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function getDiagnostics(
export function validateQuery(
ast: DocumentNode,
schema: GraphQLSchema | null | undefined = null,
customRules?: Array<ValidationRule>,
customRules?: Array<ValidationRule> | null,
isRelayCompatMode?: boolean,
): Array<Diagnostic> {
// We cannot validate the query unless a schema is provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ import {
GraphQLField,
GraphQLFieldConfig,
} from 'graphql';
import {
ContextToken,
AllTypeInfo,
Position,
} from 'graphql-language-service-types';
import { ContextToken } from 'graphql-language-service-parser';
import { AllTypeInfo, Position } from 'graphql-language-service-types';

import { Hover } from 'vscode-languageserver-types';
import { getTokenAtPosition, getTypeInfo } from './getAutocompleteSuggestions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
* is supplied.
*
*/

import {
TokenPattern,
CharacterStreamInterface,
} from 'graphql-language-service-types';
import { TokenPattern, CharacterStreamInterface } from './types';

export default class CharacterStream implements CharacterStreamInterface {
_start: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// These functions help build matching rules for ParseRules.

import { Rule, Token } from 'graphql-language-service-types';
import { Rule, Token } from './types';

// An optional rule.
export function opt(ofRule: Rule | string): Rule {
Expand Down
10 changes: 2 additions & 8 deletions packages/graphql-language-service-parser/src/Rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
*
*/

import {
CharacterStreamInterface as CharacterStream,
State,
Token,
Rule,
RuleKind,
ParseRule,
} from 'graphql-language-service-types';
import { State, Token, Rule, RuleKind, ParseRule } from './types';
import CharacterStream from './CharacterStream';
import { opt, list, butNot, t, p } from './RuleHelpers';

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql-language-service-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export { LexRules, ParseRules, isIgnored } from './Rules';
export { butNot, list, opt, p, t } from './RuleHelpers';

export { default as onlineParser } from './onlineParser';

export * from './types';
9 changes: 2 additions & 7 deletions packages/graphql-language-service-parser/src/onlineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ import {
LexRules as LexRulesType,
ParseRules as ParseRulesType,
} from './Rules';
import {
CharacterStreamInterface as CharacterStream,
State,
Token,
Rule,
RuleKind,
} from 'graphql-language-service-types';
import CharacterStream from './CharacterStream';
import { State, Token, Rule, RuleKind } from './types';

import { LexRules, ParseRules, isIgnored } from './Rules';

Expand Down
138 changes: 138 additions & 0 deletions packages/graphql-language-service-parser/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Kind } from 'graphql';
import { _Kind } from 'graphql/language/kinds';
import { Maybe } from 'graphql-language-service-types';
import CharacterStream from './CharacterStream';

export type ContextToken = {
start: number;
end: number;
string: string;
state: State;
style: string;
};

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

export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror;

export type RuleOrString = Rule | string;

export type ParseRule =
| RuleOrString[]
| ((token: Token, stream: CharacterStream) => string | null | void);

export type Token = {
kind: string;
value: string;
};

export type Rule = {
style?: string;
match?: (token: Token) => boolean;
update?: (state: State, token: Token) => void;
separator?: string | Rule;
isList?: boolean;
ofRule?: Rule | string;
};

export type State = {
level: number;
levels?: number[];
prevState: Maybe<State>;
rule: Maybe<ParseRule>;
kind: Maybe<RuleKind>;
name: Maybe<string>;
type: Maybe<string>;
step: number;
needsSeperator: boolean;
needsAdvance?: boolean;
indentLevel?: number;
};

export const AdditionalRuleKinds: _AdditionalRuleKinds = {
ALIASED_FIELD: 'AliasedField',
ARGUMENTS: 'Arguments',
SHORT_QUERY: 'ShortQuery',
QUERY: 'Query',
MUTATION: 'Mutation',
SUBSCRIPTION: 'Subscription',
TYPE_CONDITION: 'TypeCondition',
INVALID: 'Invalid',
COMMENT: 'Comment',
SCHEMA_DEF: 'SchemaDef',
SCALAR_DEF: 'ScalarDef',
OBJECT_TYPE_DEF: 'ObjectTypeDef',
INTERFACE_DEF: 'InterfaceDef',
UNION_DEF: 'UnionDef',
ENUM_DEF: 'EnumDef',
FIELD_DEF: 'FieldDef',
INPUT_DEF: 'InputDef',
INPUT_VALUE_DEF: 'InputValueDef',
ARGUMENTS_DEF: 'ArgumentsDef',
EXTEND_DEF: 'ExtendDef',
DIRECTIVE_DEF: 'DirectiveDef',
};

export type _AdditionalRuleKinds = {
ALIASED_FIELD: 'AliasedField';
ARGUMENTS: 'Arguments';
SHORT_QUERY: 'ShortQuery';
QUERY: 'Query';
MUTATION: 'Mutation';
SUBSCRIPTION: 'Subscription';
TYPE_CONDITION: 'TypeCondition';
INVALID: 'Invalid';
COMMENT: 'Comment';
SCHEMA_DEF: 'SchemaDef';
SCALAR_DEF: 'ScalarDef';
OBJECT_TYPE_DEF: 'ObjectTypeDef';
INTERFACE_DEF: 'InterfaceDef';
UNION_DEF: 'UnionDef';
ENUM_DEF: 'EnumDef';
FIELD_DEF: 'FieldDef';
INPUT_DEF: 'InputDef';
INPUT_VALUE_DEF: 'InputValueDef';
ARGUMENTS_DEF: 'ArgumentsDef';
EXTEND_DEF: 'ExtendDef';
DIRECTIVE_DEF: 'DirectiveDef';
};

export const RuleKinds = {
...Kind,
...AdditionalRuleKinds,
};

export type _RuleKinds = _Kind & typeof AdditionalRuleKinds;

export type RuleKind = _RuleKinds[keyof _RuleKinds];
export type RuleKindEnum = RuleKind;
export type TokenPattern = string | ((char: string) => boolean) | RegExp;

export interface CharacterStreamInterface {
getStartOfToken: () => number;
getCurrentPosition: () => number;
eol: () => boolean;
sol: () => boolean;
peek: () => string | null;
next: () => string;
eat: (pattern: TokenPattern) => string | undefined;
eatWhile: (match: TokenPattern) => boolean;
eatSpace: () => boolean;
skipToEnd: () => void;
skipTo: (position: number) => void;
match: (
pattern: TokenPattern,
consume?: Maybe<boolean>,
caseFold?: Maybe<boolean>,
) => string[] | boolean;
backUp: (num: number) => void;
column: () => number;
indentation: () => number;
current: () => string;
}
6 changes: 3 additions & 3 deletions packages/graphql-language-service-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ await startServer({
});
```

The graphql features we support are:
The graphql-config features we support are:

- `customDirectives` - `['@myExampleDirective']`
- `customValidationRules` - returns rules array with parameter `ValidationContext` from `graphql/validation`;
- `extensions.customDirectives` - for example `['@myExampleDirective']`
- `extensions.customValidationRules` - a function that returns rules array with parameter `ValidationContext` from `graphql/validation`. The graphql config must load the module itself.

### Usage

Expand Down
Loading

0 comments on commit 8dab038

Please sign in to comment.