什么是GraphQL? 普通神話被揭穿。

I love talking about GraphQL, especially with people who have been working with GraphQL or thinking of adopting GraphQL. One common question people have is why someone would want to move to GraphQL from REST.

我喜歡談論GraphQL,特別是和那些一直在使用GraphQL或正在考慮采用GraphQL的人談論。 人們經常遇到的一個問題是,為什么有人想要從REST遷移到GraphQL。

There are a ton of resources out there that talk about the difference between REST and GraphQL and those are great to check out if you are interested in how those two are different. In this blog post, I want to address some common misconceptions and questions asked about GraphQL.

那里有大量的資源談論REST和GraphQL之間的區別,如果您對這兩者之間的差異感興趣,那么非常有用。 在此博客文章中,我想解決一些常見的誤解和有關GraphQL的問題。

您如何從前端的GraphQL中受益? (How do you benefit from GraphQL on the front end?)

As a Front End Engineer, I like working with a GraphQL API for the following reasons:

作為前端工程師,由于以下原因,我喜歡使用GraphQL API:

  1. You can instantly test queries and mutations using GraphiQL or playground

    您可以使用GraphiQL或Playground立即測試查詢和變異
  2. Less data means lighter state management

    數據更少意味著狀態管理更輕松
  3. It offloads heavy lifting to the server through resolvers

    它通過解析器將繁重的工作轉移到服務器上
  4. It has documentation that is up-to-date and interactive

    它具有最新的文檔和交互式文檔

它比REST好嗎? (How is it better than REST?)

  1. There is one endpoint to fetch all resources.

    有一個端點可獲取所有資源。
  2. You avoid over fetching of data (getting too many fields when only a few fields are needed).

    您可以避免過度獲取數據(僅需要幾個字段時獲取太多字段)。
  3. You avoid under fetching of data (having to call multiple APIs because one API doesn't give back all the information needed).

    您可以避免獲取數據不足(必須調用多個API,因為一個API不會返回所有需要的信息)。

誤解:GraphQL用于查詢圖形數據庫。 (Myth: GraphQL is used to query graph databases.)

GraphQL can be used to query a graph database, but this is not its only use case. The "graph" in GraphQL is used to represent the graph-like structure of data. You model the data in terms of nodes and how they connect to each other. Schema is used to represent this modeling.

GraphQL可用于查詢圖形數據庫,但這不是唯一的用例。 GraphQL中的“圖形”用于表示數據的圖形結構。 您可以根據節點及其相互連接的方式對數據進行建模。 模式用于表示此建模。

There is no limitation in the GraphQL spec that enforces that the data source should be a graph.

GraphQL規范中沒有限制,即數據源應為圖形。

誤解:GraphQL僅適用于基于圖的數據庫或數據源。 (Myth: GraphQL only works with databases or data sources that are graph based.)

It's a misconception that you need to rewrite your database to adopt GraphQL. GraphQL can be a wrapper around any data source, including databases. GraphQL is a query language for your API - which means it is a syntax of how to ask for data.

您需要重寫數據庫以采用GraphQL是一個誤解。 GraphQL可以包裝任何數據源,包括數據庫。 GraphQL是query language for your APIquery language for your API -這意味著它是如何查詢數據的語法。

誤解:使用解析程序,查詢和變異進行數據提取會神奇地工作。 (Myth: Data fetching with resolvers, queries and mutations work magically.)

You will need to define exactly what each of them needs to do. You will be writing functions that get called when queries are fired, and writing functions for resolvers that send back exactly the data you need and know which API to call. You will be defining what data return through those functions by calling resolvers.

您將需要確切定義每個人需要做什么。 您將編寫在激發查詢時被調用的函數,并為解析器編寫函數,這些解析器將準確發送回所需的數據并知道要調用的API。 您將通過調用解析器來定義通過這些函數返回的數據。

誤解:GraphQL取代了Redux或任何狀態管理庫 (Myth: GraphQL replaces Redux or any state management library)

Redux is a state management library. GraphQL is not a state management library. GraphQL helps to get less data, which in turn leads to less data to manage on the client-side, but it is not a state management solution.

Redux是一個狀態管理庫。 GraphQL不是狀態管理庫。 GraphQL有助于獲取更少的數據,從而導致更少的數據需要在客戶端進行管理,但這不是狀態管理解決方案。

You will still need to manage state - albeit in a more lightweight way. Client libraries like Apollo and Relay can be used to manage state and they have caching built-in. GraphQL is not a replacement for Redux - it helps to reduce the need for it.

您仍然需要管理狀態-盡管以更輕量的方式。 客戶端庫(如Apollo和Relay)可用于管理狀態,它們具有內置的緩存。 GraphQL不能代替Redux-它有助于減少對Redux的需求。

誤解:GraphQL是一種數據庫語言。 (Myth: GraphQL is a database language.)

GraphQL is a programming language - specifically a query language. GraphQL's spec defines how GraphQL runtimes should implement the language and how data should be communicated between client and server.

GraphQL是一種編程語言,特別是一種查詢語言。 GraphQL的規范定義了GraphQL運行時應如何實現該語言以及應如何在客戶端和服務器之間傳遞數據。

GraphQL is used to ask for data and can be used in multiple places in any layer from front end to back end. There are databases such as DGraph that implement the GraphQL spec, allowing clients to use GraphQL to query the database.

GraphQL用于請求數據,并且可以在從前端到后端的任何層中的多個位置使用。 有些數據庫(例如DGraph)實現了GraphQL規范,從而允許客戶端使用GraphQL查詢數據庫。

誤解:使用GraphQL的實現中不能包含REST端點。 (Myth: You can't have REST endpoints in your implementation with GraphQL.)

You can plug in multiple REST endpoints or even multiple GraphQL endpoints in your application. Although it is not a best practice to have multiple REST endpoints, it is technically possible.

您可以在應用程序中插入多個REST端點,甚至多個GraphQL端點。 盡管擁有多個REST端點不是最佳實踐,但在技術上是可行的。

誤解:GraphQL很難在現有項目中引入。 (Myth: GraphQL is difficult to introduce in an existing project.)

GraphQL can be plugged into an existing project. You can start with one component of business logic, plug in a GraphQL endpoint, and start fetching data through GraphQL. You don't need to scrap an entire project to start using GraphQL.

GraphQL可以插入到現有項目中。 您可以從業務邏輯的一個組件開始,插入GraphQL端點,然后開始通過GraphQL獲取數據。 您無需剪貼整個項目即可開始使用GraphQL。

If switching to GraphQL endpoint is still a daunting task, you can start by masking a REST endpoint into a GraphQL endpoint using resolvers.

如果切換到GraphQL端點仍然是一項艱巨的任務,則可以通過使用解析器將REST端點屏蔽到GraphQL端點開始。

誤解:GraphQL僅適用于前端開發人員 (Myth: GraphQL is only for front-end developers)

GraphQL is platform agnostic. In my opinion, the beauty of GraphQL's benefits starts from the inside out - back end to front end.

GraphQL與平臺無關。 我認為,GraphQL的優勢之美始于從內到外-從后端到前端。

As a back end developer, you are able to expand the API by adding fields without having to publish a new version of the API. You don't need to write different endpoints for different needs since clients can fetch whatever data they need.

作為后端開發人員,您可以通過添加字段來擴展API,而不必發布API的新版本。 您無需為不同的需求編寫不同的端點,因為客戶端可以獲取所需的任何數據。

With GraphQL, you have visibility of what fields clients are using, which provides powerful instrumentation.

使用GraphQL,您可以查看客戶端使用的字段,從而提供了強大的工具。

誤解:GraphQL會自己編寫數據庫查詢,我只需要指定架構及其之間的關系 (Myth: GraphQL will write database queries itself, I just need to specify schemas and the relation between them)

You may need to write the database queries depending on which GraphQL library you are using. However, some libraries like Neo4j and Prisma write database queries too and abstract the logic away from the developer. Everything, including resolvers, queries, and mutations, needs to be defined.

您可能需要編寫數據庫查詢,具體取決于所使用的GraphQL庫。 但是,某些庫(例如Neo4j和Prisma)也編寫數據庫查詢,并將邏輯抽象給開發人員。 包括解析程序,查詢和變異在內的所有內容都需要定義。

誤解:GraphQL用于繪制圖形。 (Myth: GraphQL is used to draw graphs.)

Often people new to GraphQL think that it is a graph plotting software such as D3. GraphQL doesn't plot graphs.

經常接觸GraphQL的人會認為這是一個諸如D3之類的圖形繪圖軟件。 GraphQL不會繪制圖形。

誤解:GraphQL需要復雜的客戶端,并且幾乎不可能通過簡單的HTTP提取來完成 (Myth: GraphQL requires complicated clients and is near impossible to do with a simple HTTP fetch)

誤解:它取代了ORM。 (Myth: It replaces ORMs.)

Lately we see a lot of DB and GraphQL integration but GraphQL itself is not that.

最近,我們看到了很多數據庫和GraphQL的集成,但是GraphQL本身不是那樣。

I think GraphQL is awesome! There are a multitude of libraries that help a user get started with GraphQL. If you are interested in learning about GraphQL, start with the documentation or check out this Udemy course that I found helpful when I was new to GraphQL.

我認為GraphQL很棒! 有許多庫可以幫助用戶開始使用GraphQL。 如果您有興趣學習GraphQL, 請先閱讀 文檔,或者查看本本Udemy課程 。

翻譯自: https://www.freecodecamp.org/news/what-is-graphql-the-misconceptions/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390933.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390933.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390933.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在Spring Boot里面,怎么獲取定義在application.properties文件里的值

問題:在Spring Boot里面,怎么獲取定義在application.properties文件里的值、 我想訪問application.properties里面提供的值,像這樣: logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging…

連接sqlexpress

sqlexpress在visualstudio安裝時可選擇安裝。   數據源添加 localhost\sqlexpress window身份認證即可。轉載于:https://www.cnblogs.com/zjxbetter/p/7767241.html

在Python中使用Seaborn和WordCloud可視化YouTube視頻

I am an avid Youtube user and love watching videos on it in my free time. I decided to do some exploratory data analysis on the youtube videos streamed in the US. I found the dataset on the Kaggle on this link我是YouTube的狂熱用戶,喜歡在業余時間…

Win下更新pip出現OSError:[WinError17]與PerrmissionError:[WinError5]及解決

環境:Win7 64位,python3.6.0 我在準備用pip裝東西的時候,在cmd里先更新了一下pip,大概是9.0.1更新到9.0. 嘗試更新pip命令: pip install --upgrade pip 更新一半掛了 出現了 OSError:[WinError17] 與 PerrmissionError…

老生常談:抽象工廠模式

在創建型模式中有一個模式是不得不學的,那就是抽象工廠模式(Abstract Factory),這是創建型模式中最為復雜,功能最強大的模式.它常與工廠方法組合來實現。平時我們在寫一個組件的時候一般只針對一種語言,或者說是針對一個區域的人來實現。 例如:現有有一個新聞組件,在中國我們有…

ogc是一個非營利性組織_非營利組織的軟件資源

ogc是一個非營利性組織Please note that freeCodeCamp is not partnered with, nor do we receive a referral fee from, any of the following providers. We simply want to help guide you toward a solution for your organization.請注意,freeCodeCamp不與以下…

數據結構入門最佳書籍_最佳數據科學書籍

數據結構入門最佳書籍Introduction介紹 I get asked a lot what resources I recommend for people who want to start their Data Science journey. This section enlists books I recommend you should read at least once in your life as a Data Scientist.我被很多人問到…

函數式編程概念

什么是函數式編程 簡單地說,函數式編程通過使用函數,將值轉換成抽象單元,接著用于構建軟件系統。 面向對象VS函數式編程 面向對象編程 面向對象編程認為一切事物皆對象,將現實世界的事物抽象成對象,現實世界中的關系抽…

在Java里面怎么樣在靜態方法中調用getClass()?

問題:在Java里面怎么樣在靜態方法中調用getClass()? 我有一個類,它必須包含一些靜態方法,在這些靜態方法里面我需要像下面那樣調用getClass() 方法 public static void startMusic() {URL songPath getClass().getClassLoader(…

變量名和變量地址

變量名和變量地址 研一時,很偶然的翻開譚浩強老先生的《C程序設計》(是師姐的書,俺的老早就賣了,估計當時覺得這本書寫得不夠好),很偶然的看到關于變量名的一段話:“變量名實際上是一個符號地址…

多重插補 均值插補_Feature Engineering Part-1均值/中位數插補。

多重插補 均值插補Understanding the Mean /Median Imputation and Implementation using feature-engine….!了解使用特征引擎的均值/中位數插補和實現…。! 均值或中位數插補: (Mean or Median Imputation:) The mean or median value should be calc…

域 嵌入圖像顯示不出來_如何(以及為什么)將域概念嵌入代碼中

域 嵌入圖像顯示不出來Code should clearly reflect the problem it’s solving, and thus openly expose that problem’s domain. Embedding domain concepts in code requires thought and skill, and doesnt drop out automatically from TDD. However, it is a necessary …

linux 查看用戶上次修改密碼的日期

查看root用戶密碼上次修改的時間 方法一:查看日志文件: # cat /var/log/secure |grep password changed 方法二: # chage -l root-----Last password change : Feb 27, 2018 Password expires : never…

spring里面 @Controller和@RestController注解的區別

問題:spring里面 Controller和RestController注解的區別 spring里面 Controller和RestController注解的區別 Web MVC和REST applications都可以用Controller嗎? 如果是的話,怎么樣區別這個一個 Web MVC還是REST application呢 回答一 下面…

2流程控制

分支、循環 str1$1 str2$2 echo $# if [ $str1 $str2 ] thenecho "ab" elif [ "$str1" -lt "$str2" ] thenecho "a < b" elif [ "$str1" -gt "$str2" ] thenecho "a > b" elseecho "沒有符…

客戶行為模型 r語言建模_客戶行為建模:匯總統計的問題

客戶行為模型 r語言建模As a Data Scientist, I spend quite a bit of time thinking about Customer Lifetime Value (CLV) and how to model it. A strong CLV model is really a strong customer behavior model — the better you can predict next actions, the better yo…

linux bash命令_Ultimate Linux命令行指南-Full Bash教程

linux bash命令Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.歡迎使用我們的Linux命令行最終指南。 本教程將向您展示一些關鍵…

【知識科普】解讀閃電/雷電網絡,零基礎秒懂!

知識科普&#xff0c;解讀閃電/雷電網絡&#xff0c;零基礎秒懂&#xff01; 閃電網絡的技術是革命性的&#xff0c;將實現即時0手續費的小金額支付。第一步是解決擴容問題&#xff0c;第二部就是解決共通性問題&#xff0c;利用原子交換協議和不同鏈條的狀態通道結合&#xff…

spring框架里面applicationContext.xml 和spring-servlet.xml 的區別

問題&#xff1a;spring框架里面applicationContext.xml 和spring-servlet.xml 的區別 在Spring框架中applicationContext.xml和Spring -servlet.xml有任何關系嗎? DispatcherServlet可以使用到在applicationContext.xml中聲明的屬性文件嗎? 另外&#xff0c;為什么我需要*…

Alpha 沖刺 (5/10)

【Alpha go】Day 5&#xff01; Part 0 簡要目錄 Part 1 項目燃盡圖Part 2 項目進展Part 3 站立式會議照片Part 4 Scrum 摘要Part 5 今日貢獻Part 1 項目燃盡圖 Part 2 項目進展 已分配任務進度博客檢索功能&#xff1a;根據標簽檢索流程圖 -> 實現 -> 測試近期比…