Graphql 查询graphiql导致Apollo错误转发不是一个函数
当我转到Graphql 查询graphiql导致Apollo错误转发不是一个函数,graphql,apollo,react-apollo,graphiql,Graphql,Apollo,React Apollo,Graphiql,当我转到/graphiql并手动执行一些搜索时,我有一个带有GraphQL的快速后端。我的React前端正在尝试在后端执行搜索。以下代码应异步执行查询: const data = await this.props.client.query({ query: MY_QUERY, variables: { initials: e.target.value } }); console.log(data); 其中,MY_QUERY是在前面定义的,表示我知道可以工作并已在/graphiq
/graphiql
并手动执行一些搜索时,我有一个带有GraphQL的快速后端。我的React前端正在尝试在后端执行搜索。以下代码应异步执行查询:
const data = await this.props.client.query({
query: MY_QUERY,
variables: { initials: e.target.value }
});
console.log(data);
其中,MY_QUERY
是在前面定义的,表示我知道可以工作并已在/graphiql
上测试过的查询。要在我的React组件中实现这一点,我将其导出为export default with Apollo(MyComponent)
,这样它在道具中就有了客户端
变量
在我通过Apollo定义的index.js
文件中,连接到/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
});
在没有处理错误的链接
变量的情况下执行上述查询时,我从服务器接收到一个400错误请求
(apolleror.js:37未捕获(承诺中)错误:网络错误:响应未成功:收到状态代码400
)。由于这没有告诉我更多,在StackOverflow和Apollo网页上,我找到了上面的错误声明,它输出[网络错误]:TypeError:forward不是一个函数
。这个错误意味着什么?我该如何解决它
谢谢 您的客户端配置有一个重复的属性--您首先将链接
属性设置为HttpLink
,然后再次将其设置为错误链接
。这意味着将完全忽略HttpLink
,您只需将ErrorLink
传递给配置。您看到该错误是因为由onError
创建的ErrorLink
不打算自己使用。相反,它应该与HttpLink
链接,这就是您应该分配给link
属性的内容
文档中详细介绍了如何正确组合链接。您可以使用concat
,但我更喜欢ApolloLink.from
,因为它允许您清楚地显示链接的顺序:
const errorLink = onError(...)
const httpLink = new HttpLink(...)
const link = ApolloLink.from([
errorLink,
httpLink,
])
const client = new ApolloClient({
link,
cache,
})