jeudi 15 avril 2021

Adding an Express.router() REST endpoint in Apollo Server backend? (for e2e tests cleanup)

Is it possible to add an extra endpoint in an Apollo Server backend (besides the existing /graphql endpoint) using Express.router() or some built in Apollo function?

I have tried to add something like this but the route does not seem to be working:

// controllers/testing
import express from 'express';
import resetDatabase from '../database/resetDB';

const router = express.Router();

router.post('/reset', async (_request, response) => {
  await resetDatabase();    
  response.status(204).end();
});

export default router;

And then add the route in index where Apollo Server is created:

// index.ts
import testingRouter from './controllers/testing';

// ...

  if (process.env.NODE_ENV === 'test') {
    app.use('/api/testing', testingRouter);
  }

For the record, I am trying to do this to provide my Cypress e2e tests in an Apollo Client frontend with an endpoint to reset a testing database after running tests, if there is a better way of doing this, feel free to disregard my question and point me to the righteous path =)

Aucun commentaire:

Enregistrer un commentaire