Ing*_*yng 5 javascript apollo reactjs graphql apollo-client
我正在用 做一个简单的测试@apollo/react-hooks
,我收到了这个错误:
ApolloError.ts:46 Uncaught (in promise) Error: Network error: Unexpected end of JSON input
at new ApolloError (ApolloError.ts:46)
at Object.error (QueryManager.ts:255)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at observables.ts:15
at Set.forEach (<anonymous>)
at Object.error (observables.ts:15)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at Object.error (index.ts:81)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at httpLink.ts:184
Run Code Online (Sandbox Code Playgroud)
当我尝试使用这样的突变时:
ApolloError.ts:46 Uncaught (in promise) Error: Network error: Unexpected end of JSON input
at new ApolloError (ApolloError.ts:46)
at Object.error (QueryManager.ts:255)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at observables.ts:15
at Set.forEach (<anonymous>)
at Object.error (observables.ts:15)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at Object.error (index.ts:81)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at httpLink.ts:184
Run Code Online (Sandbox Code Playgroud)
我的 Apollo 客户端如下所示:
import React from 'react';
import { Button } from 'antd';
import { gql } from 'apollo-boost';
import { useMutation } from '@apollo/react-hooks';
const LOGIN = gql`
mutation authentication($accessToken: String!) {
login(accessToken: $accessToken) {
id
name
email
groups
}
}
`;
function Authenticating() {
const [login] = useMutation(LOGIN);
function handleClick() {
const variables = { variables: { accessToken: 'access_token_here' } };
azureLogin(variables).then(data => {
console.log(data);
}).catch(err => {
console.log(err)
});
}
return (
<Button onClick={handleClick}>Test</Button>
);
}
export default Authenticating;
Run Code Online (Sandbox Code Playgroud)
并且该Authenticating
组件由一个ApolloProvider
.
import React from 'react';
import { ApolloProvider } from '@apollo/react-hooks';
import Authenticating from './Authenticating';
import apolloClient from './apolloClient';
const App = () => {
<ApolloProvider client={apolloClient}>
<Authenticating/>
</ApolloProvider>
}
export default App;
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会收到此错误。
代码没有问题,是后端没有CORS配置。
这是一个 ruby 服务器,并且未配置机架。
后端修复后,我还将 apollo 客户端更改为这样:
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: <graphql_server>,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
fetch,
});
export default client;
Run Code Online (Sandbox Code Playgroud)
删除了fetchOptions
mode: 'no-cors'
:
fetchOptions: {
mode: 'no-cors',
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3500 次 |
最近记录: |