Go言語のAmplify functionでGraphQL APIを叩くコード(IAM)
2023/9/23 20:36:00
var ENDPOINT = os.Getenv("API_AMPLIFY_GRAPHQLAPIENDPOINTOUTPUT")
func query(query_string string) string {
json_string := `{"query":"` + strings.Replace(query_string, "\n", "\\n", 100) + `"}`
req, err := http.NewRequest(
"POST",
ENDPOINT,
bytes.NewBuffer([]byte(json_string)),
)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
//Authの設定によっては署名が必要
config := aws.Config{Region: aws.String("ap-northeast-1")}
sess := session.Must(session.NewSession(&config))
signer := v4.NewSigner(sess.Config.Credentials)
signer.Sign(req, bytes.NewReader([]byte(json_string)), "appsync", "ap-northeast-1", time.Now())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return string(body)
}