gpt4 book ai didi

node.js - 错误 : Apollo Server requires either an existing schema, 模块或 typeDefs

转载 作者:行者123 更新时间:2023-12-02 10:12:38 25 4
gpt4 key购买 nike

我有一个想要测试的函数,在一个使用 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);

但我有同样的错误

最佳答案

将apollo-server更新到最低版本并在创建apollo服务器时仅传递网关。

const server = new ApolloServer({
gateway
});

关于node.js - 错误 : Apollo Server requires either an existing schema, 模块或 typeDefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com