首页 > 解决方案 > Graphql 连接错误 - 仅获取查询或订阅或两者

问题描述

我们在将应用程序设置到 graphql 连接时遇到错误。我们的主要需求是订阅,目前我们的代码可以很好地处理这些。但是我们的查询以“在 query_root 中找不到潜在客户”作为响应

当我们修改连接代码的位置时,查询现在可以工作,但订阅停止连接。

const scheme = proto => {
    return window.location.protocol === 'https:' ? `${proto}s` : proto;
};

const wsurl = `${scheme('ws')}://${HASURA_GRAPHQL_ENGINE_HOSTNAME}/v1/graphql`;
const httpurl = `${scheme('http')}://${HASURA_GRAPHQL_ENGINE_HOSTNAME}/v1/graphql`;

// Create a WebSocket link:
const wsLink = new WebSocketLink({
    uri: wsurl,
    options: {
        reconnect: true,
        connectionParams: {
            headers: {
                'x-hasura-access-key': `${HASURA_ACCESS_KEY}`
            }
        }
    }
});

const httpLink = new HttpLink({
    uri: httpurl,
    options: {
        connectionParams: {
            headers: {
                'x-hasura-access-key': `${HASURA_ACCESS_KEY}`
            }
        }
    }
});

const link = split(
    // split based on operation type
    ({ query }) => {
        const { kind, operation } = getMainDefinition(query);
        return kind === 'OperationDefinition' && operation === 'subscription';
    },
    wsLink,
    httpLink
);

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

export default client;

在链接 const 中,如果我们将 wsLink 放在 httpLink 下方,则 wsLink 停止并且 httpLink 工作。

标签: node.jsreactjsgraphqlhasura

解决方案


推荐阅读