Created
March 2, 2018 19:59
-
-
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.
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
| /** | |
| * 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