Mas*_*iar 2 apollo graphql react-apollo graphiql
我有一个带有 GraphQL 的快速后端,当我去/graphiql手动执行一些搜索时它可以工作。我的 React 前端正在尝试在后端执行搜索。以下代码应异步执行查询:
const data = await this.props.client.query({
query: MY_QUERY,
variables: { initials: e.target.value }
});
console.log(data);
Run Code Online (Sandbox Code Playgroud)
WhereMY_QUERY是之前定义的,代表一个我知道有效并已在/graphiql. 在我做出反应组分I出口它这样做,因为export default withApollo(MyComponent)这样它具有client可变props。
在index.js我通过 Apollo 定义的文件中,连接到/graphiql以执行查询:
//link defined to deal with errors, this was found online
const link = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
//the httpLink to my GraphQL instance, BASE_URL is defined elsewhere
const httpLink = new HttpLink({
uri: BASE_URL,
headers: {
},
});
//here I define the client linking the GraphQL instance, the cache, and error handling
const client = new ApolloClient({
link: httpLink,
cache,
link
});
Run Code Online (Sandbox Code Playgroud)
在没有link处理错误的变量的情况下执行上述查询时,我400 Bad Request从服务器 ( ApolloError.js:37 Uncaught (in promise) Error: Network error: Response not successful: Received status code 400)收到一个。由于这并没有告诉我更多信息,因此在 StackOverflow 和 Apollo 网页上,我发现了上面输出[Network error]: TypeError: forward is not a function. 这个错误是什么意思,我该如何解决?
谢谢!
您的客户端配置有一个重复的属性——您首先将该link属性设置为您的HttpLink,然后再将其设置为您的ErrorLink. 这意味着HttpLink被完全忽略,您只是将 传递ErrorLink给配置。您看到该错误是因为ErrorLinkcreated byonError本身并不打算使用。相反,它应该与 链接,HttpLink这就是您应该分配给link属性的内容。
文档中的此页面详细介绍了如何正确撰写链接。您可以使用,concat但我更喜欢,ApolloLink.from因为它可以让您清楚地显示链接的顺序:
const errorLink = onError(...)
const httpLink = new HttpLink(...)
const link = ApolloLink.from([
errorLink,
httpLink,
])
const client = new ApolloClient({
link,
cache,
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3149 次 |
| 最近记录: |