如何在Firebase Functions中使用Apollo Server 2

4
我正在学习如何在 Firebase 函数中使用 Apollo GraphQL 服务器,我找到了这个链接,它使用了 Apollo Server 1。 我试图使用 Apollo Server 2,但它没有。
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';

这是文章中作者使用的词语。
我尝试以这种方式实现:
import { https } from "firebase-functions"
import { ApolloServer, gql } from 'apollo-server-express';
import * as express from 'express';

const app = express();

const graphQLServer = new ApolloServer({ typeDefs, resolvers });

graphQLServer.applyMiddleware({ app });

export const api = https.onRequest(app);

使用"apollo-server-express": "^2.0.0"尝试实现,但一直失败,因为每次按照函数链接时都会出现Cannot GET /的错误。
那么,有没有一种方法可以在Firebase/Google Functions中实现Apollo Server 2呢?

有什么运气吗?我在本地运行它,但一旦部署函数,我就会得到相同的错误。 - Rich EBee
@RichEBee,请检查我的答案。 - Pedro Arantes
1个回答

3

当我在applyMiddleware中添加 path: '/' 时,它可以正常工作。

graphQLServer.applyMiddleware({ app, path: '/' });

当以端点// https://us-central1-.cloudfunctions.net/api部署时,我使用此函数。这个Playground在这里可以正常运行。

localhost上,通过http://localhost:5000//us-central1/api。在这里,直到我将历史按钮旁边的URL更改为http://localhost:5000//us-central1/api/,这个Playground才能够正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接