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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Joseph Perez
Joseph Perez

Written by 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.

No responses yet

Write a response