惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
雷峰网
雷峰网
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
Y
Y Combinator Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
罗磊的独立博客
爱范儿
爱范儿
F
Full Disclosure
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
G
Google Developers Blog
腾讯CDC
美团技术团队
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
B
Blog
Recorded Future
Recorded Future
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
Recent Announcements
Recent Announcements
P
Palo Alto Networks Blog
Apple Machine Learning Research
Apple Machine Learning Research
MongoDB | Blog
MongoDB | Blog
G
GRAHAM CLULEY
A
Arctic Wolf
AWS News Blog
AWS News Blog
Project Zero
Project Zero
博客园 - Franky
V
Vulnerabilities – Threatpost

Blog | GraphQL

GraphQL Foundation Monthly Newsletter April 2021 GraphQL Foundation Monthly Newsletter March 2021 GraphQL Foundation Monthly Newsletter February 2021 Improving Latency with @defer and @stream Directives GraphQL Foundation Monthly Newsletter October 2020 GraphQL Foundation Monthly Newsletter September 2020 Google Season of Docs 2020 Participant: Carolyn Stransky GraphQL Foundation Monthly Newsletter August 2020 Google Summer of Code 2020 Participant: Naman GraphQL joins Google Season of Docs Web-based GraphQL IDEs for the win: How & Why Playground & GraphiQL are joining forces Announcing the 1st GraphQL Foundation Annual Report Linux Foundation Training Announces a Free Online Course-Exploring GraphQL: A Query Language for APIs GraphQL Foundation Launches Interactive Landscape and Welcomes New Members from Open Source Summit Europe The GraphQL Foundation Announces Collaboration with the Joint Development Foundation to Drive Open Source and Open Standards Channel Futures: GraphQL API Query Language Growing, Gets Own Support Foundation Datanami: Will GraphQL Become a Standard for the New Data Economy? SD Times: The Linux Foundation announces plans to form GraphQL foundation The Register: Facebook’s open-source license drama-zone GraphQL gets swanky digs in Linux mansion eWeek: GraphQL API Specification Moving Forward with Independent Foundation InfoWorld: GraphQL gets its own foundation The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL ProgrammableWeb: GraphQL Moving to Neutral, Open-Source Foundation Leaving technical preview | GraphQL Wrapping a REST API in GraphQL Mocking your server is easy with GraphQL GraphQL: A data query language
Subscriptions in GraphQL and Relay
Dan Schafer · 2015-10-16 · via Blog | GraphQL

When we announced and open-sourced GraphQL and Relay this year, we described how they can be used to perform reads with queries, and to perform writes with mutations. However, oftentimes clients want to get pushed updates from the server when data they care about changes. To support that, we’ve introduced a third operation into the GraphQL specification: subscription.

Event-based subscriptions#

The approach that we’ve taken to subscriptions parallels that of mutations; just as the list of mutations that the server supports describes all of the actions that a client can take, the list of subscriptions that the server supports describes all of the events that it can subscribe to. Just as a client can tell the server what data to refetch after it performs a mutation with a GraphQL selection, the client can tell the server what data it wants to be pushed with the subscription with a GraphQL selection.

For example, in the Facebook schema, we have a mutation field named storyLike, that clients can use to like a post. The client might want to refetch the like count, as well as the like sentence (“Dan and 3 others like this”. We do this translation on the server because of the complexity of that translation in various languages). To do so, they would issue the following mutation:

mutation StoryLikeMutation($input: StoryLikeInput) {

storyLike(input: $input) {

story {

likers { count }

likeSentence { text }

}

}

}

But when you’re looking at a post, you also want to get pushed an update whenever someone else likes the post! That’s where subscriptions come in; the Facebook schema has a subscription field named storyLikeSubscribe that allows the client to get pushed data anytime someone likes or unlikes that story! The client would create a subscription like this:

subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {

storyLikeSubscribe(input: $input) {

story {

likers { count }

likeSentence { text }

}

}

}

The client would then send this subscription to the server, along with the value for the $input variable, which would contain information like the story ID to which we are subscribing:

input StoryLikeSubscribeInput {

storyId: string

clientSubscriptionId: string

}

At Facebook, we send this query to the server at build time to generate a unique ID for it, then subscribe to a special MQTT topic with the subscription ID in it, but many different subscription mechanisms could be used here.

On the server, we then trigger this subscription every time someone likes a post. If all of our clients were using GraphQL, we could put this hook in the GraphQL mutation; since we have non-GraphQL clients as well, we put the hook in a layer below the GraphQL mutation to ensure it always fires.

Why not Live Queries?#

Notably, this approach requires the client to subscribe to events that it cares about. Another approach is to have the client subscribe to a query, and ask for updates every time the result of that query changes. Why didn’t we take that approach?

Let’s look back at the data we wanted to refetch for the story:

fragment StoryLikeData on Story {

story {

likers { count }

likeSentence { text }

}

}

What events could trigger that a change to the data fetched in that fragment?

  • Someone likes the post.
  • Someone unlikes the post.
  • Someone who had liked the post deactivates their account (changes the like count down one, changes the like sentence to decrement the translated count).
  • Someone who had liked the post reactivates their account (changes the like count up one, changes the like sentence to increment the translated count).
  • Someone who had liked the post blocks you (cannot show them in the like sentence).
  • Someone who had liked the post changes their name (need to update the text of the like sentence).
  • Our internal ranking model for the ordering of names in the like sentence updates, and we should be listing a different person first (want to update the text of the like sentence).

And that’s just the tip of the iceberg in terms of events; each of those events also becomes tricky when there are thousands of people subscribed, and millions of people who liked the post. Implementing live queries for this set of data proved to be immensely complicated.

When building event-based subscriptions, the problem of determining what should trigger an event is easy, since the event defines that explicitly. It also proved fairly straight-forward to implement atop existing message queue systems. For live queries, though, this appeared much harder. The value of our fields is determined by the result of their resolve function, and figuring out all of the things that could alter the result of that function was difficult. We could in theory have polled on the server to implement this, but that had efficiency and timeliness issues. Based on this, we decided to invest in the event-based subscription approach.

What’s next?#

We’re actively building out the event-based subscription approach described above. We’ve built out live liking and commenting features on our iOS and Android apps using that approach, and are continuing to flesh out its functionality and API. While its current implementation at Facebook is coupled to Facebook’s infrastructure, we’re certainly looking forward to open sourcing our progress here as soon as we can.

Because our backend and schema don’t offer easy support for live queries, we don’t have any plans to develop them at Facebook. At the same time, it’s clear that there are backends and schemas for which live queries are feasible, and that they offer a lot of value in those situations. The discussion in the community on this topic has been fantastic, and we’re excited to see what kind of live query proposals emerge from it!

Subscriptions create a ton of possibilities for creating truly dynamic applications. We’re excited to continue developing GraphQL and Relay with the help of the community to enable these possibilities.