我关注了this toturial使用 Apollo Server 通过 Azure Functions 构建 Azure 静态 Web 应用程序。但是,我现在需要连接到多个数据库(一个是 Cosmos DB,另一个是 Azure blob 存储),并且我只找到了使用 apollo-server-express
与中间件来分配不同路径的示例对于连接到不同数据库的不同 Apollo 服务器。但似乎apollo-server-azure-functions
包不支持中间件。
还有其他方法可以使用 apollo-server-azure-functions 连接到多个数据库吗?谢谢。
我的第一个 Apollo 服务器是
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
import { loadSchemaSync } from "@graphql-tools/load";
import { addResolversToSchema } from "@graphql-tools/schema";
import { ApolloServer } from "apollo-server-azure-functions";
import { join } from "path";
import { cosmosDataSources } from "./data/index";
import resolvers from "./resolvers";
let dataSources = cosmosDataSources
const schema = loadSchemaSync(
join(__dirname, "..", "..", "graphql", "schema.graphql"),
{
loaders: [new GraphQLFileLoader()],
}
);
const server = new ApolloServer({
schema: addResolversToSchema({ schema, resolvers }),
dataSources,
context: {},
});
export default server.createHandler({
cors: {
origin: ['*', "https://studio.apollographql.com"],
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["access-control-allow-credentials", "access-control-allow-origin", "content-type"]
},
});
我尝试为 blob 存储添加另一个连接 this tutorial但不确定应该在哪里添加新服务器以及在哪里启动客户端。
最佳答案
根据 Apollo documentation dataSources
参数是一个函数,它返回一个对象,其中每个键对应于不同的数据源。
const dataSources = () => {
cosmos: cosmosDataSources,
blob: new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
new DefaultAzureCredential()
),
anotherDataSouce: initOtherDS(),
}
在每种情况下,都需要在该语句中初始化数据源。 BlobServiceClient
使用 new BlobServiceClient()
进行初始化。不同的数据源可能会有不同的初始化。
关于azure - 使用 Azure Functions 中的 Apollo Server 连接到多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76194276/