Graphql 与在客户端中创建NetworkInterface相关的问题
我需要向graphql端点发出自己的请求。(默认实现对我们指定的url进行POST调用)。我需要打个AJAX电话。我从以下地方读到: 它明确指定我需要创建一个NetworkInterface并将其传递给客户端 我是这样做的:Graphql 与在客户端中创建NetworkInterface相关的问题,graphql,react-apollo,apollo-client,Graphql,React Apollo,Apollo Client,我需要向graphql端点发出自己的请求。(默认实现对我们指定的url进行POST调用)。我需要打个AJAX电话。我从以下地方读到: 它明确指定我需要创建一个NetworkInterface并将其传递给客户端 我是这样做的: const customInterface = { query(request) { return $.post(url_of_graphql_endpoint) .send({query : request.query.l
const customInterface = {
query(request) {
return $.post(url_of_graphql_endpoint)
.send({query : request.query.loc.source.body})
.end()
.then(result => result.text)
}
}
const client = new ApolloClient({networkInterface: customInterface});
ReactDOM.render((
<ApolloProvider client={client}>
<Router history={browserHistory}>
<Route path='/' component={App} />
</Router>
</ApolloProvider>
),
document.getElementById('root')
)
const customInterface={
查询(请求){
return$.post(图的url\u\u端点)
.send({query:request.query.loc.source.body})
(完)
.then(result=>result.text)
}
}
const-client=new-client({networkInterface:customInterface});
ReactDOM.render((
),
document.getElementById('root'))
)
正在向graphql端点发出网络请求,服务器也会响应apt响应(它应该发送)
即使在这之后,我的浏览器也会出现一些错误,因为我无法显示从服务器获取的数据。组件始终处于“加载”状态
Error in observer.error
Error
at new ApolloError (<URL>:12345/bundle.js:22220:23)
at <URL>:12345/bundle.js:21680:39
at <URL>:12345/bundle.js:22168:25
at Array.forEach (native)
at <URL>:12345/bundle.js:22165:27
at <URL>:12345/bundle.js:81569:11
at baseForOwn (<URL>:12345/bundle.js:47066:20)
at forOwn (<URL>:12345/bundle.js:82561:20)
at QueryManager.broadcastQueries (<URL>:12345/bundle.js:22163:9)
at Array.<anonymous> (<URL>:12345/bundle.js:21595:23)
at dispatch (<URL>:12345/bundle.js:56461:19)
at <URL>:12345/bundle.js:64699:30
at Object.dispatch (<URL>:12345/bundle.js:17264:16)
at <URL>:12345/bundle.js:22130:33
at tryCatcher (<URL>:12345/bundle.js:7247:23)
at Promise._settlePromiseFromHandler (<URL>:12345/bundle.js:5269:31)
at Promise._settlePromise (<URL>:12345/bundle.js:5326:18)
at Promise._settlePromise0 (<URL>:12345/bundle.js:5371:10)
at Promise._settlePromises (<URL>:12345/bundle.js:5446:18)
at <URL>:12345/bundle.js:2169:25
at MutationObserver.<anonymous> (<URL>:12345/bundle.js:6501:17)
observer.Error中的错误
错误
新建时出错(:12345/bundle.js:22220:23)
地址:12345/bundle.js:21680:39
地址:12345/bundle.js:22168:25
at Array.forEach(本机)
地址:12345/bundle.js:22165:27
地址:12345/bundle.js:81569:11
在baseForOwn(:12345/bundle.js:47066:20)
在forOwn(:12345/bundle.js:82561:20)
在QueryManager.broadcastQueries(:12345/bundle.js:22163:9)
在阵列上。(:12345/bundle.js:21595:23)
发送时(:12345/bundle.js:56461:19)
电话:12345/bundle.js:64699:30
在Object.dispatch(:12345/bundle.js:17264:16)
地址:12345/bundle.js:22130:33
在tryCatcher(:12345/bundle.js:7247:23)
按承诺。_settlePromiseFromHandler(:12345/bundle.js:5269:31)
在承诺中。_结算协议(:12345/bundle.js:5326:18)
按承诺支付。结算承诺0(:12345/bundle.js:5371:10)
按承诺。_结算承诺(:12345/bundle.js:5446:18)
地址:12345/bundle.js:2169:25
在观察者处。(:12345/bundle.js:6501:17)
我能够覆盖查询方法的功能,因此我只为可能面临相同问题的其他人发布答案
我试图覆盖查询方法的方式是错误的。我是这样做的
class CustomNetworkInterface extends HttpNetworkInterface{
query(request){
return $.post(url_of_graphql_endpoint)
.send({query : request.query.loc.source.body})
.end()
.then(result => result.text)
}
}
要创建客户端,只需传入CustomNetworkInterface的对象
const client = new ApolloClient({networkInterface: new CustomNetworkInterface(http://url)});
ReactDOM.render((
<ApolloProvider client={client}>
<Router history={browserHistory}>
<Route path='/' component={App} />
</Router>
</ApolloProvider>
),
document.getElementById('root')
)
const-client=new-client({networkInterface:new-CustomNetworkInterface(http://url)});
ReactDOM.render((
),
document.getElementById('root'))
)