Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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');
    }
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Creating one ApolloClient instance and reusing it is better for many reasons.

  1. Minimum number of instances.

Consider having 20 or more (N) endpoints/middlewares that needs to access some data that is stored in your database. Creating an ApolloClient instance for each one would mean N number of instances. This is not performant and violates and violates the 'Do not repeat yourself' coding rule.

On the other hand creating one instance and using it throughout the app is very handy. Consider this example:

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
});

// instantiating
module.exports = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
});

a module:

const dbClient = require('./db_client')

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

another module:

const dbClient = require('./db_client')
...

another module:

const dbClient = require('./db_client')
...

As can be seen, with just one instance we can achieve a more robust and performant application.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda