Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Created March 2, 2018 19:59
Show Gist options
  • Select an option

  • Save VitorLuizC/b98a14fc8b22c1169d4834b5d64d4057 to your computer and use it in GitHub Desktop.

Select an option

Save VitorLuizC/b98a14fc8b22c1169d4834b5d64d4057 to your computer and use it in GitHub Desktop.
Módulo que executa as requisições de queries GraphQL com suas variáveis.
/**
* Obtém os cabeçalhos da requisição.
* @returns {Object.<string, string>}
*/
export const getHeaders = () => {
const token = localStorage.getItem('access_token');
const headers = {
'Content-Type': 'application/json',
...token && { 'Authorization': `Bearer ${token}` }
};
return headers;
};
/**
* Faz a requisição de uma query e suas variáveis.
* @param {string} query
* @param {Object.<string, *>} [variables]
* @returns {Promise.<*>}
*/
export default async (query, variables = {}) => {
const url = 'http://localhost:3001/v1/graphql';
const config = {
method: 'POST',
body: JSON.stringify({ query, variables }),
headers: getHeaders()
};
const response = await fetch(url, config);
const data = await response.json();
return data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment