Skip to content

Instantly share code, notes, and snippets.

@bdombro
Last active September 27, 2018 02:34
Show Gist options
  • Save bdombro/1fbcadd5ab5fec1144c3ceb56eac0b9d to your computer and use it in GitHub Desktop.
Save bdombro/1fbcadd5ab5fec1144c3ceb56eac0b9d to your computer and use it in GitHub Desktop.
fetch-graphql.jsx - Run a simple GraphQL Query using isomorphic-fetch
/**
* Run a GraphQL Query using isomorphic-fetch
*/
const HTTP_BAD_REQUEST = 400
const endpoint = "https://api.graph.cool/simple/v1/cj9g48vlm79hy0120m4tvigm9"
let fetchGraphQLCache = [];
export default (query, callback) => {
for (let c of fetchGraphQLCache) {
if (c[0] === query) {
callback(c[1])
returnmv
}
}
let queryFormatted = query.replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/"/g, '\\"').replace(/ /g, '')
fetch(endpoint, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
// "x-csrf-jwt": token
},
body: `{"query":"{` + queryFormatted + `}"}`
})
.then((response) => {
if (response.status >= HTTP_BAD_REQUEST) {
throw new Error("Bad response from server");
}
response.json().then((res) => {
fetchGraphQLCache.push([query,res.data])
callback(res.data);
});
})
.catch((err) => {
throw new Error("Error Fetching Records", err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment