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

183
Vistas
NodeJS: Pass field value from ResolverQuery to another JS File

I would like to know, how can I pass a field value of a QueryResolver to another JS File (database.js) in GraphQL and NodeJS/Apollo. In the database.js file I connect with my MYSQL Database with MariaDB and get dynamically data information. The information should be get by the ID which the user enters in the query. The id is know in my QueryResolver but I don't know how I can pass this ID to my database.js file, so that my user gets the data that he wants.

Instead of the 0 in the code, there should be the ID the user entered.

What I tried: I tried to save the ID in a variable and to return the id AND the articledatas ( these are my dynamic datas the user gets after the query ). And I tried to get access to the function get_result for getting the id, but then I get stuck, because I don't know I could access to this function outside the query. Could anyone help me please?

My resolver looks like this (this is my problem):


module.exports = {
    Query: {
        article: (parent, {id}) => {
                var data = models.articledata.find((c) => c.id == id);
                var id_work = id;

                function get_returns(){return [data, id_work];}
                
                var get_results = get_returns();

                return get_results[0];
        },
    },

    Node: {
        __resolveType(node) {
            if(node.toObject().name){
                return 'Article';
            }
        }
    }
}

This is my data.js (in Models folder):


//This works fine!
var read = function(){
  return new Promise(function(resolve, reject){

    var query_str = 'SELECT * FROM Artikel';
    conn.query(query_str, function (err, rows, fields) {
      if (err) {
        return reject(err);
      }
      resolve(rows)
    });
  });
}

//Here I want to replace the 0 with the {id} of the resolver above->Problem how can I get a reference of the {id} from the resolver?
var title = read().then(function(rows){return rows[0].NAME});
var articledatas = [{
    title: title,
}];
module.exports = { articledatas };
```
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This question is a duplicate of How to return the response from an asynchronous call in most of the time. Please read the answer to the question I refer.

about 4 years ago · Juan Pablo Isaza Denunciar

0

After some tries I figured out when I past the code of my data.js DIRECTLY into the Query, then I don't have to pass the ID outside the Query. This works:

module.exports = {
    Query: {
        article: (parent, {id}) => {
              
var read = function(){
  return new Promise(function(resolve, reject){

    var query_str = 'SELECT * FROM Artikel';
    conn.query(query_str, function (err, rows, fields) {
      if (err) {
        return reject(err);
      }
      resolve(rows)
    });
  });
}

var title = read().then(function(rows){return rows[id].NAME});
var articledatas = [{
    title: title,
}];  

         return = articledata.find((c) => c.id == id);

        },
    },

    Node: {
        __resolveType(node) {
            if(node.toObject().name){
                return 'Article';
            }
        }
    }
}
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