azure - 使用 Azure Functions 中的 Apollo Server 连接到多个数据库

标签 azure azure-functions apollo-server

我关注了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/

相关文章:

Apollo 图管理器 406 : Not Acceptable

azure - 重定向 Azure Blob 存储中的基本 URL

Azure 媒体服务价格计算

c# - 如何在 Azure Functions 中设置 HTTP 输出

azure - 在 azure 函数上部署现有的 python FastAPI 项目

express - Sequelize 和 Graphql 反向查找

typescript - 您如何模拟 Apollo Server RESTDataSource 以使用 Jest 进行单元测试?

azure - pod 没有在 kubernetes 中创建,但部署存在?

java - 使用 java sdk 的 Azure 服务总线的 Azure 托管标识

javascript - 如何在azure中导出node.js函数?