Last active
May 29, 2018 22:06
-
-
Save Leocardoso94/5419b1e03a0fcae0f586dbe354e25cfb to your computer and use it in GitHub Desktop.
Consumindo uma api em graphql usando javascript puro
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
| const consumirAPI = async (graphqlEndpoint, query, variables = {}) => { | |
| const response = await fetch(graphqlEndpoint, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ query, variables }) | |
| }); | |
| return response.json(); | |
| } | |
| const GRAPHQL_ENDPOINT = 'http://localhost:8080/' | |
| const consultarTodosQuery = ` | |
| query ConsultarTodos{ | |
| todos { | |
| id | |
| title | |
| completed | |
| } | |
| } | |
| `; | |
| consumirAPI(GRAPHQL_ENDPOINT, consultarTodosQuery).then(console.log); | |
| const adicionarTodoMutation = ` | |
| mutation AdicionarTodo($title: String!) { | |
| add(title: $title){ | |
| id | |
| title | |
| completed | |
| } | |
| } | |
| ` | |
| consumirAPI(GRAPHQL_ENDPOINT, adicionarTodoMutation, { title: 'Estudar GraphQL' }).then(console.log); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment