MCPcopy Create free account
hub / github.com/dailydotdev/apps / flush

Function flush

packages/shared/src/graphql/batch.ts:127–212  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

125};
126
127const flush = async (): Promise<void> => {
128 flushHandle = undefined;
129
130 const items = queue.slice(0, GQL_MAX_BATCH_SIZE);
131 queue = queue.slice(GQL_MAX_BATCH_SIZE);
132
133 // Anything over the API's cap waits for the next window.
134 if (queue.length) {
135 flushHandle = setTimeout(flush, BATCH_WINDOW);
136 }
137
138 if (!items.length) {
139 return;
140 }
141
142 if (items.length === 1) {
143 await requestSingle(items[0]);
144
145 return;
146 }
147
148 const body = items.map(({ document, variables }) => {
149 const operationName = getOperationName(document);
150
151 return {
152 query: document,
153 variables,
154 ...(operationName && { operationName }),
155 };
156 });
157
158 let response: Response;
159
160 try {
161 response = await globalThis.fetch(graphqlUrl, {
162 method: 'POST',
163 credentials: 'include',
164 headers: { ...getClientHeaders(), 'Content-Type': 'application/json' },
165 body: JSON.stringify(body),
166 });
167 } catch (error) {
168 rejectBatch(items, 0, (error as Error)?.message ?? 'Network error');
169
170 return;
171 }
172
173 if (!response.ok) {
174 rejectBatch(items, response.status, 'GraphQL batch request failed');
175
176 return;
177 }
178
179 let payload: unknown;
180
181 try {
182 payload = await response.json();
183 } catch {
184 payload = undefined;

Callers

nothing calls this directly

Calls 6

requestSingleFunction · 0.85
getOperationNameFunction · 0.85
getClientHeadersFunction · 0.85
rejectBatchFunction · 0.85
fallbackToSingleRequestsFunction · 0.85
toClientErrorFunction · 0.85

Tested by

no test coverage detected