UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var apollo_utilities_1 = require("apollo-utilities");
4function graphql(resolver, document, rootValue, contextValue, variableValues, execOptions) {
5 if (variableValues === void 0) { variableValues = {}; }
6 if (execOptions === void 0) { execOptions = {}; }
7 var mainDefinition = apollo_utilities_1.getMainDefinition(document);
8 var fragments = apollo_utilities_1.getFragmentDefinitions(document);
9 var fragmentMap = apollo_utilities_1.createFragmentMap(fragments);
10 var resultMapper = execOptions.resultMapper;
11 var fragmentMatcher = execOptions.fragmentMatcher || (function () { return true; });
12 var execContext = {
13 fragmentMap: fragmentMap,
14 contextValue: contextValue,
15 variableValues: variableValues,
16 resultMapper: resultMapper,
17 resolver: resolver,
18 fragmentMatcher: fragmentMatcher,
19 };
20 return executeSelectionSet(mainDefinition.selectionSet, rootValue, execContext);
21}
22exports.graphql = graphql;
23function executeSelectionSet(selectionSet, rootValue, execContext) {
24 var fragmentMap = execContext.fragmentMap, contextValue = execContext.contextValue, variables = execContext.variableValues;
25 var result = {};
26 selectionSet.selections.forEach(function (selection) {
27 if (variables && !apollo_utilities_1.shouldInclude(selection, variables)) {
28 return;
29 }
30 if (apollo_utilities_1.isField(selection)) {
31 var fieldResult = executeField(selection, rootValue, execContext);
32 var resultFieldKey = apollo_utilities_1.resultKeyNameFromField(selection);
33 if (fieldResult !== undefined) {
34 if (result[resultFieldKey] === undefined) {
35 result[resultFieldKey] = fieldResult;
36 }
37 else {
38 merge(result[resultFieldKey], fieldResult);
39 }
40 }
41 }
42 else {
43 var fragment = void 0;
44 if (apollo_utilities_1.isInlineFragment(selection)) {
45 fragment = selection;
46 }
47 else {
48 fragment = fragmentMap[selection.name.value];
49 if (!fragment) {
50 throw new Error("No fragment named " + selection.name.value);
51 }
52 }
53 var typeCondition = fragment.typeCondition.name.value;
54 if (execContext.fragmentMatcher(rootValue, typeCondition, contextValue)) {
55 var fragmentResult = executeSelectionSet(fragment.selectionSet, rootValue, execContext);
56 merge(result, fragmentResult);
57 }
58 }
59 });
60 if (execContext.resultMapper) {
61 return execContext.resultMapper(result, rootValue);
62 }
63 return result;
64}
65function executeField(field, rootValue, execContext) {
66 var variables = execContext.variableValues, contextValue = execContext.contextValue, resolver = execContext.resolver;
67 var fieldName = field.name.value;
68 var args = apollo_utilities_1.argumentsObjectFromField(field, variables);
69 var info = {
70 isLeaf: !field.selectionSet,
71 resultKey: apollo_utilities_1.resultKeyNameFromField(field),
72 directives: apollo_utilities_1.getDirectiveInfoFromField(field, variables),
73 field: field,
74 };
75 var result = resolver(fieldName, rootValue, args, contextValue, info);
76 if (!field.selectionSet) {
77 return result;
78 }
79 if (result == null) {
80 return result;
81 }
82 if (Array.isArray(result)) {
83 return executeSubSelectedArray(field, result, execContext);
84 }
85 return executeSelectionSet(field.selectionSet, result, execContext);
86}
87function executeSubSelectedArray(field, result, execContext) {
88 return result.map(function (item) {
89 if (item === null) {
90 return null;
91 }
92 if (Array.isArray(item)) {
93 return executeSubSelectedArray(field, item, execContext);
94 }
95 return executeSelectionSet(field.selectionSet, item, execContext);
96 });
97}
98var hasOwn = Object.prototype.hasOwnProperty;
99function merge(dest, src) {
100 if (src !== null && typeof src === 'object') {
101 Object.keys(src).forEach(function (key) {
102 var srcVal = src[key];
103 if (!hasOwn.call(dest, key)) {
104 dest[key] = srcVal;
105 }
106 else {
107 merge(dest[key], srcVal);
108 }
109 });
110 }
111}
112exports.merge = merge;
113//# sourceMappingURL=graphql.js.map
\No newline at end of file