Skip to content

Instantly share code, notes, and snippets.

@lindenmelvin
Created February 12, 2020 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lindenmelvin/6f201b569d17859b0e37b40e3c7cc5c0 to your computer and use it in GitHub Desktop.
Save lindenmelvin/6f201b569d17859b0e37b40e3c7cc5c0 to your computer and use it in GitHub Desktop.
const axios = require("axios");
axios.defaults.baseURL = "http://localhost:4000/";
const login = async (email, password) => {
const axiosResponse = await axios.post('/', {
query: `mutation Login {
login(email: "${email}", password: "${password}") {
token
}
}`
});
const graphqlQueryData = axiosResponse.data;
const authenticationToken = graphqlQueryData.data.login.token;
axios.defaults.headers.common['Authorization'] = `Bearer ${authenticationToken}`;
}
const posts = async () => {
const axiosResponse = await axios.post('/', {
query: `query FetchPosts {
posts {
title
}
}`
});
const graphqlQueryData = axiosResponse.data;
const posts = graphqlQueryData.data.posts;
return posts;
}
const main = async () => {
await login("foo@example.com", "bar");
await posts();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment