Home > front end >  NodeJS: Pass field value from ResolverQuery to another JS File
NodeJS: Pass field value from ResolverQuery to another JS File

Time:10-25

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

CodePudding user response:

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.

CodePudding user response:

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';
            }
        }
    }
}
  • Related