gatsby_gatsby graphql和有关类型定义的缺失但必要的解释

2023-11-10 19:30

本文主要是介绍gatsby_gatsby graphql和有关类型定义的缺失但必要的解释,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

gatsby

The error that appeared on my console was Cannot query field “Title” on type “strapiArticle”. I was working on Strapi at the time and the error seemed too foreign, too weird, too “GraphQLesque” when I first saw it, also it was too specific to my application to look for it on Google.

我的控制台上出现的错误是无法查询“ strapiArticle类型的“ Title”字段 。 当时我正在使用Strapi,当我第一次看到它时,错误似乎太陌生,太奇怪,太“ GraphQLesque”,而且它对我的应用程序来说太具体了,无法在Google上查找。

When I started reading about GraphQL I thought it was an impressive technology that solved a very specific problem, the problem of querying exactly what you need from an established REST API data and reduce it to basically a “Query Language”. However this worked perfectly when all the data that has been queried does not have missing fields, if we have a REST API that returns [ { ‘lastname’: ‘Lovecraft’ } ] we cannot remove the ‘lastname’ or send it empty (e.g. {‘lastname’: ‘’}) whenever we feel like it, because GraphQL would exclude it from the query if not other entry has a value defined on this field, so if you have something like { user { lastname } } as your GraphQL query in your code and the ‘lastname’ field is empty in your REST API you would get a similar error message as the one you read in the first sentence.

当我开始阅读有关GraphQL的文章时,我认为这是一种令人印象深刻的技术,可以解决一个非常具体的问题,即从已建立的REST API数据中准确查询您所需要的问题,并将其简化为一种“查询语言”。 但是,当所有查询的数据都不缺少字段时,此方法非常有效,如果我们有一个返回[{'lastname':'Lovecraft'}]的REST API,我们将无法删除'lastname'或将其发送为空(例如{'lastname':''}),因为只要没有其他条目在此字段上定义了值,GraphQL就会将其从查询中排除,因此如果您将{user {lastname}}这样的内容作为GraphQL在您的代码中查询,并且REST API中的“姓氏”字段为空,您将得到与第一句中读取的类似的错误消息。

盖茨比-斯特拉皮问题 (The Gatsby-Strapi problem)

Image for post

GatsbyJS is a recent technology that help us to solve an important problem, the problem of generating static websites based on CMS content, it solves this problem making use of GraphQL to query the CMS content data, as I explained at the begining of the first paragraph I was also working on Strapi at the time, Strapi is a very new CMS system that you can clone and modify in practically any way you need to, it has a very powerful content type manager that allows you to create any data model you can think of, some people often use Wordpress as well together with Gatsby experiencing exactly the same error. That means that the problem itself it is not a GraphQL issue nor a Strapi one and certainly not a Wordpress one either, the problem lies in human nature.

GatsbyJS是一项最新技术,可以帮助我们解决一个重要问题,即基于CMS内容生成静态网站的问题,它通过使用GraphQL查询CMS内容数据来解决此问题,正如我在第一段开头所解释的那样当时我还从事Strapi的开发工作,Strapi是一个非常新的CMS系统,您可以用几乎所需的任何方式对其进行克隆和修改,它具有非常强大的内容类型管理器,可让您创建任何您认为可以使用的数据模型当然,有些人经常与Gatsby一起使用Wordpress并遇到完全相同的错误。 这意味着问题本身不是GraphQL的问题,也不是Strapi的问题,当然也不是Wordpress的问题,问题在于人性。

Image for post

Human nature is not seen as a problem on itself, it is something we need to deal with as software engineers (as we do it everyday), there is nothing wrong with Strapi and Wordpress they are just CMSs, and as CMSs they are subjected to change so if the business requires to make a field optional the user can choose not to fill it, that is where the Cannot query field “Title” on type “strapiArticle” error comes from, the user didn’t choose to fill the “title” field on the form we created on our Strapi CMS and the GraphQL query inside our Gatsby project will always look for it and will fail every time it doesn’t find it.

人性本身并不被视为问题,这是我们作为软件工程师需要处理的事情(就像我们每天都在做的那样),对于Strapi和Wordpress来说,它们只是CMS,而作为CMS,则没有错进行更改,以便如果企业要求将字段设为可选,则用户可以选择不填充该字段 ,也就是说, “ strapiArticle类型的“无法查询”字段“ Title”来自错误,因此用户没有选择填充“标题”我们在Strapi CMS上创建的表单上的“”字段以及Gatsby项目中的GraphQL查询将始终查找该字段,并且每次找不到它都会失败。

An interesting property of GraphQL is that the GraphQL query is not going to fail when at least one definition of the the field exists, for example if you have a list of users been returned from an API and at least one user has the “lastname” field defined with a value, GraphQL won’t fail for the other users that doesn’t have the field, that makes the problem even harder to detect early on. One of the cute solutions Gatsby suggests is to always create a dummy entry on Strapi or Wordpress so the GraphQL definitions won’t fail, how cute of them.

GraphQL的一个有趣特性是,当存在至少一个字段定义时,例如,如果您从API返回了用户列表,并且至少有一个用户具有“姓氏”,则GraphQL查询不会失败。使用值定义的字段,对于没有该字段的其他用户,GraphQL不会失败,这使得问题更难以早期发现。 Gatsby建议的一种可爱的解决方案是,始终在Strapi或Wordpress上创建一个虚拟条目,以使GraphQL定义不会失败,因为它们多么可爱。

类型定义作为Gatsby-Strapi问题的解决方案 (Type definitions as a solution to the Gatsby-Strapi problem)

Image for post

Type definitions can be tricky and they can be misleading when you are not really sure about the problem you are solving, when I realized that the solution was only create the definition of the missing field I thought, “oh that’s easy”, but I was wrong, creating type definitions could lead to several ramifications, let’s see an example. What would be the type definition for the next GraphQL query if you want to solve the Cannot query field “Title” on type “strapiArticle” error?

当您不确定要解决的问题时,类型定义可能会很棘手,并且可能会误导我,当我意识到解决方案只是为缺少的字段创建定义时,我以为“哦,这很容易”,但是我错误,创建类型定义可能会导致若干后果,让我们来看一个示例。 如果要解决类型为“ strapiArticle ”错误的“无法查询”字段“标题”,下一个GraphQL查询的类型定义是什么?

Image for post

If you are acquainted with type definitions you would probably be able to write a definition right away, however I was a GraphQL newbie and I just wanted to solve the Cannot query field “Title” on type “strapiArticle” error so after reading the definitions on the GraphQL documentation I tried the following:

如果您熟悉类型定义,则可能可以立即编写定义,但是我是GraphQL新手,我只是想解决类型“ strapiArticle ”错误的Cannot查询字段“ Title”,因此在阅读了以下内容的定义后GraphQL文档中,我尝试了以下操作:

Image for post

That solved the error for that specific field problem, however, the fields “description”, “content” and the “author” section were optional too, the problem was solved but it would pop up again at any time if no dummy entries were created or even if they were created the error will appear again when the dummy entries were accidentally removed.

这就解决了该特定字段问题的错误,但是,“描述”,“内容”和“作者”部分也是可选的,此问题已解决,但如果未创建任何虚拟条目,它将随时弹出。甚至即使它们被创建,当虚拟条目被意外删除时,该错误也会再次出现。

So after hours of research and looking at different GraphQL type definitions on public GitHub repositories (The GraphQL documentation is not that explicit regarding type definitions) and after some trial and error I had came up with a way to define as a type the GraphQL query I’ve showed you at the beginning of the section.

因此,经过数小时的研究并在公共GitHub存储库上查看了不同的GraphQL类型定义(关于类型定义的GraphQL文档不是那么明确),经过反复试验,我想出了一种方法来定义GraphQL查询作为类型I在本节的开头向您展示了。

Image for post

The “author” field from the “strapiArticle” type definition was the hardest to figure out, many people don’t define it that way, actually they don’t define it at all, they usually create another definition called “strapiArticleAuthorArticle” and include the fields there, technically this is another way to do define the type and I don’t blame them, in the face of terror you take regretful decisions, but when I wrote this definition it seemed to me that this was a cleaner way to do it giving a sense of structure to the definition.

最难弄清楚“ strapiArticle”类型定义中的“ author”字段,很多人没有这样定义,实际上他们根本没有定义,他们通常创建另一个定义为“ strapiArticleAuthorArticle”并包括从技术上讲,这是定义类型的另一种方法,我不怪他们,面对恐怖,您会后悔地做出决定,但是当我编写此定义时,在我看来,这是一种更清洁的方法它给定义赋予了结构感。

The full implementation on Gatsby would be adding this piece of code to the “sourceNodes” function inside the “gatsby-node.js” file on the root of your project or plugin in the following fashion.

Gatsby的完整实现将以以下方式将这段代码添加到项目或插件根目录下“ gatsby-node.js”文件中的“ sourceNodes”函数中。

Image for post

Defining your type definitions this way will assign a default “null” value to your properties that can be validated in the logic of your application avoiding to raise exceptions for optional new fields when building your static site.

通过这种方式定义类型定义将为属性分配一个默认的“空”值,该值可以在应用程序的逻辑中进行验证,从而避免在构建静态站点时引发可选新字段的异常。

结论 (Conclusions)

Image for post

The error Cannot query field “Title” on type “strapiArticle” was solved at the end and also future errors with that specific type were avoided (at least the GraphQL ones), this problem teach us that for certain problems there are several solutions and the nature of the solution depends on the way the problem is affecting you. We could have fixed this issue adding a dummy entry to the “strapiArticle” but that will mean that for each new content type we added we would have to do the exact same thing, also if that dummy entry gets deleted by accident the error would pop up crashing the application unexpectedly. I am not telling you that this is not the right way to solve the problem, it actually is (at least for the Gatsby team), but if we wouldn’t have tried to look for a better solution we wouldn’t have learned how the types definitions work on Gatsby and probably this article wouldn’t have been written either. Try to always look for new ways to solve specific problems and never stop asking the question “Is this the best way to do this?”.

最后,解决了“ strapiArticle类型上的错误“无法查询”字段“标题”,并且避免了将来使用该特定类型的错误(至少是GraphQL错误),该问题告诉我们对于某些问题,有几种解决方案,并且解决方案的性质取决于问题影响您的方式。 我们可以解决此问题,并在“ trapiArticle ”中添加一个虚拟条目,但这意味着对于我们添加的每个新内容类型,我们都必须做完全相同的事情,如果该虚拟条目被意外删除,错误也会弹出导致应用程序意外崩溃。 我不是在告诉您这不是解决问题的正确方法,实际上(至少对于盖茨比团队而言)是正确的,但是如果我们不试图寻找更好的解决方案,我们就不会学习如何类型定义可以在Gatsby上使用,并且可能也不会写这篇文章。 尝试始终寻找解决特定问题的新方法,并且永不停止询问“这是否是解决问题的最佳方法?”的问题。

翻译自: https://medium.com/@64lines/gatsby-graphql-and-the-missing-but-necessary-explanation-about-type-definitions-87a5ef83e759

gatsby


http://www.taodudu.cc/news/show-8207939.html

相关文章:

  • 单个样本维度_我们想要训练一个 ML 模型,样本数量有 100 万个,特征维度是 5000,面对如此大数据,如何有效地训练模型(多选)?...
  • PHP少儿课外培训班信息平台 计算机毕设源码17153
  • PHP少儿课外培训班信息平台 计算机毕设源码171535
  • PHP少儿课外培训班信息平台 计算机专业毕业设计源码17153
  • PHP少儿课外培训班信息平台 毕业设计-附源码171535
  • jvm新生代为什么需要2个Survivor
  • 【08-JVM面试专题-JVM运行时数据区堆的结构是怎么划分?为什么分代设计呢?为什么要Survivor区?只有Eden不行吗?为什么要两个Survivor区?为什么Eden:s0:s1是8:1:1】
  • JVM 3. 为什么需要两个survivor区
  • 新生代、老年代、为什么要有Survivor区?看这一篇就够了
  • 为什么新生代有两个Survivor区
  • java堆,新生代,老年代,Eden空间,From Survivor空间,To Survivor空间
  • 为什么新生代有两个Survivor分区
  • JVM Survivor行为一探究竟
  • 记录一次jvm问题:Survivor区非常小 | UseAdaptiveSizePolicy策略
  • java survivor_[Java] JVM(6)为什么新生代有两个Survivor分区?
  • Survivor java_JVM(六)为什么新生代有两个Survivor分区?
  • java survivor_JVM之Survivor区使用率以及对象晋升年龄
  • java survivor_jvm中的新生代Eden和survivor区
  • Survivor java_Survivor空间溢出实例
  • JVM中新生代为什么要有两个Survivor(from,to)
  • JVM内存分配中为什么需要两个Survivor区域(转载)
  • JVM系列之:你知道为什么要有两个 Survivor吗?关于卡表技术又有多少了解
  • 学习Java引起对系统分配内存的思考
  • 对未来的思考
  • 又一家手机厂要被高通断供!唏嘘
  • 阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
  • 人生唏嘘!985大学同宿舍六位室友,15年后现状~
  • 忍住唏嘘.
  • 昔日液晶巨头凉了!令人唏嘘
  • 铜梁安居古镇建西部第一芦苇荡 打造5A级景区
  • 这篇关于gatsby_gatsby graphql和有关类型定义的缺失但必要的解释的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/384785

    相关文章

    wolfSSL参数设置或配置项解释

    1. wolfCrypt Only 解释:wolfCrypt是一个开源的、轻量级的、可移植的加密库,支持多种加密算法和协议。选择“wolfCrypt Only”意味着系统或应用将仅使用wolfCrypt库进行加密操作,而不依赖其他加密库。 2. DTLS Support 解释:DTLS(Datagram Transport Layer Security)是一种基于UDP的安全协议,提供类似于

    嵌入式技术的核心技术有哪些?请详细列举并解释每项技术的主要功能和应用场景。

    嵌入式技术的核心技术包括处理器技术、IC技术和设计/验证技术。 1. 处理器技术    通用处理器:这类处理器适用于不同类型的应用,其主要特征是存储程序和通用的数据路径,使其能够处理各种计算任务。例如,在智能家居中,通用处理器可以用于控制和管理家庭设备,如灯光、空调和安全系统。    单用途处理器:这些处理器执行特定程序,如JPEG编解码器,专门用于视频信息的压缩或解压。在数字相机中,单用途

    请解释Java Web应用中的前后端分离是什么?它有哪些好处?什么是Java Web中的Servlet过滤器?它有什么作用?

    请解释Java Web应用中的前后端分离是什么?它有哪些好处? Java Web应用中的前后端分离 在Java Web应用中,前后端分离是一种开发模式,它将传统Web开发中紧密耦合的前端(用户界面)和后端(服务器端逻辑)代码进行分离,使得它们能够独立开发、测试、部署和维护。在这种模式下,前端通常通过HTTP请求与后端进行数据交换,后端则负责业务逻辑处理、数据库交互以及向前端提供RESTful

    OpenStack:Glance共享与上传、Nova操作选项解释、Cinder操作技巧

    目录 Glance member task Nova lock shelve rescue Cinder manage local-attach transfer backup-export 总结 原作者:int32bit,参考内容 从2013年开始折腾OpenStack也有好几年的时间了。在使用过程中,我发现有很多很有用的操作,但是却很少被提及。这里我暂不直接

    OpenStack实例操作选项解释:启动和停止instance实例

    关于启动和停止OpenStack实例 如果你想要启动和停止OpenStack实例时,有四种方法可以考虑。 管理员可以暂停、挂起、搁置、停止OpenStack 的计算实例。但是这些方法之间有什么不同之处? 目录 关于启动和停止OpenStack实例1.暂停和取消暂停实例2.挂起和恢复实例3.搁置(废弃)实例和取消废弃实例4.停止(删除)实例 1.暂停和取消暂停实例

    Zuul详细解释

    Zuul 是 Netflix 开源的 API 网关,广泛用于微服务架构中。它作为系统的前置网关,主要功能包括路由、负载均衡、限流、安全性管理等。Zuul 最常见的应用场景是作为反向代理,它接收所有来自客户端的请求,并将请求转发给后端的微服务,从而屏蔽了微服务的复杂性。Spring Cloud 集成了 Zuul,使其成为 Spring Cloud 微服务生态系统中的一个重要组件。 为什么使用 Zu

    GetWay详细解释

    Spring Cloud Gateway 是 Spring Cloud 提供的一款全功能 API 网关,作为微服务架构中的流量管理工具,提供了统一的入口来处理来自客户端的所有请求。它具有以下功能:路由请求、限流、熔断、监控、认证与授权等。 Spring Cloud Gateway 的设计基于 Spring 5.0 和 Spring Boot 2.0,并与 Spring WebFlux 深度集成,

    rtklib.h : RTKLIB constants, types and function prototypes 解释

    在 RTKLIB 中,rtklib.h 是一个头文件,包含了与 RTKLIB 相关的常量、类型和函数原型。以下是该头文件的一些常见内容和翻译说明: 1. 常量 (Constants) rtklib.h 中定义的常量通常包括: 系统常量: 例如,GPS、GLONASS、GALILEO 等系统的常量定义。 时间常量: 如一年、一天的秒数等。 精度常量: 如距离、速度的精度标准。 2. 类型

    代码编译过程详细解释

    代码编译过程是将源代码转化为计算机可以执行的机器代码的过程。这个过程分为几个主要阶段,每个阶段负责将源代码逐步转化为最终的可执行文件。以下是详细的编译过程解释: 1. 预处理 (Preprocessing) 功能:处理所有的预处理指令,如 #include、#define、#ifdef 等。 主要操作: 文件包含:替换 #include 指令,插入头文件的内容。宏替换:将 #define

    为控制器的方法添加必要参数

    前言:做这个系统时,要求每次调用接口时要传操作人、操作人电脑ip、菜单id,然后计入log。本来前端读取到然后加入请求头,后端写入log即可。但是老大要求后端也要把控必传参数,避免前端忘记。所以就写了这个。IOperationFilter 这个是百度知道的。主要用于在Swagger生成的API文档中动态地添加或修改操作(即API接口)的元数据。 用处:可以自动地为 Swagger/OpenAPI