Skip to content

Commit bd8c4d8

Browse files
committed
create: reausable github's graphql api caller method
1 parent a0168b4 commit bd8c4d8

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/api/api.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const GITHUB_GRAPHQL_URL = "https://api.github.com/graphql";
2+
3+
export async function githubGraphQL<T>(
4+
query: string,
5+
variables: Record<string, any> = {},
6+
token?: string
7+
): Promise<T> {
8+
const headers = token
9+
? {
10+
"Content-Type": "application/json",
11+
Authorization: `bearer ${token}`
12+
}
13+
: {
14+
"Content-Type": "application/json"
15+
};
16+
17+
const res = await fetch(GITHUB_GRAPHQL_URL, {
18+
method: "POST",
19+
headers: headers as HeadersInit,
20+
body: JSON.stringify({ query, variables })
21+
});
22+
23+
const json = await res.json();
24+
console.log(json)
25+
if (json.errors){
26+
const error = JSON.stringify(json.errors, null, 2);
27+
console.log(json.errors)
28+
}
29+
return json.data;
30+
}

0 commit comments

Comments
 (0)