连接到多个数据库与Apollo Server在Azure Functions中。

huangapple go评论91阅读模式
英文:

Connecting to Multiple Databases with Apollo Server in Azure Functions

问题

I followed this toturial to build an Azure static web app with Azure Functions using Apollo Server. However, I now need to connect to multiple databases (one is Cosmos DB and the other one is Azure blob storage), and I have only found examples of using apollo-server-express with middleware to assign different paths for different Apollo servers that connect to different databases. But it seems that the apollo-server-azure-functions package does not support middleware.

Is there any other way to connect to multiple databases using apollo-server-azure-functions? Thanks.

My first Apollo server is


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"]
  },
});

and I tried to add another connection for blob storage by following this tutorial but not sure where should I add a new server and where to start a client.

英文:

I followed this toturial to build an Azure static web app with Azure Functions using Apollo Server. However, I now need to connect to multiple databases(one is Cosmos DB and other one is Azure blob storage), and I have only found examples of using apollo-server-express with middleware to assign different paths for different Apollo servers that connect to different databases. But it seems that the apollo-server-azure-functions package does not support middleware.

Is there any other way to connect to multiple databases using apollo-server-azure-functions? Thanks.

My first Apollo server is


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"]
  },
});

and I tried to add another connetion for blob storage by followed this tutorial but not sure where should I add a new server and where to start a client.

答案1

得分: 1

根据Apollo文档dataSources参数是一个返回对象的函数,其中每个键对应不同的数据源。

const dataSources = () => {
  cosmos: cosmosDataSources,
  blob: new BlobServiceClient(
    `https://${accountName}.blob.core.windows.net`,
    new DefaultAzureCredential()
    ),
  anotherDataSouce: initOtherDS(),
}

在每种情况下,数据源需要在该语句中进行初始化。BlobServiceClient使用new BlobServiceClient()进行初始化。不同的数据源可能以不同的方式进行初始化。

英文:

As per the Apollo documentation the dataSources parameter is a function that returns an object with each key corresponding to a different data source.

const dataSources = () => {
cosmos: cosmosDataSources,
blob: new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
new DefaultAzureCredential()
),
anotherDataSouce: initOtherDS(),
}

In each case the data source needs to be initialized in that statement. The BlobServiceClient is initialized with new BlobServiceClient(). Different data sources may be initialized differently.

huangapple
  • 本文由 发表于 2023年5月7日 21:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定