使用graphql和sequelize检索数据的问题

时间:2019-03-28 22:02:44

标签: node.js graphql sequelize.js

我在使用graphql进行序列化和检索数据时联接表时遇到问题。

首先,这是我的代码:

schema.js

import { gql } from 'apollo-server-koa';
export default gql`
    type Person {
        id: ID!,
        lastName: String!,
        gender: String!
    }
    type Company {
        id: ID!,
        contactPerson: String!,
        website: String
    }
    type Lead {
        id: ID!
        name: String!,
        address: String,
        phone: String!,
        email: String!,
        companyId: ID,
        company: Company,
        personId: ID,
        person: Person,
        category: String!
    }
    type Query {
        leads: [Lead!]!
    }
`;

lead.js

module.exports = (sequelize, DataTypes) => {
    const Lead = sequelize.define('Leads', {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        name: DataTypes.STRING,
        address: DataTypes.STRING,
        phone: DataTypes.STRING,
        email: DataTypes.STRING,
        company_id: DataTypes.INTEGER,
        person_id: DataTypes.INTEGER,
        category: DataTypes.STRING
      },
      {
        freezeTableName: true,
        timestamps: false
      }
    );

    Lead.associate = (models) => {
        Lead.hasOne(models.Persons);
        Lead.hasMany(models.Companies);
    };

    return Lead;
}

person.js

module.exports = (sequelize, DataTypes) => {
    const Person = sequelize.define('Persons', {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        last_name: DataTypes.STRING,
        gender: DataTypes.STRING
      },
      {
        freezeTableName: true,
        timestamps: false
      }
    );

    Person.associate = (models) => {
        Person.belongsTo(models.Leads);
    };

    return Person;
}

company.js

module.exports = (sequelize, DataTypes) => {
    const Company = sequelize.define('Companies', {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        contact_person: DataTypes.STRING,
        website: DataTypes.STRING
      },
      {
        freezeTableName: true,
        timestamps: false
      }
    );

    Company.associate = (models) => {
        Company.belongsTo(models.Leads);
    };

    return Company;
}

resolvers.js

export default {
    Query: {
        leads: (parent, args, { db }, info) => {
            return db.Leads.findAll(
                {
                    include: [
                        {model: db.Persons}, 
                        {model: db.Companies}
                    ]
                }
            )
        }
    }
}

当我尝试获取数据时,出现此错误:

"sqlMessage": "Unknown column 'Person.LeadId' in 'field list'",

我试图通过添加外键来解决此问题,例如:

Lead.associate = (models) => {
     Lead.hasOne(models.Persons, {foreignKey: 'id'});
     Lead.hasMany(models.Companies, {foreignKey: 'id'});
};

Person.associate = (models) => {
    Person.belongsTo(models.Leads, {foreignKey: 'id'});
};

Company.associate = (models) => {
    Company.belongsTo(models.Leads, {foreignKey: 'id'});
};

并且错误已被删除,但我没有从联接表中获取任何数据,而是得到了空值:

"data": {
    "leads": [
      {
        "id": "1",
        "name": "Grant - Weber",
        "address": "611 Maurine Lane Suite 828",
        "phone": "1-315-477-8900",
        "email": "Grant-Weber.Schimmel12@gmail.com",
        "companyId": null,
        "company": null,
        "personId": null,
        "person": null,
        "category": "new"
      },
      {
        "id": "2",
        "name": "Jevon",
        "address": "88329 Hermiston Summit Suite 558",
        "phone": "1-825-314-1844",
        "email": "Jevon.Considine60@gmail.com",
        "companyId": null,
        "company": null,
        "personId": null,
        "person": null,
        "category": "new"
      }
   ]
}

如果我尝试使用其他任何键,都会得到与以前相同的错误。

我不知道是什么问题。

请帮助!

0 个答案:

没有答案