taro开发小程序,想用apollo-client 的 WebSocketLink,一直报module "npm/ws/index.js" is not defined



  • Object.defineProperty(exports, "__esModule", { value: true });
    var WebSocket = require("../../ws/index.js"); // 加载不到websocket
    var message_types_1 = require("./message-types.js");
    var protocol_1 = require("./protocol.js");
    var is_object_1 = require("./utils/is-object.js");
    var graphql_1 = require("../../graphql/index.js");
    var empty_iterable_1 = require("./utils/empty-iterable.js");
    var iterall_1 = require("../../iterall/index.js");
    var is_subscriptions_1 = require("./utils/is-subscriptions.js");
    var parse_legacy_protocol_1 = require("./legacy/parse-legacy-protocol.js");
    var isWebSocketServer = function (socket) {
    return socket.on;
    };

    下面是项目代码
    ....
    const wsLink = new WebSocketLink({
    uri: ws://localhost:5000/,
    options: {
    reconnect: false,
    // reconnect: true
    connectionParams: async () => {
    const { accessToken: token } = await getItem(keys.auth);
    return { token };
    }
    }
    });

    ....

    const link = ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
    if (graphQLErrors) {
    graphQLErrors.map(({ message, locations, path }) =>
    console.log(
    [GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}
    )
    );
    }
    if (networkError)
    console.error([Network error]: ${networkError}, networkError.stack);
    }),
    authMiddleware,
    // 小程序不支持websocket 需要用Taro.connectSocket
    ApolloLink.split(
    ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === "OperationDefinition" && operation === "subscription";
    },
    wsLink,
    httpLink
    )
    ]);



  • @woshiji_111taro开发小程序,想用apollo-client 的 WebSocketLink,一直报module "npm/ws/index.js" is not defined 中说:

    const wsLink = new WebSocketLink({
    uri: ws://localhost:5000/,
    options: {
    reconnect: false,
    // reconnect: true
    connectionParams: async () => {
    const { accessToken: token } = await getItem(keys.auth);
    return { token };
    }
    }
    });
    ....
    const link = ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
    if (graphQLErrors) {
    graphQLErrors.map(({ message, locations, path }) =>
    console.log(
    [GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}
    )
    );
    }
    if (networkError)

    是因为微信小程序环境的node模块缺少 ws ? https://github.com/websockets/ws


登录后回复