iT邦幫忙

0

請問 graphql 如何篩選搜尋的字串

  • 分享至 

  • xImage

目前正在練習graphql 使用apolloClient的方式
期望結果是希望透過query查詢 篩選出搜尋框內容(mission_name)的資料
query不知該如何撰寫才好
網路上說可以使用filter 但我使用卻會錯誤
請各位大大指點
https://ithelp.ithome.com.tw/upload/images/20220720/20123840vPh5mBmHu3.png

對了 搜尋框還沒寫 想先試看看送出query就卡住了
以下是codesandbox範例 還沒出錯版

https://codesandbox.io/s/spacex-graph-api-c661bj?file=/src/graphql/queries.js

zyxa9527 iT邦新手 5 級 ‧ 2022-07-20 15:22:20 檢舉
後來發現 find: { mission_name: $search } 可以成功篩選
不過不知道是不是正規方式XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
通靈亡
iT邦高手 1 級 ‧ 2022-07-20 17:37:49
最佳解答

無法用 filter 的原因很簡單,因為 launchesPast 定義的 Query 欄位不包含 filter 欄位

https://ithelp.ithome.com.tw/upload/images/20220720/20120331Stc936cto7.png

https://ithelp.ithome.com.tw/upload/images/20220720/20120331ZLIa79xQPg.png

所以你要用 launchesPast 定義的 find Query 欄位來查詢
詳細可以參考這支的 API 文件:
https://api.spacex.land/graphql/

zyxa9527 iT邦新手 5 級 ‧ 2022-07-20 17:51:48 檢舉

感謝解答
一開始我看網上用filter的很多 以為大家都這樣用
但到後面確實有在猜是不是只能用API提供的欄位find來使用

我要發表回答

立即登入回答