diff --git a/scripts/utils/graphqlRequest.js b/scripts/utils/graphqlRequest.js new file mode 100644 index 00000000000..c8f324cc263 --- /dev/null +++ b/scripts/utils/graphqlRequest.js @@ -0,0 +1,34 @@ +const fetch = require('cross-fetch'); + +const graphqlRequest = async (queryOrMutation, variables, url, authKey) => { + const options = { + method: 'POST', + headers: { + 'x-api-key': authKey, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query: queryOrMutation, + variables, + }), + }; + + let body; + let response; + + try { + response = await fetch(url, options); + body = await response.json(); + return body; + } catch (error) { + /* + * Something went wrong... obviously + */ + console.error(error); + return null; + } +}; + +module.exports = { + graphqlRequest, +};