UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var apollo_utilities_1 = require("apollo-utilities");
4var graphql_1 = require("./graphql");
5var ts_invariant_1 = require("ts-invariant");
6var hasOwnProperty = Object.prototype.hasOwnProperty;
7function filter(doc, data, variableValues) {
8 if (variableValues === void 0) { variableValues = {}; }
9 if (data === null)
10 return data;
11 var resolver = function (fieldName, root, args, context, info) {
12 return root[info.resultKey];
13 };
14 return Array.isArray(data)
15 ? data.map(function (dataObj) { return graphql_1.graphql(resolver, doc, dataObj, null, variableValues); })
16 : graphql_1.graphql(resolver, doc, data, null, variableValues);
17}
18exports.filter = filter;
19function check(doc, data, variables) {
20 if (variables === void 0) { variables = {}; }
21 var resolver = function (fieldName, root, args, context, info) {
22 ts_invariant_1.invariant(hasOwnProperty.call(root, info.resultKey) ||
23 (!variables && hasVariableInclusions(info.field.directives)), info.resultKey + " missing on " + JSON.stringify(root));
24 return root[info.resultKey];
25 };
26 graphql_1.graphql(resolver, doc, data, {}, variables, {
27 fragmentMatcher: function () { return false; },
28 });
29}
30exports.check = check;
31function hasVariableInclusions(directives) {
32 return apollo_utilities_1.getInclusionDirectives(directives).some(function (_a) {
33 var ifArgument = _a.ifArgument;
34 return ifArgument.value && ifArgument.value.kind === 'Variable';
35 });
36}
37var ANONYMOUS = '<<anonymous>>';
38function PropTypeError(message) {
39 this.message = message;
40 this.stack = '';
41}
42PropTypeError.prototype = Error.prototype;
43var reactPropTypeLocationNames = {
44 prop: 'prop',
45 context: 'context',
46 childContext: 'child context',
47};
48function createChainableTypeChecker(validate) {
49 function checkType(isRequired, props, propName, componentName, location, propFullName) {
50 componentName = componentName || ANONYMOUS;
51 propFullName = propFullName || propName;
52 if (props[propName] == null) {
53 var locationName = reactPropTypeLocationNames[location];
54 if (isRequired) {
55 if (props[propName] === null) {
56 return new PropTypeError("The " + locationName + " `" + propFullName + "` is marked as required " +
57 ("in `" + componentName + "`, but its value is `null`."));
58 }
59 return new PropTypeError("The " + locationName + " `" + propFullName + "` is marked as required in " +
60 ("`" + componentName + "`, but its value is `undefined`."));
61 }
62 return null;
63 }
64 else {
65 return validate(props, propName, componentName, location, propFullName);
66 }
67 }
68 var chainedCheckType = checkType.bind(null, false);
69 chainedCheckType.isRequired = checkType.bind(null, true);
70 return chainedCheckType;
71}
72function propType(doc, mapPropsToVariables) {
73 if (mapPropsToVariables === void 0) { mapPropsToVariables = function (props) { return null; }; }
74 return createChainableTypeChecker(function (props, propName) {
75 var prop = props[propName];
76 try {
77 if (!prop.loading) {
78 check(doc, prop, mapPropsToVariables(props));
79 }
80 return null;
81 }
82 catch (e) {
83 return e;
84 }
85 });
86}
87exports.propType = propType;
88//# sourceMappingURL=utilities.js.map
\No newline at end of file