ReferenceError:进程未定义模块.../../../node_modules/graphql/jsutils/instanceOf.mjs

160*_*Tâm 5 javascript websocket express web graphql

我在我的项目中使用 graphql 订阅,它是一个 Instagram 克隆,我正在做喜欢或不喜欢帖子的功能,我用它npx create-react-app来创建我的客户端,我用它express来制作我的服务器。

client在客户端,我这样设置:

src/apis/client.js

import {
  ApolloClient,
  ApolloLink,
  HttpLink,
  InMemoryCache,
  split,
} from 'apollo-boost';
import {getMainDefinition} from '@apollo/client/utilities';
import {WebSocketLink} from '@apollo/client/link/ws';
const httpUrl = 'http://localhost:5000/graphql';
const wsUrl = 'ws://localhost:5000/graphql';

const httpLink = ApolloLink.from([
  new ApolloLink((operation, forward) => {}),
  new HttpLink({uri: httpUrl}),
]);

const wsLink = new WebSocketLink({
  uri: wsUrl,
  options: {
    // connectionParams: () => {},
    lazy: true,
    reconnect: true,
  },
});

function isSubscription(operation) {
  const definition = getMainDefinition(operation.query);
  return (
    definition.kind === 'OperationDefinition' &&
    definition.operation === 'subscription'
  );
}

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: split(isSubscription, wsLink, httpLink),
  defaultOptions: {query: {fetchPolicy: 'no-cache'}},
});

export default client;
Run Code Online (Sandbox Code Playgroud)

如您所见,我同时使用 websocket 和 https 连接。以前,我只使用http连接,一切都很完美。

这是我的server一面:

app.js

const {ApolloServer, gql} = require('apollo-server-express');
const http = require('http');
const fs = require('fs');
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');

const app = express();
app.use(cors(), bodyParser.json());

const typeDefs = gql(fs.readFileSync('./schema.graphql', {encoding: 'utf8'}));

const resolvers = require('./resolvers');

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
});
apolloServer.applyMiddleware({app, path: '/graphql'});

const httpServer = http.createServer(app);
apolloServer.installSubscriptionHandlers(httpServer);
const port = 5000;
httpServer.listen(port, () => console.log(`Server started on port ${port}`));
Run Code Online (Sandbox Code Playgroud)

这是错误:

在此输入图像描述

我尝试谷歌,但看起来 React 的 webpack.config 文件中缺少一些东西,但我不知道 webpack。请告诉我如何处理,非常感谢,祝你有美好的一天

小智 1

安装特定软件包的最新版本后,我遇到了同样的问题。能够通过删除node_modules并再次package-lock.json运行来解决该问题npm install