all files / src/ link.js

89.42% Statements 93/104
79.75% Branches 63/79
86.21% Functions 25/29
91.4% Lines 85/93
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                          22×   21×   25×       16× 16× 16× 16×           13× 23×             64× 64× 64× 11× 11×   53× 53× 42×     28× 33× 33×           62× 62× 62× 36×   62× 58×   62×   62×   62× 62× 39×     23×   62× 56× 55× 55× 55× 55× 55× 55×           62×   138× 138× 138× 138×          
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var linkUtils_1 = require("./linkUtils");
var parser_1 = require("graphql/language/parser");
var Observable = require("zen-observable");
var ApolloLink = (function () {
    function ApolloLink() {
    }
    ApolloLink.from = function (links) {
        if (links.length === 0) {
            return ApolloLink.empty();
        }
        return links
            .map(linkUtils_1.toLink)
            .reduce(function (x, y) { return x.concat(y); });
    };
    ApolloLink.empty = function () {
        return new FunctionLink(function (op, forward) { return Observable.of(); });
    };
    ApolloLink.passthrough = function () {
        return new FunctionLink(function (op, forward) { return forward ? forward(op) : Observable.of(); });
    };
    ApolloLink.split = function (test, left, right) {
        if (right === void 0) { right = ApolloLink.passthrough(); }
        var leftLink = linkUtils_1.validateLink(linkUtils_1.toLink(left));
        var rightLink = linkUtils_1.validateLink(linkUtils_1.toLink(right));
        if (linkUtils_1.isTerminating(leftLink) && linkUtils_1.isTerminating(rightLink)) {
            return new FunctionLink(function (operation) {
                return test(operation) ?
                    leftLink.request(operation) || Observable.of() :
                    rightLink.request(operation) || Observable.of();
            });
        }
        else {
            return new FunctionLink(function (operation, forward) {
                return test(operation) ?
                    leftLink.request(operation, forward) || Observable.of() :
                    rightLink.request(operation, forward) || Observable.of();
            });
        }
    };
    ApolloLink.prototype.split = function (test, left, right) {
        if (right === void 0) { right = ApolloLink.passthrough(); }
        return this.concat(ApolloLink.split(test, left, right));
    };
    ApolloLink.prototype.concat = function (next) {
        var _this = this;
        linkUtils_1.validateLink(this);
        if (linkUtils_1.isTerminating(this)) {
            console.warn(new linkUtils_1.LinkError("You are calling concat on a terminating link, which will have no effect", this));
            return this;
        }
        var nextLink = linkUtils_1.validateLink(linkUtils_1.toLink(next));
        if (linkUtils_1.isTerminating(nextLink)) {
            return new FunctionLink(function (operation) { return _this.request(operation, function (op) { return nextLink.request(op) || Observable.of(); }) || Observable.of(); });
        }
        else {
            return new FunctionLink(function (operation, forward) {
                return _this.request(operation, function (op) {
                    return nextLink.request(op, forward) || Observable.of();
                }) || Observable.of();
            });
        }
    };
    return ApolloLink;
}());
exports.ApolloLink = ApolloLink;
function execute(link, operation) {
    var copy = __assign({}, operation);
    linkUtils_1.validateOperation(copy);
    if (!copy.context) {
        copy.context = {};
    }
    if (!copy.variables) {
        copy.variables = {};
    }
    if (!copy.query) {
        console.warn("query should either be a string or GraphQL AST");
        copy.query = {};
    }
    return link.request(transformOperation(copy)) || Observable.of();
}
exports.execute = execute;
function transformOperation(operation) {
    var transformedOperation;
    if (typeof operation.query === 'string') {
        transformedOperation = __assign({}, operation, { query: parser_1.parse(operation.query) });
    }
    else {
        transformedOperation = __assign({}, operation);
    }
    if (!transformedOperation.operationName) {
        if (transformedOperation.query && transformedOperation.query.definitions) {
            var operationTypes_1 = ['query', 'mutation', 'subscription'];
            var definitions = transformedOperation.query.definitions.filter(function (x) { return x.kind === 'OperationDefinition' && (operationTypes_1.indexOf(x.operation) >= 0); });
            Eif (definitions.length) {
                var definition = definitions[0];
                var hasName = definition.name && definition.name.kind === 'Name';
                transformedOperation.operationName = hasName ? definitions[0].name.value : '';
            }
        }
        else {
            transformedOperation.operationName = '';
        }
    }
    return transformedOperation;
}
var FunctionLink = (function (_super) {
    __extends(FunctionLink, _super);
    function FunctionLink(f) {
        var _this = _super.call(this) || this;
        _this.f = f;
        _this.request = f;
        return _this;
    }
    FunctionLink.prototype.request = function (operation, forward) {
        throw Error('should be overridden');
    };
    return FunctionLink;
}(ApolloLink));
exports.FunctionLink = FunctionLink;
//# sourceMappingURL=link.js.map