Reputation: 305
I'm making a simple test with @apollo/react-hooks
, and I'm getting this error:
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
When I try to use a mutation like this:
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;
My Apollo client looks like this:
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: <graphql_server>,
fetchOptions: {
mode: 'no-cors',
},
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
fetch,
});
export default client;
And the Authenticating
component is wrapped by an 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;
I have no clue why I'm getting this error.
Upvotes: 9
Views: 10452
Reputation: 305
There is nothing wrong with the code, it was the backend that didnt have the CORS configuration.
It's a ruby server, and the rack-cors wasn't configurated.
I also changed the apollo client to this, after the backend was fixed:
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;
Removed the fetchOptions
mode: 'no-cors'
:
fetchOptions: {
mode: 'no-cors',
}
Upvotes: 7