Last active
September 27, 2018 02:34
-
-
Save bdombro/1fbcadd5ab5fec1144c3ceb56eac0b9d to your computer and use it in GitHub Desktop.
fetch-graphql.jsx - Run a simple GraphQL Query using isomorphic-fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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