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')
Fernando
  • 349
  • 2
  • 6
  • 21

1 Answers1

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.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 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 Jan 31 '20 at 15:48
  • @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](https://stackoverflow.com/questions/56319137/why-does-a-graphql-query-return-null) – Daniel Rearden Jan 31 '20 at 16:35
  • I have edited the question by adding the code and order of execution of the code with promises. – Fernando Feb 03 '20 at 15:22