1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var apollo_utilities_1 = require("apollo-utilities");
|
4 | var graphql_1 = require("./graphql");
|
5 | var ts_invariant_1 = require("ts-invariant");
|
6 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
7 | function 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 | }
|
18 | exports.filter = filter;
|
19 | function 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 | }
|
30 | exports.check = check;
|
31 | function 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 | }
|
37 | var ANONYMOUS = '<<anonymous>>';
|
38 | function PropTypeError(message) {
|
39 | this.message = message;
|
40 | this.stack = '';
|
41 | }
|
42 | PropTypeError.prototype = Error.prototype;
|
43 | var reactPropTypeLocationNames = {
|
44 | prop: 'prop',
|
45 | context: 'context',
|
46 | childContext: 'child context',
|
47 | };
|
48 | function 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 | }
|
72 | function 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 | }
|
87 | exports.propType = propType;
|
88 |
|
\ | No newline at end of file |