I'm running an online GraphQL API, it seems to work fine when I'm trying postman for example to make requests, but always fails, with Apollo Link React-native, that returns error: TypeError: Network request failed.
const cache = new InMemoryCache();
const token = this.props.account.accessToken || "";
const httpLink = createHttpLink({
uri: config.graphql_host,
credentials: "same-origin",
});
const websocket = new WebSocketLink({
uri: `ws://${config.ws_host}`,
options: {
reconnect: true,
connectionParams: { authorization: `Bearer ${token}` },
},
});
const middleware = new ApolloLink((operation, forward) => {
operation.setContext({ headers: { authorization: `Bearer ${token}` } });
return forward(operation);
});
const splitlink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
websocket,
httpLink
);
const client = new ApolloClient({
connectToDevTools: true,
link: concat(middleware, splitlink),
cache: cache,
defaultOptions: {
watchQuery: { fetchPolicy: "cache-and-network", errorPolicy: "all" },
query: { fetchPolicy: "cache-and-network", errorPolicy: "all" },
},
mutate: { errorPolicy: "all" },
});
package.json:
"@apollo/client": "^3.1.3",
"@apollo/react-hooks": "^3.1.5",
"apollo-link-http": "^1.5.17",
"apollo-cache-inmemory": "^1.6.6",
I get the error as follow:
Error: Network error: Network request failed
at new ApolloError (bundle.umd.js:92)
at Object.error (bundle.umd.js:1330)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at bundle.umd.js:1107
at Set.forEach (<anonymous>)
at Object.error (bundle.umd.js:1106)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)