UNPKG

@apollo/core-schema

Version:
335 lines 13.9 kB
"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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _Linker_link; Object.defineProperty(exports, "__esModule", { value: true }); exports.Linker = exports.id = exports.ErrBadImport = exports.LINK_SPEC_URLS = exports.LINK_DIRECTIVES = void 0; const recall_1 = __importStar(require("@protoplasm/recall")); const graphql_1 = require("graphql"); const values_1 = require("graphql/execution/values"); const import_1 = require("./import"); const link_url_1 = require("./link-url"); const gref_1 = require("./gref"); const names_1 = require("./names"); const each_1 = require("./each"); const is_1 = require("./is"); const error_1 = __importDefault(require("./error")); const gql_1 = __importDefault(require("./gql")); const directives_1 = __importDefault(require("./directives")); const LINK_SPECS = new Map([ ['https://specs.apollo.dev/core/v0.1', 'feature'], ['https://specs.apollo.dev/core/v0.2', 'feature'], ['https://specs.apollo.dev/link/v0.3', 'url'], ['https://specs.apollo.dev/link/v1.0', 'url'], ]); exports.LINK_DIRECTIVES = new Set([...LINK_SPECS.keys()].map(url => gref_1.GRef.rootDirective(url))); exports.LINK_SPEC_URLS = new Set([...exports.LINK_DIRECTIVES].map(ref => ref.graph)); const Url = new graphql_1.GraphQLScalarType({ name: 'Url', parseValue: val => val, parseLiteral(node) { if (node.kind === 'StringValue') return link_url_1.LinkUrl.parse(node.value); return null; } }); const Name = new graphql_1.GraphQLScalarType({ name: 'Name', parseValue: val => val, parseLiteral(node) { if (node.kind === 'StringValue') return node.value; if (node.kind === 'EnumValue') return node.value; return; } }); const ErrBadImport = (node, expectedKinds) => (0, error_1.default)('BadImport', { message: `expected node of kind ${expectedKinds.join(' | ')}, got ${node.kind}`, node, expectedKinds }); exports.ErrBadImport = ErrBadImport; const Imports = new graphql_1.GraphQLScalarType({ name: 'Imports', parseValue: val => val, parseLiteral(value) { if (value.kind === graphql_1.Kind.LIST) { const text = value.values.map(value => { var _b; if (value.kind === graphql_1.Kind.STRING) return value.value; if (value.kind === graphql_1.Kind.OBJECT) { const name = (0, each_1.only)((0, is_1.byName)(value.fields).get('name')).value; const alias = (_b = (0, each_1.maybeOne)((0, is_1.byName)(value.fields).get('as'))) === null || _b === void 0 ? void 0 : _b.value; if (!(0, is_1.isAst)(name, graphql_1.Kind.STRING, graphql_1.Kind.ENUM)) { (0, recall_1.report)((0, exports.ErrBadImport)(name, [graphql_1.Kind.STRING, graphql_1.Kind.ENUM])); return; } if (alias && !(0, is_1.isAst)(alias, graphql_1.Kind.STRING, graphql_1.Kind.ENUM)) { (0, recall_1.report)((0, exports.ErrBadImport)(alias, [graphql_1.Kind.STRING, graphql_1.Kind.ENUM])); return; } if (alias && alias.value !== name.value) return `${alias.value} : ${name.value}`; return name.value; } return undefined; }).filter(Boolean).join(' '); return import_1.ImportsParser.fromString(text); } if (value.kind !== graphql_1.Kind.STRING) return; return import_1.ImportsParser.fromString(value.value); } }); const $bootstrap = new graphql_1.GraphQLDirective({ name: 'link', args: { url: { type: Url }, feature: { type: Url }, as: { type: Name }, }, locations: [graphql_1.DirectiveLocation.SCHEMA], isRepeatable: true, }); const $id = new graphql_1.GraphQLDirective({ name: 'id', args: { url: { type: new graphql_1.GraphQLNonNull(Url) }, as: { type: Name }, }, locations: [graphql_1.DirectiveLocation.SCHEMA], isRepeatable: true, }); const ID_DIRECTIVE = gref_1.GRef.rootDirective('https://specs.apollo.dev/id/v1.0'); exports.id = (0, recall_1.default)(function id(scope, dir) { var _b; if (scope.locate(dir) === ID_DIRECTIVE) { const args = (0, values_1.getArgumentValues)($id, dir); const url = args.url; const name = ((_b = args.as) !== null && _b !== void 0 ? _b : url.name); return { name, gref: gref_1.GRef.schema(url), via: dir, }; } return null; }); class Linker { constructor(strap, url, urlParam) { this.strap = strap; this.url = url; this.urlParam = urlParam; _Linker_link.set(this, new graphql_1.GraphQLDirective({ name: this.strap.name.value, args: { [this.urlParam]: { type: new graphql_1.GraphQLNonNull(Url) }, as: { type: Name }, import: { type: Imports }, }, locations: [graphql_1.DirectiveLocation.SCHEMA], isRepeatable: true, })); } static from(scope, dir) { const self = this.bootstrap(dir); if (self) return self; const other = scope.lookup('@' + dir.name.value); if (!(other === null || other === void 0 ? void 0 : other.via)) return; return Linker.bootstrap(other.via); } static bootstrap(strap) { var _b; const args = (0, values_1.getArgumentValues)($bootstrap, strap); const url = ((_b = args.url) !== null && _b !== void 0 ? _b : args.feature); if (!url) return; const urlArg = LINK_SPECS.get(url.href); if (!urlArg) return; if (args[urlArg] !== url) return; return new this(strap, url, urlArg); } *links(directive) { var _b, _c, _d; const args = (0, values_1.getArgumentValues)(__classPrivateFieldGet(this, _Linker_link, "f"), directive); const url = args[this.urlParam]; const name = ((_b = args.as) !== null && _b !== void 0 ? _b : url.name); if (name !== '') { yield { name, gref: gref_1.GRef.schema(url), via: directive, linker: this.strap, }; yield { name: '@' + name, gref: gref_1.GRef.rootDirective(url), via: directive, linker: this.strap, implicit: true, }; } for (const i of (_c = args.import) !== null && _c !== void 0 ? _c : []) { const alias = (0, names_1.scopeNameFor)((_d = i.alias) !== null && _d !== void 0 ? _d : i.element); const name = (0, names_1.scopeNameFor)(i.element); yield { name: alias, gref: gref_1.GRef.named(name, url), via: directive, linker: this.strap, }; } } *synthesize(links) { const linksByUrl = byUrl(links); const urls = [...linksByUrl.keys()].sort((a, b) => (exports.LINK_SPEC_URLS.has(b) ? 1 : 0) - (exports.LINK_SPEC_URLS.has(a) ? 1 : 0)); for (const url of urls) { if (!url) continue; if (url === link_url_1.LinkUrl.GRAPHQL_SPEC) continue; const linksForUrl = linksByUrl.get(url); let alias = null; const imports = []; for (const link of linksForUrl) { if (!link.gref.name) { alias = link.name; continue; } if (link.gref.name === '@') continue; imports.push([link.name, link.gref.name]); } const args = [{ kind: graphql_1.Kind.ARGUMENT, name: { kind: graphql_1.Kind.NAME, value: this.urlParam }, value: { kind: graphql_1.Kind.STRING, value: url.href, }, }]; if (alias === '') { yield { kind: graphql_1.Kind.DIRECTIVE, name: { kind: graphql_1.Kind.NAME, value: "id" }, arguments: [{ kind: graphql_1.Kind.ARGUMENT, name: { kind: graphql_1.Kind.NAME, value: "url" }, value: { kind: graphql_1.Kind.STRING, value: url.href, }, }], gref: ID_DIRECTIVE, }; continue; } if (alias && alias !== url.name) { args.push({ kind: graphql_1.Kind.ARGUMENT, name: { kind: graphql_1.Kind.NAME, value: 'as', }, value: { kind: graphql_1.Kind.STRING, value: alias }, }); } if (imports.length) { args.push({ kind: graphql_1.Kind.ARGUMENT, name: { kind: graphql_1.Kind.NAME, value: 'import', }, value: { kind: graphql_1.Kind.LIST, values: imports.map(([alias, name]) => alias === name ? { kind: graphql_1.Kind.STRING, value: name } : { kind: graphql_1.Kind.OBJECT, fields: [ { kind: graphql_1.Kind.OBJECT_FIELD, name: { kind: graphql_1.Kind.NAME, value: "name" }, value: { kind: graphql_1.Kind.STRING, value: name } }, { kind: graphql_1.Kind.OBJECT_FIELD, name: { kind: graphql_1.Kind.NAME, value: "as" }, value: { kind: graphql_1.Kind.STRING, value: alias } } ] }) }, }); } yield { kind: graphql_1.Kind.DIRECTIVE, name: this.strap.name, arguments: args, gref: gref_1.GRef.rootDirective(this.url) }; } } } _a = Linker, _Linker_link = new WeakMap(); Linker.DEFAULT = _a.bootstrap((0, each_1.only)((0, directives_1.default)((0, gql_1.default) `@link(url: "https://specs.apollo.dev/link/v1.0")`))); __decorate([ (0, recall_1.use)(recall_1.replay) ], Linker.prototype, "links", null); __decorate([ (0, recall_1.use)(recall_1.default) ], Linker, "bootstrap", null); exports.Linker = Linker; const byUrl = (0, each_1.groupBy)((link) => link.gref.graph); //# sourceMappingURL=linker.js.map