@apollo/core-schema
Version:
Apollo Core Schema processing library
69 lines • 2.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImportsParser = void 0;
const graphql_1 = require("graphql");
const parser_1 = require("graphql/language/parser");
class ImportsParser extends parser_1.Parser {
static fromString(source) {
return new ImportsParser(new graphql_1.Source(source))
.parseImports();
}
parseImports() {
return this.many(graphql_1.TokenKind.SOF, this.parseImport, graphql_1.TokenKind.EOF);
}
parseImport() {
const start = this._lexer.token;
const first = this.parseImportElement();
if (this.peek(graphql_1.TokenKind.COLON)) {
this.expectToken(graphql_1.TokenKind.COLON);
const remote = this.parseImportElement();
if (remote.kind !== first.kind)
throw new Error('local and remote name must be same kind of reference');
return this.node(start, {
type: 'Import',
element: remote,
alias: first
});
}
if (this.peek(graphql_1.TokenKind.PAREN_L)) {
this.expectToken(graphql_1.TokenKind.PAREN_L);
this.expectKeyword('as');
const local = this.parseImportElement();
if (local.kind !== first.kind)
throw new Error('local and remote name must be same kind of reference');
this.expectToken(graphql_1.TokenKind.PAREN_R);
return this.node(start, {
type: 'Import',
element: first,
alias: local
});
}
return this.node(start, {
type: 'Import',
element: first
});
}
parseImportElement() {
if (this.peek(graphql_1.TokenKind.AT))
return this.parseDirectiveName();
return this.parseNamedType();
}
parseDirectiveName() {
const start = this._lexer.token;
const at = this.expectToken(graphql_1.TokenKind.AT);
if (this.peek(graphql_1.TokenKind.NAME)) {
const tok = this._lexer.token;
if (tok.line === at.line && tok.column === at.column + 1)
return this.node(start, {
kind: graphql_1.Kind.DIRECTIVE,
name: this.parseName()
});
}
return this.node(start, {
kind: graphql_1.Kind.DIRECTIVE,
name: this.node(at, { kind: graphql_1.Kind.NAME, value: '' })
});
}
}
exports.ImportsParser = ImportsParser;
//# sourceMappingURL=import.js.map