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

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

Blog | GraphQL

GraphQL Foundation Monthly Newsletter April 2021 GraphQL Foundation Monthly Newsletter March 2021 GraphQL Foundation Monthly Newsletter February 2021 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 Subscriptions in GraphQL and Relay GraphQL: A data query language
Improving Latency with @defer and @stream Directives
Rob Richard, · 2020-12-08 · via Blog | GraphQL

Rob Richard and Liliana Matos are front-end engineers at 1stDibs.com. They have been working with the GraphQL Working Group as champions of the the @defer and @stream directives.

The @defer and @stream directives have been a much anticipated set of features ever since Lee Byron first talked about it at GraphQL Europe 2016. For most of 2020, we have been working with the GraphQL Working Group to standardize this feature. It is now a Stage 2 proposal, but to advance further, we are looking to the GraphQL community to try using these directives and provide feedback. We have released experimental versions of GraphQL.js and express-graphql. They are published to npm under graphql@experimental-stream-defer and express-graphql@experimental-stream-defer. We encourage everyone interested in this feature to try out these releases and let us know how it goes in the issue created for feedback. Read on to find out more about what this proposal offers.

One of the disadvantages of GraphQL’s request/response model is that the GraphQL response is not returned to clients until the entire request has finished processing. However, not all requested data may be of equal importance, and in some use cases it may be possible for applications to act on a subset of the requested data. An application can speed up its time-to-interactive if the GraphQL server can send the most important data as soon as it’s ready. The new @defer and @stream directives allow GraphQL servers to do exactly that by returning multiple payloads from a single GraphQL response.

The @defer directive can be applied to fragment spreads and inline fragments. It is a declarative way for developers to mark parts of a query as non-essential for immediate return.

Here’s an example of the @defer directive:

Request#

query {

person(id: "cGVvcGxlOjE=") {

name

...HomeworldFragment @defer(label: "homeworldDefer")

}

fragment HomeworldFragment on Person {

homeworld {

name

}

}

Response#

Payload 1

{

"data": {

"person": {

"name": "Luke Skywalker"

}

},

"hasNext": true

}

Payload 2

{

"label": "homeworldDefer",

"path": ["person"],

"data": {

"homeworld": {

"name": "Tatooine"

}

},

"hasNext": false

}

When the GraphQL execution engine encounters the @defer directive, it will fork execution and begin to resolve those fields asynchronously. While the deferred payload is still being prepared, the client can receive and act on the initial payload. This is most useful when the deferred data is large, expensive to load, or not on the critical path for interactivity.

Similar to @defer, the @stream directive also allows the client to receive data before the entire result is ready. @stream can be used on list fields. Here’s an example of the @stream directive:

Request#

query {

person(id: "cGVvcGxlOjE=") {

name

films @stream(initialCount: 2, label: "filmsStream") {

title

}

}

Response#

Payload 1

{

"data": {

"person": {

"name": "Luke Skywalker",

"films": [

{ "title": "A New Hope" },

{ "title": "The Empire Strikes Back" }

]

}

},

"hasNext": true

}

Payload 2

{

"label": "filmsStream",

"path": ["person", "films", 2],

"data": {

"title": "Return of the Jedi"

},

"hasNext": true

}

Payload 3

{

"label": "filmsStream",

"path": ["person", "films", 3],

"data": {

"title": "Revenge of the Sith"

},

"hasNext": false

}

When the GraphQL execution engine encounters the @stream directive, it will resolve as many list items specified by the initialCount argument. The rest will be resolved asynchronously. This is especially useful for an interface where only a few elements can be rendered above the fold. The client can render these elements as soon as possible while the server is still resolving the rest of the data.

While the GraphQL specification doesn’t specify transport protocols, we expect the most common transport for queries with @defer/@stream to be HTTP with chunked transfer encoding. This allows a GraphQL server to keep a standard HTTP connection open, while streaming each payload to the client as soon as it’s ready. It has low overhead, has been supported by browsers for decades, and can work easily with most infrastructure.

You can learn more about these directives at:

Rob Richard, Liliana Matos, Front-End Engineering, 1stDibs.com