Skip to content

Instantly share code, notes, and snippets.

@Leocardoso94
Last active May 29, 2018 22:06
Show Gist options
  • Select an option

  • Save Leocardoso94/5419b1e03a0fcae0f586dbe354e25cfb to your computer and use it in GitHub Desktop.

Select an option

Save Leocardoso94/5419b1e03a0fcae0f586dbe354e25cfb to your computer and use it in GitHub Desktop.
Consumindo uma api em graphql usando javascript puro
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