gQuery/dist/gFetch.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-14 16:20:26 +00:00
import { print } from "graphql";
export const stringifyDocument = (node) => {
let str = (typeof node !== "string" ? print(node) : node)
.replace(/([\s,]|#[^\n\r]+)+/g, " ")
.trim();
return str;
};
2021-10-06 21:41:44 +00:00
export class GFetch extends Object {
constructor(options) {
super();
const { path } = options;
this.path = path;
this.fetch = this.fetch.bind(this);
}
// * gFetch
// This is a fetcher that returns a promise that resolves to a graphql response
2021-12-14 16:20:26 +00:00
async fetch({ queries, fetch, }) {
// let document: DocumentNode = addTypenameToDocument(queries[0].query);
let documentString = stringifyDocument(queries[0].query);
2021-10-06 21:41:44 +00:00
const newQueries = {
...queries[0],
query: documentString,
2021-10-06 21:41:44 +00:00
};
// This is generic fetch, that is polyfilled via svelte kit
// graph ql fetches must be POST
// credentials include for user ssr data
const res = await fetch(this.path, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newQueries),
});
// Gets the data back from the server
const data = await res.json();
return {
...data.data,
2021-11-09 16:46:12 +00:00
errors: data.errors,
2021-10-06 21:41:44 +00:00
};
}
}
//# sourceMappingURL=gFetch.js.map