【发布时间】:2019-03-26 09:12:57
【问题描述】:
我有一个使用 GraphQL 的快速后端,当我转到 /graphiql 并手动执行一些搜索时,它可以工作。我的 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 withApollo(MyComponent),以便它在 props 中具有 client 变量。
在 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
});
在没有处理错误的link变量的情况下执行上述查询时,我从服务器(ApolloError.js:37 Uncaught (in promise) Error: Network error: Response not successful: Received status code 400)收到400 Bad Request。由于这并没有告诉我更多信息,因此在 StackOverflow 和 Apollo 网页上,我发现了上面输出 [Network error]: TypeError: forward is not a function 的错误声明。这个错误是什么意思,我该如何解决?
谢谢!
【问题讨论】:
标签: graphql apollo react-apollo graphiql