原文地址
上一篇 Mutations
Relay文档翻译目录
Relay has a network layer abstraction that separates mutations and queries from the actual machinery that sends requests to the GraphQL server. This gives us the flexibility to configure or even completely replace the default network layer via injection.
Relay抽象了一个network层,用来将mutations、queries与实际的发送给GraphQL server的请求分开了。它让我们配置起来很灵活,甚至通过injection可以替换默认的network层。
Default Network Layer
Relay is pre-configured to use a default network layer that works with express-graphql. This default network layer is exposed via Relay.DefaultNetworkLayer.
Relay预先配置好了使用默认的network层,它可以同express-graphql配合工作。默认的network层通过Relay.DefaultNetworkLayer暴露。
By default, Relay assumes that GraphQL is served at /graphql relative to the origin where our application is served. This can be re-configured by injecting a custom instantiation of the default network layer.
默认情况下,Relay假设GraphQL通过应用的相同域下的/graphql路径进行服务。这是可以定制改变的。
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://example.com/graphql')
);
Underneath the hood, the default network layer uses fetch (Living Standard). The constructor for Relay.DefaultNetworkLayer takes an optional second argument that accepts any valid initialization property that fetch accepts.
在这样的机制下,默认network层使用fetch (Living Standard)。Relay.DefaultNetworkLayer的构造函数接收一个可选的第二个参数,用于接收一个有效的fetch初始化配置。
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://example.com/graphql', {
credentials: 'same-origin',
})
);
When it sends queries, it will automatically fail requests after a 15 second timeout. Also, failed requests are automatically retried twice, with a 1 second delay and a 3 second delay, respectively.
当发送query请求时,默认15秒超时后自动失败处理。同时,失败的请求会自动重试两次,一次是1秒后,一次是3秒后。
Like the GraphQL URI, the timeout and retry behavior can be configured:
如GraphQL URI一样,超时和重试都是可以配置的:
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://example.com/graphql', {
fetchTimeout: 30000, // Timeout after 30s.
retryDelays: [5000], // Only retry once after a 5s delay.
})
);
Unlike queries, failed requests for mutations are not automatically retried.
与query不同的是,mutation失败的请求不是自动重试的。
Custom HTTP headers can be configured by providing a headers object:
用户定义的HTTP header可以通过headers对象配置。
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://example.com/graphql', {
headers: {
Authorization: 'Basic SSdsbCBmaW5kIHNvbWV0aGluZyB0byBwdXQgaGVyZQ==',
},
})
);
Custom Network Layers
Relay also lets us completely replace the default network layer.
Relay也允许我们完全的替换默认的network层。
Custom network layers must conform to the following RelayNetworkLayer interface. Although the default network layer is an instantiable class that accepts some configuration, this is not a requirement of an injected network layer.
自定义的network层必须遵循RelayNetworkLayer的接口规范。虽然默认的network层是一个实例化的类并可以接受一些配置参数,但对于注入的network层并不是强制要求。
For example, a network layer can be a simple object that conforms to the interface:
例如,一个network层,可以是一个满足接口标准很简单的对象:
var myNetworkLayer = {
sendMutation(mutationRequest) {
// ...
},
sendQueries(queryRequests) {
// ...
},
supports(...options) {
// ...
},
};
Relay.injectNetworkLayer(myNetworkLayer);
You can read more about the API RelayNetworkLayer interface.
更多请阅读API。
本文介绍了Relay中网络层的配置方法,包括默认配置、自定义配置及如何替换默认网络层等内容。通过调整网络层设置,可以实现更灵活的数据请求方式。
1353

被折叠的 条评论
为什么被折叠?



