@inrupt/experimental-graphql-directives-linked-data
Version:
GraphQL directives for Linked Data
66 lines (65 loc) • 3.65 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.is = void 0;
//
// Copyright 2022 Inrupt Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
const utils_1 = require("@graphql-tools/utils");
const experimental_graphql_directives_utils_1 = require("@inrupt/experimental-graphql-directives-utils");
const experimental_sparql_utils_1 = require("@inrupt/experimental-sparql-utils");
const n3_1 = require("n3");
// TODO: Check the ordering of resolution calls
function is(directiveName) {
return (schema) => (0, utils_1.mapSchema)(schema, {
[utils_1.MapperKind.OBJECT_FIELD]: (fieldConfig) => {
var _a, _b;
const typeName = (_b = (_a = fieldConfig.type) === null || _a === void 0 ? void 0 : _a.ofType) === null || _b === void 0 ? void 0 : _b.name;
if (typeName) {
const directiveNode = schema.getType(typeName);
if (!directiveNode) {
throw new Error(`Could not find directive name`);
}
const directive = (0, experimental_graphql_directives_utils_1.getSingleDirective)(schema, directiveNode, directiveName);
if (directive) {
const c = directive.class;
const resolve = (0, experimental_graphql_directives_utils_1.getResolver)(fieldConfig);
fieldConfig.resolve = (...args) => __awaiter(this, void 0, void 0, function* () {
const resolved = resolve(...args);
if (yield (0, experimental_sparql_utils_1.isType)(args[2], resolved, n3_1.DataFactory.namedNode(c))) {
return resolved;
}
throw new Error(`Expected ${(yield resolved).value} to be of type ${c}`);
});
}
}
return fieldConfig;
},
});
}
exports.is = is;
;