@apollo/core-schema
Version:
Apollo Core Schema processing library
256 lines • 9.79 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.including = exports.Scope = exports.ErrExtraImport = void 0;
const recall_1 = __importStar(require("@protoplasm/recall"));
const graphql_1 = require("graphql");
const linker_1 = require("./linker");
const de_1 = require("./de");
const gref_1 = __importDefault(require("./gref"));
const is_1 = require("./is");
const names_1 = require("./names");
const scope_map_1 = __importDefault(require("./scope-map"));
const error_1 = __importDefault(require("./error"));
const ErrExtraImport = (gref, node) => (0, error_1.default)('ExtraImport', {
message: `extra import of ${gref} ignored`,
gref, node
});
exports.ErrExtraImport = ErrExtraImport;
class Scope {
constructor(parent) {
var _b, _c;
this.parent = parent;
this.names = new scope_map_1.default((_b = this.parent) === null || _b === void 0 ? void 0 : _b.names);
this.reverse = new scope_map_1.default((_c = this.parent) === null || _c === void 0 ? void 0 : _c.reverse);
}
static create(fn, parent) {
const child = new this(parent);
if (fn)
fn(child);
return Object.freeze(child);
}
get self() { return this.names.lookup(''); }
get url() { var _b; return (_b = this.self) === null || _b === void 0 ? void 0 : _b.gref.graph; }
locate(node) {
var _b, _c, _d, _e, _f;
if ((0, de_1.hasRef)(node))
return node.gref;
if ((0, is_1.isAst)(node, graphql_1.Kind.SCHEMA_DEFINITION, graphql_1.Kind.SCHEMA_EXTENSION)) {
return gref_1.default.schema(this.url);
}
const [prefix, name] = (0, names_1.getPrefix)((_c = (_b = node.name) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : '');
if (prefix) {
const found = this.lookup(prefix);
if (found)
return gref_1.default.canon((0, names_1.scopeNameFor)(node, name), found.gref.graph);
}
if ((0, is_1.isAst)(node, graphql_1.Kind.DIRECTIVE) && !prefix) {
const named = (_d = this.lookup((0, names_1.scopeNameFor)(node))) === null || _d === void 0 ? void 0 : _d.gref;
if (named)
return named;
const maybeNs = this.lookup(name);
if (maybeNs === null || maybeNs === void 0 ? void 0 : maybeNs.gref.isSchema()) {
return gref_1.default.rootDirective(maybeNs.gref.graph);
}
}
return (_f = (_e = this.lookup((0, names_1.scopeNameFor)(node))) === null || _e === void 0 ? void 0 : _e.gref) !== null && _f !== void 0 ? _f : gref_1.default.canon((0, names_1.scopeNameFor)(node), this.url);
}
header() {
const directives = [...this.linker.synthesize(this)];
if (directives.length) {
return [{ kind: graphql_1.Kind.SCHEMA_EXTENSION, directives, gref: gref_1.default.schema(this.url) }];
}
return [];
}
name(gref) {
const bareName = this.reverse.lookup(gref);
if (bareName)
return [null, bareName];
const prefix = this.reverse.lookup(gref.setName(''));
if (prefix)
return [prefix, gref.name];
return;
}
denormalize(node) {
const self = this;
return (0, graphql_1.visit)(node, {
enter(node, _) {
if ((0, is_1.isAst)(node, graphql_1.Kind.INPUT_VALUE_DEFINITION))
return;
if ((0, is_1.isAst)(node, graphql_1.Kind.ENUM_VALUE_DEFINITION))
return;
if ((0, de_1.isLocatable)(node)) {
return { ...node, gref: self.locate(node) };
}
return;
}
});
}
renormalize(node, redirects) {
const self = this;
return (0, graphql_1.visit)(node, {
enter(node, _) {
if ((0, is_1.isAst)(node, graphql_1.Kind.INPUT_VALUE_DEFINITION))
return;
if (!(0, is_1.hasName)(node) || !(0, de_1.isLocated)(node))
return;
const path = self.name(redirect(node.gref, redirects));
if (!path)
return;
return {
...node,
name: { ...node.name, value: (0, names_1.toPrefixed)(path) }
};
}
});
}
*renormalizeDefs(defs) {
const redirects = new Map();
const onlyDefs = [];
for (const redir of defs)
if ((0, de_1.isRedirect)(redir)) {
const existing = redirects.get(redir.gref);
if (existing) {
if (existing.toGref !== redir.toGref)
(0, recall_1.report)((0, exports.ErrExtraImport)(redir.gref, redir.via));
continue;
}
redirects.set(redir.gref, redir);
}
else {
onlyDefs.push(redir);
}
for (const def of onlyDefs)
if ((0, de_1.isRedirect)(def))
continue;
else
yield this.renormalize(def, redirects);
}
*[Symbol.iterator]() {
for (const ent of this.entries())
yield ent[1];
}
get flat() {
return Scope.create(scope => {
for (const [_, link] of this.visible())
scope.add(link);
});
}
own(name) { return this.names.own(name); }
has(name) { return this.names.has(name); }
hasOwn(name) { return this.names.hasOwn(name); }
lookup(name) { return this.names.lookup(name); }
visible() { return this.names.visible(); }
entries() { return this.names.entries(); }
child(fn) {
return Scope.create(fn, this);
}
clone(fn) {
return Scope.create(scope => {
for (const [_, link] of this.entries())
scope.add(link);
if (fn)
fn(scope);
}, this.parent);
}
get linker() {
var _b, _c;
for (const link of this) {
const linker = link.linker ? linker_1.Linker.bootstrap(link.linker) : null;
if (linker)
return linker;
}
return (_c = (_b = this.parent) === null || _b === void 0 ? void 0 : _b.linker) !== null && _c !== void 0 ? _c : linker_1.Linker.DEFAULT;
}
add(link) {
this.names.set(link.name, link);
this.reverse.set(link.gref, link.name);
}
}
_a = Scope;
Scope.EMPTY = _a.create();
__decorate([
(0, recall_1.use)(recall_1.default)
], Scope.prototype, "denormalize", null);
__decorate([
(0, recall_1.use)(recall_1.default)
], Scope.prototype, "renormalize", null);
exports.Scope = Scope;
exports.default = Scope;
const including = (refs) => (scope) => {
for (const node of refs) {
if ((0, de_1.isRedirect)(node)) {
const src = scope.name(node.gref);
if (!src)
continue;
const [prefix, name] = src;
if (prefix)
continue;
scope.add({
...scope.lookup(name),
name,
gref: node.toGref,
});
}
else {
const graph = node.gref.graph;
if (!graph)
continue;
const found = scope.name(node.gref);
if (found)
continue;
addGraph(graph);
}
}
function addGraph(graph) {
for (const name of graph.suggestNames()) {
if (scope.has(name))
continue;
scope.add({
name, gref: gref_1.default.schema(graph)
});
break;
}
}
};
exports.including = including;
function redirect(gref, redirects) {
if (!redirects)
return gref;
while (redirects.has(gref))
gref = redirects.get(gref).toGref;
return gref;
}
//# sourceMappingURL=scope.js.map