Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!