Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Installing @apollo/client for React gives Eslint errors. #1169

@RoyBkker

Description

@RoyBkker

Hi all,

When starting a new project, Eslint gives the following errors on Apollo imports, which is new for me. Previous projects with same setup, doesn't complain about it.

This is my apolloProvider.tsx:

import React from "react";

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

import { setContext } from "@apollo/client/link/context";
import { onError } from "@apollo/client/link/error";
import { getMainDefinition } from "@apollo/client/utilities";
import { WebSocketLink } from "@apollo/client/link/ws";

import { getBackendUri, getWebsocketUri } from "../utils/getEnv";

const httpLink = createHttpLink({
  uri: `${getBackendUri()}/graphql`,
  credentials: "include",
});

const wsLink = new WebSocketLink({
  uri: `${getWebsocketUri()}`,
  options: {
    reconnect: true,
  },
});

// The split function takes three parameters:
//
// * A function that's called for each operation to execute
// * The Link to use for an operation if the function returns a "truthy" value
// * The Link to use for an operation if the function returns a "falsy" value
const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === "OperationDefinition" &&
      definition.operation === "subscription"
    );
  },
  wsLink,
  httpLink
);

const authLink = setContext((_, { headers }) => {
  // get the authentication token from local storage if it exists
  const token = localStorage.getItem("token");
  // return the headers to the context so httpLink can read them
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : "",
    },
  };
});

const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${JSON.stringify(
          locations
        )}, Path: ${path}`
      )
    );

  if (networkError) console.log(`[Network error]: ${networkError}`);
});

const client = new ApolloClient({
  link: from([authLink, errorLink, splitLink]),
  cache: new InMemoryCache(),
});

interface Props {
  children: React.ReactNode;
}
const Client = (props: Props) => {
  const { children } = props;
  return <ApolloProvider client={client}>{children}</ApolloProvider>;
};

export default Client;

This is the error from Eslint:

'@apollo/client/link/ws' should be listed in the project's dependencies. Run 'npm i -S @apollo/client/link/ws' to add iteslintimport/no-extraneous-dependencies

Running the provided command is not working.

The error is for all the following imports:

import { setContext } from "@apollo/client/link/context";
import { onError } from "@apollo/client/link/error";
import { getMainDefinition } from "@apollo/client/utilities";
import { WebSocketLink } from "@apollo/client/link/ws";

Running "@apollo/client": "^3.3.18"

Anyone else had this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions