Your IP : 216.73.217.13


Current Path : /home/deltalab/PMS/ims-connector/node_modules/graphql-compose-mongoose/lib/utils/
Upload File :
Current File : //home/deltalab/PMS/ims-connector/node_modules/graphql-compose-mongoose/lib/utils/testHelpers.js

"use strict";
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());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.testFieldConfig = void 0;
const graphql_compose_1 = require("graphql-compose");
const graphql_1 = require("graphql-compose/lib/graphql");
const FIELD = 'test_field';
function testOperation(opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = yield (0, graphql_1.graphql)({
            schema: opts.schemaComposer.buildSchema(),
            source: opts.operation,
            rootValue: (opts === null || opts === void 0 ? void 0 : opts.source) || {},
            contextValue: (opts === null || opts === void 0 ? void 0 : opts.context) || {},
            variableValues: opts === null || opts === void 0 ? void 0 : opts.variables,
        });
        return res;
    });
}
function testFieldConfig(opts) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        const { field, selection, args } = opts, restOpts = __rest(opts, ["field", "selection", "args"]);
        const sc = (opts === null || opts === void 0 ? void 0 : opts.schemaComposer) || new graphql_compose_1.SchemaComposer();
        sc.Query.setField(FIELD, field);
        const ac = _getArgsForQuery(field, args, sc);
        const selectionSet = selection.trim();
        if (!selectionSet.startsWith('{') || !selectionSet.endsWith('}')) {
            throw new Error(`Error in testFieldConfig({ selection: '...' }) – selection must be a string started from "{" and ended with "}"`);
        }
        const res = yield testOperation(Object.assign(Object.assign({}, restOpts), { variables: args, operation: `
      query ${ac.queryVars} {
        ${FIELD}${ac.fieldVars} ${selectionSet}
      }
    `, schemaComposer: sc }));
        if (res.errors) {
            throw new Error(((_a = res === null || res === void 0 ? void 0 : res.errors) === null || _a === void 0 ? void 0 : _a[0]) || 'GraphQL Error');
        }
        return (_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b[FIELD];
    });
}
exports.testFieldConfig = testFieldConfig;
function _getArgsForQuery(fc, variables = {}, schemaComposer) {
    const sc = schemaComposer || new graphql_compose_1.SchemaComposer();
    sc.Query.setField(FIELD, fc);
    const varNames = Object.keys(variables);
    const argNames = sc.Query.getFieldArgNames(FIELD);
    if (argNames.length === 0 && varNames.length > 0) {
        throw new Error(`FieldConfig does not have any arguments. But in test you provided the following variables: ${(0, graphql_compose_1.inspect)(variables)}`);
    }
    varNames.forEach((varName) => {
        if (!argNames.includes(varName)) {
            throw new Error(`FieldConfig does not have '${varName}' argument. Available arguments: '${argNames.join("', '")}'.`);
        }
    });
    argNames.forEach((argName) => {
        if (sc.Query.isFieldArgNonNull(FIELD, argName)) {
            const val = variables[argName];
            if (val === null || val === undefined) {
                throw new Error(`FieldConfig has required argument '${argName}'. But you did not provide it in your test via variables: '${(0, graphql_compose_1.inspect)(variables)}'.`);
            }
        }
    });
    const queryVars = varNames
        .map((n) => `$${n}: ${String(sc.Query.getFieldArgType(FIELD, n))}`)
        .join(' ');
    const fieldVars = varNames.map((n) => `${n}: $${n}`).join(' ');
    return {
        queryVars: queryVars ? `(${queryVars})` : '',
        fieldVars: fieldVars ? `(${fieldVars})` : '',
    };
}
//# sourceMappingURL=testHelpers.js.map