Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda