graphql 标量类型_使用graphql类型的graphql订阅

graphql 标量类型

Being a developer it’s always cool to explore new libraries and technology.

作为开发人员,探索新的库和技术总是很酷的。

Being fed up with REST API’s, I moved to GraphQL last year. In simple words, GraphQL is not a 100% solution for every endpoint. When it comes to webhooks, simple public routes REST API’s suits better.

我受够了REST API,去年我搬到了GraphQL。 简而言之,GraphQL并不是每个端点的100%解决方案。 当涉及到Webhooks时,简单的公共路由REST API更适合。

So the purpose of this post is to show how to code a simple graphql subscription in typescript.

因此,本文的目的是展示如何在打字稿中编写简单的graphql订阅代码。

Here we’ll be using well known graphql library Apollo Graphql. For typescript compatibility, we’ll be using type-graphql.

在这里,我们将使用众所周知的graphql库Apollo Graphql 。 为了兼容打字稿,我们将使用type-graphql

Okay now let’s take a look at how subscription work in brief.

好了,现在让我们简要地看一下订阅的工作方式。

Image for post

The above image shows the working of a subscription. It's just like a phone call one will listen and one will talk. Here client (subscriber) will be listening to any published event and the server (publisher) will publish an event whenever a certain mutation or query is hit.

上图显示了订阅的工作方式。 就像一个电话,一个人会听,一个人会说话。 此处的客户 (订户) 将侦听任何已发布的事件,并且只要命中某个突变或查询, 服务器 (发布者)就会发布事件。

Here is the folder structure.

这是文件夹结构。

Image for post

Let’s configure the subscription resolver first.

让我们先配置订阅解析器。

Subscription Resolver:

订阅解析器:

Going through the above code. We create a simple query which will return Hello World on request but before returning there is an event being published called MESSAGES. Now we’ll create one more subscription query to listen for the events published. We add topics for which subscription query should listen to, in this case, its MESSAGES. This can be also an array of topics if it’s multiple.

通过上面的代码。 我们创建一个简单的查询,它将返回Hello World 根据要求,但在返回之前,将发布一个名为MESSAGES的事件。 现在,我们将再创建一个订阅查询,以监听发布的事件。 我们添加了订阅查询应监听的主题,在这种情况下,它是MESSAGES 。 如果涉及多个主题,则也可以是一系列主题。

Here is the full code.

这是完整的代码。

输出: (Output:)

翻译自: https://medium.com/@imdhanush/graphql-subscription-using-type-graphql-f65abee4e62d

graphql 标量类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值