1

I am trying to connect a Node.js and express web service with my SQLServer database.

I have been testing and they return the correct values ​​to the database, but the problem is when I try to get the information from Graphql and convert it to JSON

The JSON returns me the structure that I need correctly, but with all the data with null value.

This is the database configuration:

const sql = require('mssql');

const config = {
user: 'user',
password: 'password',
server: 'server',
database: 'db',
options: {
  encrypt: true
}
};

This is the Method that is called by Graphql:

let getEmpleado = (id) => {
let datos = sql.connect(config, function(err){
   if (err) console.log(err);   

   let sqlQuery = 'SELECT Empleado, Nombre, Apellido1, Apellido2 FROM dbo.Empleados WHERE Empleado=' + id.id;
   let sqlRequest = new sql.Request();   

//console.log(sqlQuery);

    let datos1 = sqlRequest.query(sqlQuery, function(err, empleado){
        let datos2;
        if (err) console.log(err)
        datos = JSON.stringify(empleado.recordset[0]);
        sql.close();  
        console.log(datos);
    //return datos2; 
   });
   //return datos1;
  });
 return datos;
}

Result:

{
"data": {
  "empleado": {
  "Empleado": null,
  "Nombre": null,
  "Apellido1": null,
  "Apellido2": null
  }
 }
}

I'm quite new with this and I need some help because I've gone crazy looking for information.

Edit 1:

After debugging the function, I have seen that it is being executed in an order that is not logical to me.

I put the code again with a number indicating the steps it does.

let getEmpleado = (id) => {
 1- let datos = sql.connect(config, function(err){
   3- if (err) console.log(err);   

   4- let sqlQuery = 'SELECT Empleado, Nombre, Apellido1, Apellido2 FROM dbo.Empleados WHERE Empleado=' + id.id;
   5- let sqlRequest = new sql.Request();   

//console.log(sqlQuery);

    6- let datos1 = sqlRequest.query(sqlQuery, function(err, empleado){
        8- let datos2;
        9- if (err) console.log(err)
        10- datos = JSON.stringify(empleado.recordset[0]);
        11- sql.close();  
        12- console.log(datos);
    13- //return datos2; 
   });
   7- //return datos1;
  });
 2- return datos;
}

Edit 2:

As Daniel sugested i check the mssql documentation and i use the promise Queries Example: Promises Queries Example.

After debugging, again some strange order executing the functions.

1- const sql = require('mssql')

2- sql.on('error', err => {
  // ... error handler
})

3- sql.connect(config).then(pool => {
// Query

6- return pool.request()
    .input('input_parameter', sql.Int, id.id)
    .query('SELECT Empleado, Nombre, Apellido1, Apellido2 FROM dbo.Empleados WHERE Empleado= @input_parameter')
4- }).then(result => {
    7- console.dir(result)
    8- return result.recordset[0]
}).catch(err => {
    // ... error checks
});

5- console.log('End')
7
  • 1
    and console logs good data? Commented Jan 31, 2020 at 14:12
  • Yes, the console print the data correctly. When i debug The function, ir seems that return the value before connecting to the database Commented Jan 31, 2020 at 14:15
  • use async await ? Commented Jan 31, 2020 at 14:15
  • No, as i said, i am new with JavaScript and i don't know exactly what os happening. Commented Jan 31, 2020 at 14:19
  • Added some additional details to the answer. Please check the question I marked as a dupe for a more extensive explanation. Commented Jan 31, 2020 at 14:27

1 Answer 1

0

There's no need to use stringify. Your resolver should return a plain Object with properties that match the fields of the return type. In other words, it should be sufficient to return empleado.recordset[0].

Every resolver receives the value their parent field resolved to as the first parameter. If you don't provide a customer resolver, the default behavior in GraphQL.js is to try to return the property on the parent value with the same name as the field. In other words, the default resolution behavior for the Nombre field is to return parent.Nombre. If the parent field's resolver returns { Nombre: 'Fernando' }, then the Nombre field will resolve to "Fernando". If the parent field's resolver returns a string, Nombre will resolve to null because strings don't have a property named Nombre.

Sign up to request clarification or add additional context in comments.

3 Comments

i As you have suggested, I have changed the code and put in the return only employee.recordset [0], but I still have the same problem. I have added additional information about the order the function is executed.
@Fernando you also need to avoid using callbacks when working with GraphQL. You can use Promises with the mssql library -- check their documentation. Also see Common Scenario #6
I have edited the question by adding the code and order of execution of the code with promises.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.