通过GraphQL从MySQL数据库获取空值

时间:2019-06-05 21:18:29

标签: mysql node.js null graphql

我是GraphQL的新手,并且获得了将与MySql数据库连接的唯一代码存储库,但是在键入查询后,我在各个字段(而不是数据)前获取了NULL值。

我已经对结果进行console.log了,数据在终端中显示了值,但是在GraphiQL中却没有显示,我猜我是在以错误的方式执行查询。

这是resolvers.js代码

import {connection} from '../Connectors/mysql';

let getSupplierByCompany = (company) => {
    const sqlAll = `SELECT * FROM supplier`;
    const sqlByCompany = `${sqlAll} WHERE COMPANY = '${company}'`;
    let sql = company ? sqlByCompany : sqlAll;

    return new Promise((resolve, reject) => {
        connection.query(sql, (err, results) => { console.log(results);
            if (err) reject(err); 
            resolve(results); 
        });
    });
};

let getCompany = (name) => {
    return new Promise((resolve, reject) => {
        let sql = `SELECT * FROM Company WHERE NAME = '${name}'`;
        connection.query(sql, (err, results) => { 
            if (err) reject(err);
            resolve(results);
        });
    });
};

export const resolvers = {
    Query: {

        getSupplier: (obj, {company}) => {

             return getSupplierByCompany(company).then(rows => rows);             
            //  return getSupplierByCompany(company).then(rows => rows);
        },
    },

    Supplier: {

        COMPANY: (obj) => {
            return getCompany(obj.COMPANY).then(rows => rows[0]);

        }
    }

};

我正在这样输入查询

query {
  getSupplier (company:"bCompany")
}

这将导致给我这样的值

query {
  getSupplier (company:"bCompany") {
    NAME
    PROFESSION
    PHONE
    EMAIL
    COMPANY
    BUSINESS_NUMBER
  } 
 }

并在右侧给出了类似的内容。

"data": {
    "getSupplier": [
      {
        "NAME": null,
        "PROFESSION": null,
        "PHONE": null,
        "EMAIL": null,
        "COMPANY": null,
        "BUSINESS_NUMBER": null
      }
    ]
  }

console.log(results)的结果是这样的

[ RowDataPacket {
    id: 2,
    name: 'newName',
    profession: 'Proession',
    phone: '000000000',
    email: 'test@test.com',
    company: 'bCompany',
    business_number: '9999999999' } ]

请帮助!

0 个答案:

没有答案