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.
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.