Do check out this service, to get started to build your own REST API & use it in your project.
Leetcode Public REST API
Below code might give you a deep understanding of how to extract the data.
Assume this is the GraphQL Query.
export const recentSubmissionsQuery = `
query recentAcSubmissions($username: String!, $limit: Int!) {
recentAcSubmissionList(username: $username, limit: $limit) {
id
title
titleSlug
timestamp
}
}
`;
You can create an endpoint in NodeJS/ExpressJS like this
router.get("/:username/submissions", async (req, res) => {
const query = recentSubmissionsQuery;
const { username } = req.params;
let { limit } = req.query;
if (!limit) limit = 3;
const data = await fetchGraphQLData(query, { username, limit });
res.json(data);
});
And the fetchGraphQLData will look something similar to like this:
import fetch from "node-fetch";
const URL = BASE_URL;
const fetchGraphQLData = async (query, variables) => {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
};
try {
const response = await fetch(URL, options);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
};
export default fetchGraphQLData;
- use
node-fetch
or axios
libraries to do HTTP requests else, you will be facing issues in your deployment.