Call GraphQL API with Axios

Joseph Perez
1 min readJul 25, 2021

Axios is an HTTP client library for JavaScript. If you’re building an app with React, Vue, or Angular, it’s useful to use a GraphQL client library like Apollo Client, but if you just need to make one GraphQL call, for example from a Node.js script, you can use any library you want. We’ve had a few questions about how to do it with Axios, so I thought I’d throw together a super quick example.

In this example we’re going to call the authors and posts server hosted on Launchpad. Go there to see the server code. The GraphQL API URL for that server is:

https://1jzxrj179.lp.gql.zone/graphql

And the query we want to send is:

query PostsForAuthor {
author(id: 1) {
firstName
posts {
title
votes
}
}
}

Now we can put those together into a simple POST request with Axios like so:

const axios = require("axios")axios({
url: 'https://1jzxrj179.lp.gql.zone/graphql',
method: 'post',
data: {
query: `
query PostsForAuthor {
author(id: 1) {
firstName
posts {
title
votes
}
}
}
`
}
}).then((result) => {
console.log(result.data)
});

Wow, that was simple! You can try that yourself by running the code in your browser using RunKit.

--

--

Joseph Perez

I am a software engineer working in EdTech. I have a passion for supporting those working hard to get into the tech industry.