|
| 1 | +import { Parser } from 'graphql/language/parser'; |
| 2 | +import { Lexer } from 'graphql/language/lexer'; |
| 3 | +import { TokenKind, Kind, Source, DocumentNode, TokenKindEnum, Token } from 'graphql'; |
| 4 | + |
| 5 | +declare module 'graphql/language/parser' { |
| 6 | + export class Parser { |
| 7 | + constructor(source: string | Source, options?: ParseOptions); |
| 8 | + _lexer: Lexer; |
| 9 | + expectOptionalKeyword(word: string): boolean; |
| 10 | + expectToken(token: TokenKindEnum): void; |
| 11 | + peek(token: TokenKindEnum): boolean; |
| 12 | + parseFragmentName(): string; |
| 13 | + parseArguments(flag: boolean): any; |
| 14 | + parseDirectives(flag: boolean): any; |
| 15 | + loc(start: Token): any; |
| 16 | + parseNamedType(): any; |
| 17 | + parseSelectionSet(): any; |
| 18 | + expectKeyword(keyword: string): void; |
| 19 | + parseVariableDefinitions(): void; |
| 20 | + parseDocument(): DocumentNode; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export class FragmentArgumentCompatibleParser extends Parser { |
| 25 | + parseFragment() { |
| 26 | + const start = this._lexer.token; |
| 27 | + this.expectToken(TokenKind.SPREAD); |
| 28 | + const hasTypeCondition = this.expectOptionalKeyword('on'); |
| 29 | + |
| 30 | + if (!hasTypeCondition && this.peek(TokenKind.NAME)) { |
| 31 | + const name = this.parseFragmentName(); |
| 32 | + |
| 33 | + if (this.peek(TokenKind.PAREN_L)) { |
| 34 | + return { |
| 35 | + kind: Kind.FRAGMENT_SPREAD, |
| 36 | + name, |
| 37 | + arguments: this.parseArguments(false), |
| 38 | + directives: this.parseDirectives(false), |
| 39 | + loc: this.loc(start), |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + kind: Kind.FRAGMENT_SPREAD, |
| 45 | + name: this.parseFragmentName(), |
| 46 | + directives: this.parseDirectives(false), |
| 47 | + loc: this.loc(start), |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + return { |
| 52 | + kind: Kind.INLINE_FRAGMENT, |
| 53 | + typeCondition: hasTypeCondition ? this.parseNamedType() : undefined, |
| 54 | + directives: this.parseDirectives(false), |
| 55 | + selectionSet: this.parseSelectionSet(), |
| 56 | + loc: this.loc(start), |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + parseFragmentDefinition() { |
| 61 | + const start = this._lexer.token; |
| 62 | + this.expectKeyword('fragment'); |
| 63 | + const name = this.parseFragmentName(); |
| 64 | + |
| 65 | + if (this.peek(TokenKind.PAREN_L)) { |
| 66 | + return { |
| 67 | + kind: Kind.FRAGMENT_DEFINITION, |
| 68 | + name, |
| 69 | + variableDefinitions: this.parseVariableDefinitions(), |
| 70 | + typeCondition: (this.expectKeyword('on'), this.parseNamedType()), |
| 71 | + directives: this.parseDirectives(false), |
| 72 | + selectionSet: this.parseSelectionSet(), |
| 73 | + loc: this.loc(start), |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + kind: Kind.FRAGMENT_DEFINITION, |
| 79 | + name, |
| 80 | + typeCondition: (this.expectKeyword('on'), this.parseNamedType()), |
| 81 | + directives: this.parseDirectives(false), |
| 82 | + selectionSet: this.parseSelectionSet(), |
| 83 | + loc: this.loc(start), |
| 84 | + }; |
| 85 | + } |
| 86 | +} |
0 commit comments