Модуль не найден: не удается разрешить «apollo-link-context» - РЕАКТ, APOLLO-SERVER - PullRequest
0 голосов
/ 04 февраля 2020

У меня есть серверная часть с пользователями, которую я хочу отобразить в своей папке веб-интерфейса. Для этого мне нужно, чтобы заголовки авторизации были реализованы в React через Apollo Server.

Я следовал некоторым инструкциям здесь -> https://www.apollographql.com/docs/react/networking/authentication/

Это мой код внутри приложения. js

import React from 'react';
import HeroesDota from './components/HeroesDota';


import { ApolloProvider } from '@apollo/react-hooks';

import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { setContext } from 'apollo-link-context';


const httpLink = new HttpLink({
  uri: "http://localhost:8000/graphql/",
});


const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

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 App = () => {
return (
    <ApolloProvider client={client}>
      <HeroesDota />
    </ApolloProvider>
)};

export default App;

После того, как я запускаю сервер на локальном хосте: 3000, он говорит: Uncaught Ошибка: не удается найти модуль 'apollo-link-context'

Я установил apollo-link как зависимость. Я не знаю, почему эта ошибка появляется. Вот фактическое изображение.

enter image description here

упаковка. json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@apollo/react-hooks": "^3.1.3",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.1",
    "apollo-boost": "^0.4.7",
    "apollo-cache-inmemory": "^1.6.5",
    "apollo-link": "^1.2.13",
    "apollo-link-ws": "^1.0.19",
    "bootstrap": "^4.4.1",
    "graphql": "^14.5.8",
    "react": "^16.12.0",
    "react-apollo": "^3.1.3",
    "react-bootstrap": "^1.0.0-beta.16",
    "react-dom": "^16.12.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "styled-components": "^5.0.0",
    "subscriptions-transport-ws": "^0.9.16"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {}
}

1 Ответ

1 голос
/ 04 февраля 2020

Вы импортируете его прямо здесь:

import { setContext } from 'apollo-link-context';

Пожалуйста, запустите npm i apollo-link-context, чтобы исправить.

...