私はこのGraphqlイントロhttps://www.apollographql.com/docs/apollo-server/getting-started/をフォローしています。ファイルを設定し(少し変更しました)、基本クエリはhttp:// localhost:4000 /で機能しています。
基本を理解した後の次の質問は、パラメータに基づいてデータを取得するにはどうすればよいですか?ここまでは取得しましたが、プレイグラウンドでのクエリは結果を返しません。
index.js
const typeDefs = gql`
type Item {
name: String
description: String
full_url: String
term: String
}
type Query {
items: [Item]
itemsSearch(term: String!): [Item]
}
`;
const resolvers = {
Query: {
// this works. it is the example from the guide.
items: () => items,
// this doesn't work. `term` is always undefined
itemsSearch: term => {
console.log('term', term);
console.log('items', items);
return items.filter(item => item.title.indexOf(term) > -1 || item.author.indexOf(term) > -1);
},
},
};
次に、このクエリを遊び場で実行しています。(主にhttps://graphql.org/graphql-js/passing-arguments/から作業しています)
{
itemsSearch(term: "Rowling") {
title
author
}
}
応答は成功しましたが、データがありません。前述のようterm
に、itemsSearchリゾルバーにログインすると未定義が出力されます。
そのパラメーターterm
をリゾルバーに渡して結果を取得する方法はありますか?前もって感謝します。
リゾルバの引数がありparent
、args
、context
とinfo
:
args
このフィールドに提供されたすべてのGraphQL引数を含むオブジェクト。
例えば、実行時
query{ user(id: "4") }
、args
オブジェクトは、リゾルバユーザに渡されます{ "id": "4" }
。
したがって、次のことterm
を実行args
します。
itemsSearch: (parent, { term }) => {
...
}
または:
itemsSearch: (parent, args) => {
const term = args.term;
...
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加