'What's the right way to declare an object in Node.js router

I'm trying to solve memory leaks in my Node.js app and seems this code does leak

const ApolloClient = require('apollo-client').ApolloClient;
const fetch = require('node-fetch');
const createHttpLink = require('apollo-link-http').createHttpLink;
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;

const httpLink = createHttpLink({
  uri: 'http://xxxxxx',
  fetch: fetch
});

const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
});


module.exports = (app) => {
  app.post('/graphql', async (req, res, next) => {
    try {
        const data = await client.query({
            query: 'xxxxxxx',
            variables: 'xxxxxxx'
        });
        return res.json(data);
    } catch (err) {
      return next('error');
    }
  });
};

so ApolloClient client recreates every time since it's a global. Is it better to define it inside the route? Won't it cause performance issues then?

const ApolloClient = require('apollo-client').ApolloClient;
const fetch = require('node-fetch');
const createHttpLink = require('apollo-link-http').createHttpLink;
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;

module.exports = (app) => {
  app.post('/graphql', async (req, res, next) => {
    try {
        let httpLink = createHttpLink({
            uri: 'http://xxxxxx',
            fetch: fetch
          });
          
          let client = new ApolloClient({
            link: httpLink,
            cache: new InMemoryCache()
          });

        const data = await client.query({
            query: 'xxxxxxx',
            variables: 'xxxxxxx'
        });

        httpLink = null
        client = null
        
        return res.json(data);
    } catch (err) {
      return next('error');
    }
  });
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source