注入自定义网络 url 不工作 relayjs

Injecting custom network url not working relayjs

我正在关注这个示例应用程序:https://github.com/relayjs/relay-starter-kit

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('http://localhost:8080/graphql')
);

使用自定义 url 我收到以下错误

OPTIONS http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @ relay.js:21558Promise @
app.js:3335self.fetch @ relay.js:21508sendTimedRequest @
relay.js:20131module.exports @ relay.js:20186tryCallTwo @
relay.js:3762doResolve @ relay.js:3879Promise @
relay.js:3782fetchWithRetries @ relay.js:20122_sendQuery @
relay.js:13042(anonymous function) @ relay.js:12976sendQueries @
relay.js:12975sendQueries @ relay.js:3502processQueue @
relay.js:17591(anonymous function) @ relay.js:17582tryCallOne @
relay.js:3754(anonymous function) @ relay.js:3820flush @ relay.js:9457
relay.js:21558 OPTIONS http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @ relay.js:21558Promise @
app.js:3335self.fetch @ relay.js:21508sendTimedRequest @
relay.js:20131 relay.js:21558 OPTIONS http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @ relay.js:21558Promise @
app.js:3335self.fetch @ relay.js:21508sendTimedRequest @
relay.js:20131 relay.js:7449 Network request failed_markAsRejected @
relay.js:7449_handleSubtractedQueryFailure @ relay.js:7430tryCallOne @
relay.js:3754(anonymous function) @ relay.js:3820flush @ relay.js:9457

我猜您的应用程序在端口 3000 上 运行。如果您尝试向不同的 domain/port 组合发出客户端=>服务器请求,而不启用此类跨源请求服务器端,您的浏览器将使用其默认的 CORS 策略来阻止请求。

您可以在服务器端启用来自所有来源的请求,如下所示:

import cors from 'cors';

var graphQLServer = express();
graphQLServer.use(cors());
/* ... */

查看 cors docs 以获得更精细的控制。

请务必 npm install --save cors,无论您的服务器代码位于何处。