node postgres dataloader for apollo server
npm install pg-datasourcenpm i pg-datasource
js
// pgDB.js
const { PgDataSource } = require('pg-datasource')
class PgDB extends PgDataSource {
async helloWorld() {
return await this.db.query('SELECT NOW() as now')
}
}
module.exports = PgDB;
`
And use it in your Apollo server configuration:
`js
// index.js
const PgDB = require("./PgDB");
const db = new PgDB(pgConnection);
// you can pass a Postgres.js instance or other db instance instead of a configuration object
const server = new ApolloServer({
typeDefs,
resolvers,
cache,
context,
dataSources: () => ({ db })
});
``