GraphQL案例演示
TakeShape的聯合創始人Andrew Sprouse在紐約的JAMstack聚會介紹了GraphQL。
什麼是GraphQL?
模式定義+查詢語言+解析框架
架構
- 提供資料的強型別描述
- 架構描述語言(SDL)是指定架構的推薦跨平臺方式。
enum Title { ACTOR DIRECTOR } type Role { characterName: String! title: Title movie: Movie } type Person { name: String! photo: String filmography: [Role] } type Movie { title: String! watched: Boolean rating: Float poster: String actors: [Person]! director: Person! year: String } |
GraphQL Schema還指定了如何使用查詢和突變與資料進行互動:
type Query { listMovies: [Movie] } type Mutation { addMove(title: String): [Movie] removeMovie(title: String): [Movie] } |
查詢Langauge
查詢您的資料並準確獲取您需要的內容:
query { getToWatchList { watched movie { title year director } } } |
{ "data": { "getToWatchList": [ { "watched": true, "movie": { "title": "Top Gun", "year": "1985", "director": "Tony Scott" } }, { "watched": true, "movie": { "title": "Big Trouble in Little China", "year": "1986", "director": "John Carpenter" } }, { "watched": true, "movie": { "title": "The Princess Bride", "year": "1987", "director": "Rob Reiner" } }, { "watched": false, "movie": { "title": "Taxi Driver", "year": "1976", "director": "Martin Scorsese" } } ] } } |
修改資料
mutation { addMovieToWatch(title: "Die Hard") { watched movieTitle movie { title year director } } } { "data": { "addMovieToWatch": [ { "watched": false, "movie": { "title": "Taxi Driver", "year": "1976", "director": "Martin Scorsese" } }, { "watched": false, "movie": { "title": "Die Hard", "year": "1988", "director": "John McTiernan" } } ] } } |
實現框架
- 每個GraphQL實現都提供自己的查詢解析框架
- GraphQL.js是參考實現
- 執行查詢和變異解析
架構(SDL)
type Query { getToWatchList: [ToWatch] } type Mutation { addMovieToWatch(title: String!): [ToWatch] removeMovieToWatch(title: String!): [ToWatch] markMovieWatched(title: String! watched: Boolean!): [ToWatch] } |
解析器
const resolvers = { Query: { getToWatchList: () => { return WatchList.list(); } }, Mutation: { addMovieToWatch(_, {title}) { return WatchList.add(title); }, removeMovieToWatch(_, {title}) { return WatchList.remove(title); }, markMovieWatched(_, {title, watched}) { return WatchList.markWatched(title, watched); } } }; |
解析器還能夠解析動態計算欄位
架構(SDL)
type Move { title: String rating: Float poster: String actors: String director: String year: String } type ToWatch { movieTitle: String! movie: Movie watched: Boolean! } |
解析器
const resolvers = { ToWatch: { async movie(toWatch) { const info = await fetchMovieInfo(toWatch.movieTitle); return info ? { title: info.Title, rating: info.imdbRating, poster: info.Poster, year: info.Year, actors: info.Actors, director: info.Director } : null } } }; |
相關文章
- GraphQL:PayPal結賬的成功案例
- 2.2反射性xss案例演示反射
- InnoDB鎖衝突案例演示
- .NET使用Graphql的演示——新一代的API互動API
- NCF 的Azure Cosmos DB 演示案例
- Spring Boot + GraphQL建立API的開源案例Spring BootAPI
- ECMASCRIPT 2021新功能程式碼演示案例
- SpringMVC入門案例 & 常用API使用演示SpringMVCAPI
- Spring Boot和Netflix DGS的GraphQL原始碼案例Spring Boot原始碼
- NFT交易系統開發(APP案例演示)APP
- 交易所APP軟體開發(演示案例)APP
- 遺留程式碼處理技巧與案例演示
- Golang 併發,有快取通道,通道同步案例演示Golang快取
- SpringBoot2 基礎案例(12):基於轉賬案例,演示事務管理操作Spring Boot
- GraphQL 快速入門【5】GraphQL 示例
- Graphql學習(一)-GraphQL介紹
- MySQL記錄鎖、間隙鎖、臨鍵鎖小案例演示MySql
- 3568F-翼輝SylixOS國產作業系統演示案例作業系統
- 華登區塊狗現成系統軟體原始碼案例演示原始碼
- GraphQL學習:Github GraphQL API v4初探GithubAPI
- Graphql 調研
- Flutter x GraphQLFlutter
- GraphQL入門
- GraphQL & Relay 初探
- 現成雜湊遊戲系統原始碼搭建丨演示版案例遊戲原始碼
- defi質押挖礦dapp系統開發原理技術分析(案例演示)APP
- danthelion/trino-minio-iceberg-example:使用Minio、Trino、iceberg搭建資料工程演示案例
- GraphQL初體驗,Node.js構建GraphQL API指南Node.jsAPI
- 《GraphQL 名詞 101:解析 GraphQL 的查詢語法》【譯】
- You-Get開源線上下載神器,搭配python更加絲滑(文中案例演示)Python
- rails graphql的使用AI
- GraphQL & Relay 實戰
- Vue + GraphQL初試Vue
- GraphQL入門:GraphQL與REST區別的不同舉例 - SithiraREST
- graphql-normalizr: 儲存規範化GraphQL響應資料ORM
- api一鍵合約跟單帶單軟體開發案例演示(原始碼交付)API原始碼
- GraphQL 漸進學習 05-graphql-resolvers-union-聯合的使用
- GraphQL 入門簡介