Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to multiple database with GraphQL

Tags:

graphql

We are building new GraphQL api using apollo server 2. Most of our data live in MongoDB and we are able to access easily with mongoose. But there are many data points which exists in our mySQL database. How can we connect to multiple database in GraphQL API to connect to mongodb and mySQL ?

like image 661
jvm Avatar asked Oct 12 '25 11:10

jvm


1 Answers

Apollo server is not concerned with how many databases you use, so you should be able to setup as many database connections as your node process allows.

You will have to define resolving logic, probably utilizing dataloaders, where some fields are fetching data from the SQL database and some fields fetch data from the mongoDB.

Another strategy you might want to consider is to create separate (micro)services for these databases. Your GraphQL server would then fetch its data from these services, and dropping the database handling altogether.

like image 95
peternyc Avatar answered Oct 14 '25 02:10

peternyc