Skip to main content

Integrate GraphQL with axios

Why using GraphQL with axios

Using axios to a GraphQL is the fastest way to integrate a GraphQL API inside your Spell.

However, you'll lose some Meta API features, so we recommend you to integrate your GraphQL API using a connector.

Integrate a GraphQL API with axios

The axios library is already added to your Spell, so you can call it directly like this:

const myRequest = await axios({
method: "POST",
url: "https://myserver.com/graphql",
headers: {
apikey: "test123",
},
data: {
query,
operationName: "PostPageCommentVotersComponent",
variables: { id: post_id },
},
});

You'll have to manage manually your authentication using headers or query parameters (refer to your vendor's API).

To make you code more readable, you can define your query or mutation request outside the axios configuration, like this:

const query = `
query PostPageCommentVotersComponent($id:ID) {
post(id:$id){
id
name
url
comments {
totalCount
edges {
cursor
node {
body
parentId
url
user {
name
username
url
profileImage
followers {
totalCount
}
}
replies {
totalCount
}
}
}
}
votes {
totalCount
}
}
}`;

const myRequest = await axios({
method: "POST",
url: "https://myserver.com/graphql",
headers: {
apikey: "test123",
},
data: {
query,
operationName: "PostPageCommentVotersComponent",
variables: { id: post_id },
},,
});

console.log(myRequest.data);