@inrupt/experimental-graphql-directives-linked-data
Version:
GraphQL directives for Linked Data
77 lines (76 loc) • 4.34 kB
JavaScript
//
// 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.
//
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.orderBy = void 0;
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 graphql_1 = require("graphql");
const rdf_literal_1 = require("rdf-literal");
const utils_2 = require("../utils");
/* @experimental */
function orderBy(directiveName) {
return (schema) => (0, utils_1.mapSchema)(schema, {
[utils_1.MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const directive = (0, experimental_graphql_directives_utils_1.getSingleDirective)(schema, fieldConfig, directiveName);
if (directive) {
// Get this field's original resolver
const resolve = (0, experimental_graphql_directives_utils_1.getResolver)(fieldConfig);
if (!(0, graphql_1.isListType)((0, graphql_1.isNonNullType)(fieldConfig.type)
? fieldConfig.type.ofType
: fieldConfig.type)) {
throw new Error(`@${directiveName} can only be applied to list types`);
}
const node = (0, utils_2.nodeFromDirective)(directive, directiveName);
fieldConfig.resolve = (source, args, context, info) => __awaiter(this, void 0, void 0, function* () {
const list = yield resolve(source, args, context, info);
if (!Array.isArray(list)) {
throw new Error(`Order by expects array of elements, received ${typeof list}`);
}
// TODO: Fix this logic
const orderings = yield Promise.all(list.map((elem) => (0, experimental_sparql_utils_1.queryObject)(context, elem, node).then((e) => {
if (e.termType === "Literal") {
return { term: elem, ordering: (0, rdf_literal_1.fromRdf)(e) };
}
return { term: elem, ordering: e.value };
})));
return orderings
.sort((a, b) =>
// eslint-disable-next-line no-nested-ternary
a.ordering === b.ordering ? 0 : a.ordering > b.ordering ? 1 : -1)
.map((e) => e.term);
});
}
return fieldConfig;
},
});
}
exports.orderBy = orderBy;
;