UNPKG

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