こちらのプログラムを Deno で書き換えました。
Curl: GitLab で GraphQL を使う
プログラム
get_data.sh
export ACCESS_TOKEN="glpat-******"
deno run --allow-all get_data.ts query.json
get_data.ts
// ---------------------------------------------------------------
// get_data.ts
//
// Apr/15/2023
//
// ---------------------------------------------------------------
import process from "node:process"
const file_in:string =Deno.args[0]
console.log (file_in)
const url = "https://gitlab.com/api/graphql"
const query:string = Deno.readTextFileSync (file_in,{ encoding: "utf8" })
console.log(query)
const headers = new Headers({
"Authorization": `bearer ${process.env.ACCESS_TOKEN}`,
"Content-Type": "application/json",
})
const response = await fetch(url, {
method: "POST",
headers: headers,
body: query,
})
const data = await response.json()
console.log(data)
// ---------------------------------------------------------------
query.json
{"query": "query {currentUser {name location username webUrl}}"}
実行結果
$ ./get_data.sh
query.json
{"query": "query {currentUser {name location username webUrl}}"}
{
data: {
currentUser: {
name: "Uchida Masatomo",
location: "栃木県",
username: "ekzemplaro",
webUrl: "https://gitlab.com/ekzemplaro"
}
}
}
確認したバージョン
$ deno --version
deno 1.32.4 (release, x86_64-unknown-linux-gnu)
v8 11.2.214.9
typescript 5.0.3