Using Sequelize with GraphQL
The database object is initialized upon starting the server within the root index.js
file. We pass it from this global location down to the spots where we rely on the database. This way, we do not import the database file repeatedly but have a single instance that handles all the database queries for us.
The services that we want to publicize through the GraphQL API need access to our MySQL database. The first step is to implement the posts in our GraphQL API. It should respond with the fake posts from the database we just inserted.
Global database instance
To pass the database down to our GraphQL resolvers, we must create a new object in the server index.js
file:
import db from './database'; const utils = { db, };
Here, we created a utils
object directly under the import
statement of the database
folder.
The utils
object holds all the utilities that our services might need access to. This can be anything from third...