英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论