質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
GraphQL

GraphQL は、アプリケーション・プログラミング・インタフェース (API) 向けのクエリ言語およびサーバーサイドランタイムです。APIの速度、柔軟性、開発者にとっての使いやすさを向上させるために設計され、データを複数のデータソースから取得するリクエストを1つのAPI呼び出しで構成できます。

Q&A

0回答

1098閲覧

[GraphQL] 値で指定できない エラーになる

DaisukeMori

総合スコア217

GraphQL

GraphQL は、アプリケーション・プログラミング・インタフェース (API) 向けのクエリ言語およびサーバーサイドランタイムです。APIの速度、柔軟性、開発者にとっての使いやすさを向上させるために設計され、データを複数のデータソースから取得するリクエストを1つのAPI呼び出しで構成できます。

0グッド

0クリップ

投稿2021/08/27 01:11

GraphQLを最近学び始めて、GraphQLサーバーを立てることは、公式通りやったら難なくできたのですが、そこから例えば、値に応じてクエリ発行することができないかと思い、以下Qiita記事を見ながらやってみるのですが、同じようにやってもエラーになりうまくいきません。

試していること

GraphQLのクエリを基礎から整理してみた
この記事の「一致取得」 の部分

GraphQLサーバーのコードはこちら
※Apollo serverを利用
※Apollo serverのコードを、ほぼそのまま利用

js

1const { ApolloServer, gql } = require("apollo-server"); 2 3// スキーマを定義する 4const typeDefs = gql` 5 type Book { 6 id: String 7 title: String 8 lead: String 9 url: String 10 author: String 11 } 12 13 type Query { 14 books: [Book] 15 } 16`; 17 18// クエリで取得するデータを定数で置いておく 19const books = [ 20 { 21 id: "2", 22 title: "Harry Potter and the Chamber of Secrets", 23 lead: "This is comments", 24 url: "http://www.example.com", 25 author: "J.K. Rowling" 26 }, 27 { 28 id: "1", 29 title: "Jurassic Park", 30 lead: "This is comments", 31 url: "http://www.example.com", 32 author: "Michael Crishton" 33 } 34]; 35 36// booksクエリ発行時の処理を指定する 37const resolvers = { 38 Query: { 39 books: () => books 40 } 41}; 42 43// サーバーを起動する 44const server = new ApolloServer({ typeDefs, resolvers }); 45 46server.listen().then(({ url }) => { 47 console.log(`???? Server ready at ${url}`); 48});

http://localhost:4000/
こちらでplaygroundが立ち上がります。

そのままクエリ発行

query ExampleQuery { books { id title lead url } }

結果は以下

{ "data": { "books": [ { "id": "2", "title": "Harry Potter and the Chamber of Secrets", "lead": "This is comments", "url": "http://www.example.com" }, { "id": "1", "title": "Jurassic Park", "lead": "This is comments", "url": "http://www.example.com" } ] } }

この後、記事の通り以下のようにクエリ発行

query ExampleQuery { books(id: "1") { id title lead url } }

これでid: 1のアイテムが取得できると思いきや、(id: "1")を追加した時点で
Unknown argument "id" on field "Query.books".というエラーが出ます。

当然これでクエリ発行しても、エラーになります。

{ "errors": [ { "message": "Unknown argument \"id\" on field \"Query.books\".", "locations": [ { "line": 2, "column": 9 } ], "extensions": { "code": "GRAPHQL_VALIDATION_FAILED", "exception": { "stacktrace": [ "GraphQLError: Unknown argument \"id\" on field \"Query.books\".", " at Object.Argument (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/graphql/validation/rules/KnownArgumentNamesRule.js:46:29)", " at Object.enter (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/graphql/language/visitor.js:323:29)", " at Object.enter (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/graphql/utilities/TypeInfo.js:370:25)", " at visit (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/graphql/language/visitor.js:243:26)", " at Object.validate (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/graphql/validation/validate.js:69:24)", " at validate (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/apollo-server-core/dist/requestPipeline.js:186:26)", " at Object.processGraphQLRequest (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/apollo-server-core/dist/requestPipeline.js:90:34)", " at runMicrotasks (<anonymous>)", " at processTicksAndRejections (node:internal/process/task_queues:96:5)", " at async processHTTPRequest (/Users/p000078/Desktop/apollo-graphQL_react/graphql-server-example/node_modules/apollo-server-core/dist/runHttpQuery.js:179:30)" ] } } } ], "data": null }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問