需要通过使用架构拼接连接订阅来查找错误

问题描述 投票:0回答:1

我正在使用apollo-server-express for graphql后端。我将只处理那里的突变,但我想通过内省的模式拼接重定向hasura上的查询和订阅。通过apollo-server到hasura的查询工作正常并返回预期的数据。

但订阅不起作用,我收到此错误:“预期Iterable,但没有找到一个字段subscription_root.users”。

此外,服务器hasura正在接收事件:

但阿波罗服务器对hasura的答案表示不满。这不是我受苦的第一天,我无法理解问题所在。

在编辑器hasura订阅工作。

Link to full code

如果您需要任何其他信息,我很乐意为您提供。

import {
  introspectSchema,
  makeExecutableSchema,
  makeRemoteExecutableSchema,
  mergeSchemas,
  transformSchema,
  FilterRootFields
} from 'graphql-tools';
import { HttpLink } from 'apollo-link-http';
import nodeFetch from 'node-fetch';
import { resolvers } from './resolvers';
import { hasRoleResolver } from './directives';
import { typeDefs } from './types';
import { WebSocketLink } from 'apollo-link-ws';
import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import * as ws from 'ws';
import { OperationTypeNode } from 'graphql';

interface IDefinitionsParams {
  operation?: OperationTypeNode,
  kind: 'OperationDefinition' | 'FragmentDefinition'
}

const wsurl = 'ws://graphql-engine:8080/v1alpha1/graphql';

const getWsClient = function (wsurl: string) {
  const client = new SubscriptionClient(wsurl, {
    reconnect: true,
    lazy: true
  }, ws);
  return client;
};

const wsLink = new WebSocketLink(getWsClient(wsurl));

const createRemoteSchema = async () => {
  const httpLink = new HttpLink({
    uri: 'http://graphql-engine:8080/v1alpha1/graphql',
    fetch: (nodeFetch as any)
  });

  const link = split(
    ({ query }) => {
      const { kind, operation }: IDefinitionsParams = getMainDefinition(query);
      console.log('kind = ', kind, 'operation = ', operation);
      return kind === 'OperationDefinition' && operation === 'subscription';
    },
    wsLink,
    httpLink,
  );

  const remoteSchema = await introspectSchema(link);
  const remoteExecutableSchema = makeRemoteExecutableSchema({
    link,
    schema: remoteSchema
  });

  const renamedSchema = transformSchema(
    remoteExecutableSchema,
    [
      new FilterRootFields((operation, fieldName) => {
        return (operation === 'Mutation') ? false : true; //  && fieldName === 'password'
      })
    ]
  );
  return renamedSchema;
};

export const createNewSchema = async () => {
  const hasuraExecutableSchema = await createRemoteSchema();
  const apolloSchema = makeExecutableSchema({
    typeDefs,
    resolvers,
    directiveResolvers: {
      hasRole: hasRoleResolver
    }
  });
  return mergeSchemas({
    schemas: [
      hasuraExecutableSchema,
      apolloSchema
    ]
  });
};
node.js graphql graphql-js apollo-server hasura
1个回答
0
投票

通过安装graphql-tools第4版修复。它告诉编辑器甚至没有注意到我没有这种依赖,只是采用了由其他一些软件包安装的node_modules版本。问题出在版本3.x. Pull request是修复bug的地方。

© www.soinside.com 2019 - 2024. All rights reserved.