Google Apps ScriptでAniList GraphQL APIv2を叩くと、'No query or mutation provided...'のエラー400が返ってくる
689 語
3 分
Google Apps ScriptでAniList GraphQL APIv2を叩くと、'No query or mutation provided...'のエラー400が返ってくる
はじまり

リサちゃん
あーーー沼だ・・・

135ml
おっなんだ詰まってんのか

リサちゃん
GraphQLで上手く行かん・・・

135ml
GASでfetchしてるのか、普通のJavaScriptとちょっと違うからなあ

リサちゃん
何がダメなのかさっさと教えてくれ!
まず、何が起きてるんだ
今回は、AniList GraphQL APIv2を、Google Apps ScriptのUrlFetchApp.fetch()メソッドでリクエストします。
anilist.gitbook.io
anilist.gitbook.io
anilist.gitbook.io
まあ、このGeting Startedと同じ感じで、クエリとスクリプトをこんな風に叩いてみるのですが・・・
// Here we define our query as a multi-line string// Storing it in a separate .graphql/.gql file is also possiblevar query = `query ($id: Int) { # Define which variables will be used in the query (id) Media (id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query) id title { romaji english native } }}`;
// Define our query variables and values that will be used in the query requestvar variables = { id: 15125};
// Define the config we'll need for our Api requestvar url = 'https://graphql.anilist.co', options = { method: 'post', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, body: JSON.stringify({ 'query': query, 'variables': variables }), muteHttpExceptions: true };
// Make the HTTP Api requestconsole.log(options)
let response = UrlFetchApp.fetch(url, options);console.log(JSON.parse(response));エラーコード400で、うまく動きません。
'No query or mutation provided. Please view https://anilist.co/graphiql to see available queries and mutations.'とか書いてあったので、指示されたサイトでクエリを打ったのですが、そのクエリは上手くいきました。
13:25:45 情報 { errors: [ { message: 'No query or mutation provided. Please view https://anilist.co/graphiql to see available queries and mutations.', status: 400, locations: [Object] } ], data: null }一体、何が悪かったのか
「クエリが無い」というエラーになった原因は、payloadではなく、bodyにしていたことです。
// Here we define our query as a multi-line string// Storing it in a separate .graphql/.gql file is also possiblevar query = `query ($id: Int) { # Define which variables will be used in the query (id) Media (id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query) id title { romaji english native } }}`;
// Define our query variables and values that will be used in the query requestvar variables = { id: 15125};
// Define the config we'll need for our Api requestvar url = 'https://graphql.anilist.co', options = { method: 'post', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, // body: JSON.stringify({ payload: JSON.stringify({ // ここ! 'query': query, 'variables': variables }), muteHttpExceptions: true };
// Make the HTTP Api requestconsole.log(options)
let response = UrlFetchApp.fetch(url, options);console.log(JSON.parse(response.getContentText()));console.log(JSON.parse(response.getContentText())["data"]["Media"]["title"]);これで叩くと、ちゃんとデータが取得されて帰ってきます。ころしあえ~!
13:27:51 情報 { method: 'post', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, payload: '{"query":"\\n query ($id: Int) { # Define which variables will be used in the query (id)\\n Media (id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query)\\n id\\n title {\\n romaji\\n english\\n native\\n }\\n }\\n }\\n ","variables":{"id":15125}}', muteHttpExceptions: true }13:27:51 情報 { data: { Media: { id: 15125, title: [Object] } } }13:27:51 情報 { romaji: 'Teekyuu', english: 'Teekyuu', native: 'てーきゅう' }GASのUrlFetchApp.fetch()メソッドでリクエストするときは、bodyじゃなくてpayloadでなくてはならないんですね。 今まであんまり気にしてなかったから盲点だった・・・
意外と検索しても出てこないのが沼ポイントでした。
おしまい

リサちゃん
あ~、取れました~

135ml
こういう沼が一番厄介な気がするわ
以上になります!
記事を共有
この記事が役に立ったなら、ぜひ他の人と共有してください!
Google Apps ScriptでAniList GraphQL APIv2を叩くと、'No query or mutation provided...'のエラー400が返ってくる
https://endorphinbath.com/posts/gas-anilist-graphql-error/ 関連記事 スマート
1
【GraphQL】基礎的な書き方から、AniList API用のMutationクエリ(複数レコード更新)まで。
Code GraphQLの基礎的な書き方から、初心者でも迷わずにクエリを構築できるように、QueryとMutationの書き方を紹介します。そして、実際にAniListというサービスで叩いた結果も紹介します。
2
【Notion API】Google Apps Scriptでリッチテキストの編集を自動化する
Code NotionのAPIを使って、ページ内のブロックのリッチテキストを自動的に編集する方法を紹介します。日々の日記用のページを自動作成したり、自動更新することが出来るようになります。
3
【Notion API】Google Apps Scriptでデータベースからページの中身を取得する
Code NotionのDatabase API、Page API、Block APIでページの内容を取得する方法を紹介します。ベースはJavaScriptなのでNode.js、Deno、Bunでも流用できるかと。
4
【GAS】Google Apps Scriptで作った自作関数に対してテストコードを書けるライブラリ「TestGAS」を作りました!
Code Google Apps Scriptで作った関数に対して、テストコードを使って動作を検証するためのツール「TestGAS」を作成および公開しました。初めて作ったテストツールですが、個人的にかなり便利なものになったので、ぜひ使ってみて下さい。
5
【GAS】Google Apps Scriptで書いたコードをGiitHubで公開するための段取り
Code Google Apps Scriptを、Gitで管理して公開するための方法を紹介します。PublicリポジトリでTokenやIDを公開せずにソースとして管理できる方法になっています。
ランダム記事 ランダム