Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApolloClient Type errors for ApolloLink

I'm trying to create ApolloClient using TypeScript, but there are some type-errors that I can't resolve. Can anyone point me to right direction what to do?

Below are the sample code (which is working ok with JavaScript) for creating client:

import {
  ApolloClient,
  ApolloProvider,
  InMemoryCache,
  split
} from '@apollo/client';

import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';

const authLink = setContext((_, { headers }) => {
  const token = localStorage.getItem('consequat-token');
  return {
    headers: {
      ...headers,
      authorization: token ? `bearer ${token}` : null
    }
  };
});

const httpLink = createHttpLink({ uri: 'http://localhost:4000' });

const wsLink = new WebSocketLink({
  uri: 'ws://localhost:4000/graphql',
  options: { reconnect: true },
});

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  wsLink,
  authLink.concat(httpLink)
);

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: splitLink
});

Problem is that authLink.concat(httpLink) line is complaining:

Argument of type 'ApolloLink' is not assignable to parameter of type 'ApolloLink | RequestHandler | undefined'.
Type 'ApolloLink' is missing the following properties from type 'ApolloLink': onError, setOnError ts(2345)

I can't find any answers from Apollo docs or Google.

like image 757
darx Avatar asked Jul 09 '20 11:07

darx


People also ask

How do you handle errors in Apollo GraphQL?

By default, Apollo Client throws away partial data and populates the error. graphQLErrors array of your useQuery call (or whichever hook you're using). You can instead use these partial results by defining an error policy for your operation. If the response includes GraphQL errors, they are returned on error.

What is Apollolink?

The Apollo Link library helps you customize the flow of data between Apollo Client and your GraphQL server. You can define your client's network behavior as a chain of link objects that execute in a sequence: Apollo Client.

How do you send an error response in GraphQL?

To add errors to your data, you need to use the Union type (a.k.a. Result ) in your GraphQL schema. Also, the logic in the resolver is rewritten so that next to the result or error for the operation you also need to return a type for it.

How are HTTP requests sent ApolloClient authenticated?

Luckily, Apollo provides a nice way for authenticating all requests by using the concept of middleware, implemented as an Apollo Link. import { setContext } from '@apollo/client/link/context'; This middleware will be invoked every time ApolloClient sends a request to the server.


1 Answers

Answer to my own post:

import {
  ApolloClient,
  ApolloProvider,
  InMemoryCache,
  split
} from '@apollo/client';

import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';

needs to be changed to:

import {
  ApolloClient,
  ApolloProvider,
  InMemoryCache,
  HttpLink,
  split
} from '@apollo/client';

import { setContext } from '@apollo/link-context';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';

those @apollo/ and apollo- libraries was not compatible. Also, createHttpLink replaced with HttpLink imported from @apollo/client, and it's usage:

const httpLink = createHttpLink({ uri: 'http://localhost:4000' });

changed to:

const httpLink = new HttpLink({ uri: 'http://localhost:4000' });
like image 118
darx Avatar answered Sep 28 '22 00:09

darx