UNPKG

5.13 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports", "apollo-utilities"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require("apollo-utilities"));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.apolloUtilities);
11 global.unknown = mod.exports;
12 }
13})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _apolloUtilities) {
14
15 _exports.__esModule = true;
16 _exports.Cache = _exports.ApolloCache = void 0;
17
18 function queryFromPojo(obj) {
19 var op = {
20 kind: 'OperationDefinition',
21 operation: 'query',
22 name: {
23 kind: 'Name',
24 value: 'GeneratedClientQuery'
25 },
26 selectionSet: selectionSetFromObj(obj)
27 };
28 var out = {
29 kind: 'Document',
30 definitions: [op]
31 };
32 return out;
33 }
34
35 function fragmentFromPojo(obj, typename) {
36 var frag = {
37 kind: 'FragmentDefinition',
38 typeCondition: {
39 kind: 'NamedType',
40 name: {
41 kind: 'Name',
42 value: typename || '__FakeType'
43 }
44 },
45 name: {
46 kind: 'Name',
47 value: 'GeneratedClientQuery'
48 },
49 selectionSet: selectionSetFromObj(obj)
50 };
51 var out = {
52 kind: 'Document',
53 definitions: [frag]
54 };
55 return out;
56 }
57
58 function selectionSetFromObj(obj) {
59 if (typeof obj === 'number' || typeof obj === 'boolean' || typeof obj === 'string' || typeof obj === 'undefined' || obj === null) {
60 return null;
61 }
62
63 if (Array.isArray(obj)) {
64 return selectionSetFromObj(obj[0]);
65 }
66
67 var selections = [];
68 Object.keys(obj).forEach(function (key) {
69 var nestedSelSet = selectionSetFromObj(obj[key]);
70 var field = {
71 kind: 'Field',
72 name: {
73 kind: 'Name',
74 value: key
75 },
76 selectionSet: nestedSelSet || undefined
77 };
78 selections.push(field);
79 });
80 var selectionSet = {
81 kind: 'SelectionSet',
82 selections: selections
83 };
84 return selectionSet;
85 }
86
87 var justTypenameQuery = {
88 kind: 'Document',
89 definitions: [{
90 kind: 'OperationDefinition',
91 operation: 'query',
92 name: null,
93 variableDefinitions: null,
94 directives: [],
95 selectionSet: {
96 kind: 'SelectionSet',
97 selections: [{
98 kind: 'Field',
99 alias: null,
100 name: {
101 kind: 'Name',
102 value: '__typename'
103 },
104 arguments: [],
105 directives: [],
106 selectionSet: null
107 }]
108 }
109 }]
110 };
111
112 var ApolloCache = function () {
113 function ApolloCache() {}
114
115 ApolloCache.prototype.transformDocument = function (document) {
116 return document;
117 };
118
119 ApolloCache.prototype.transformForLink = function (document) {
120 return document;
121 };
122
123 ApolloCache.prototype.readQuery = function (options, optimistic) {
124 if (optimistic === void 0) {
125 optimistic = false;
126 }
127
128 return this.read({
129 query: options.query,
130 variables: options.variables,
131 optimistic: optimistic
132 });
133 };
134
135 ApolloCache.prototype.readFragment = function (options, optimistic) {
136 if (optimistic === void 0) {
137 optimistic = false;
138 }
139
140 return this.read({
141 query: (0, _apolloUtilities.getFragmentQueryDocument)(options.fragment, options.fragmentName),
142 variables: options.variables,
143 rootId: options.id,
144 optimistic: optimistic
145 });
146 };
147
148 ApolloCache.prototype.writeQuery = function (options) {
149 this.write({
150 dataId: 'ROOT_QUERY',
151 result: options.data,
152 query: options.query,
153 variables: options.variables
154 });
155 };
156
157 ApolloCache.prototype.writeFragment = function (options) {
158 this.write({
159 dataId: options.id,
160 result: options.data,
161 variables: options.variables,
162 query: (0, _apolloUtilities.getFragmentQueryDocument)(options.fragment, options.fragmentName)
163 });
164 };
165
166 ApolloCache.prototype.writeData = function (_a) {
167 var id = _a.id,
168 data = _a.data;
169
170 if (typeof id !== 'undefined') {
171 var typenameResult = null;
172
173 try {
174 typenameResult = this.read({
175 rootId: id,
176 optimistic: false,
177 query: justTypenameQuery
178 });
179 } catch (e) {}
180
181 var __typename = typenameResult && typenameResult.__typename || '__ClientData';
182
183 var dataToWrite = Object.assign({
184 __typename: __typename
185 }, data);
186 this.writeFragment({
187 id: id,
188 fragment: fragmentFromPojo(dataToWrite, __typename),
189 data: dataToWrite
190 });
191 } else {
192 this.writeQuery({
193 query: queryFromPojo(data),
194 data: data
195 });
196 }
197 };
198
199 return ApolloCache;
200 }();
201
202 _exports.ApolloCache = ApolloCache;
203 var Cache;
204 _exports.Cache = Cache;
205
206 (function (Cache) {})(Cache || (_exports.Cache = Cache = {}));
207
208});