Skip to content

Commit 87d1175

Browse files
committed
start vue thing
1 parent 2f6f3f4 commit 87d1175

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/vue/fetch.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { QueryFetcher } from "gqless";
2+
3+
/*
4+
Listener code:
5+
const listener = m => {
6+
if (m.data === "DATA_FETCHED") {
7+
this.$forceUpdate();
8+
window.removeEventListener("message", listener);
9+
}
10+
};
11+
window.addEventListener("message", listener);
12+
*/
13+
14+
const vueFetch: QueryFetcher = async (endpoint: string) => {
15+
return async function (query: any, variables: any) {
16+
const response = await fetch(endpoint, {
17+
method: 'POST',
18+
headers: { 'Content-Type': 'application/json' },
19+
body: JSON.stringify({ query, variables }),
20+
mode: 'cors'
21+
});
22+
if (!response.ok) throw new Error(`Network error, received status code ${response.status}`);
23+
const json = await response.json();
24+
window.postMessage("DATA_FETCHED", window.location.href);
25+
return json;
26+
}
27+
}

0 commit comments

Comments
 (0)