UNPKG

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