什么是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.mzph.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;根据标签检索流程图 -> 实现 -> 测试近期比…