【问题标题】:Error: Apollo Server requires either an existing schema, modules or typeDefs错误:Apollo Server 需要现有的模式、模块或 typeDefs
【发布时间】:2020-05-14 20:55:58
【问题描述】:

我有我想测试的这个功能,在一个使用 Apollo Server 的联合网关实现的 nodeJS 项目中。

@Service()
export class Server {


    constructor();
    }

    async startAsync(): Promise<void> {
        await this.createApolloGateway();
    }

    private async createApolloGateway(): Promise<void> {

        const gateway = new ApolloGateway({
                serviceList: [{ name: 'products', url: 'https://products-service.dev/graphql' },
                { name: 'reviews', url: 'https://reviews-service.dev/graphql' }]});


        const {schema, executor} = await gateway.load();
        const server = new ApolloServer({schema, executor});

        return new Promise((resolve, _) => {
            server.listen(8080).then(({url}) => {
                resolve();
            });
        });
    }

}

但是当我测试这个函数时我有这个错误:

Error: Apollo Server requires either an existing schema, modules or typeDefs

我很乐意在 jest 框架中模拟这种模式

 apolloGateway = createMockInstance(ApolloGateway);

 it('should start an http server', async () => {
        // Arrange

        const server = new Server(configurationService, loggerService);

        const schema = `
  type User {
    id: ID!
    name: String
    lists: [List]
  }

  type RootQuery {
    user(id: ID): User
  }
  schema {
    query: RootQuery
  }
`;

         apolloGateway.load = jest.fn().mockReturnValue(schema);




        // Act
        await server.startAsync();

        // Assert
        await expect(server).not.toBeNull;
    }, 30000);

但我有同样的错误

【问题讨论】:

    标签: node.js typescript jestjs apollo apollo-server


    【解决方案1】:

    将 apollo-server 更新到最低版本,创建 apollo 服务器时只通过网关。

    const server = new ApolloServer({
      gateway  
    }); 
    
    

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 2019-04-22
      • 1970-01-01
      • 2011-10-09
      • 2017-01-27
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      相关资源
      最近更新 更多