如何在 GraphQL 接口上将模型与 "has one" 关系相关联

How do I associate a model with a "has one" relationship on an GraphQL interface

我有一个 GraphQL/Apollo 服务器使用 Sequelize/mysql。我的 GraphQL 类型(Employee、Contractor)各自实现了一个 Person 接口。我的数据库模型包含雇员、承包商和事件 table。我希望我的人物类型与事件有 "has many" 关系。虽然我的事件类型 "belongs to" 是员工或承包商的人员类型。

我猜它与事件类型中的 person_id 字段有关。我可以在没有接口的情况下在单个 table "Employee" 上工作,并将 person_id 更改为 employee_id。所以我猜它只是不知道如何区分 Employee 和 Contractor 来引用 table?

//typeDefs.js

const typeDefs = gql`
  type Event {
    id: ID!
    person_id: ID!
    location: String!
    escort: String!
  }

  interface Person {
    id: ID!
    first_name: String!
    last_name: String!
    department_company: String!
    events: [Event]
  }

  type Employee implements Person {
    id: ID!
    first_name: String!
    last_name: String!
    department_company: String!
    events: [Event]
    employee_sh_id: String
  }

  type Contractor implements Person {
    id: ID!
    first_name: String!
    last_name: String!
    department_company: String!
    events: [Event]
    escort_required: Boolean!
  }
//Employee model

module.exports = (sequelize, DataTypes) => {
  const Employee = sequelize.define('Employee', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    department_company: DataTypes.STRING,
    emplyee_sh_id: DataTypes.STRING
  }, {});
  Employee.associate = function(models) {
      Employee.hasMany(models.Event);
  };
  return Employee;
};
// Contractor model

module.exports = (sequelize, DataTypes) => {
  const Contractor = sequelize.define('Contractor', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    department_company: DataTypes.STRING,
    escort_required: DataTypes.BOOLEAN,
  }, {});
  Contractor.associate = function(models) {
      Contractor.hasMany(models.Event);
  };
  return Contractor;
};
// Event model

module.exports = (sequelize, DataTypes) => {
  const Event = sequelize.define(
    "Event",
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      person_id: DataTypes.INTEGER,
      location: DataTypes.STRING,
      escort: DataTypes.STRING
    },
    {}
  );
  Event.associate = function(models) {
    Event.belongsTo(models.Employee),
    Event.belongsTo(models.Contractor)
  };
  return Event;
};
// resolvers.js

const resolvers = {
  Query: {
    async employee(root, { id }, { models }) {
      return models.Employee.findByPk(id);
    },
    async contractor(root, { id }, { models }) {
      return models.Contractor.findByPk(id);
    },
    async employees(root, args, { models }) {
      return models.Employee.findAll();
    },
    async contractors(root, args, { models }) {
      return models.Contractor.findAll();
    },
    async event(root, { id }, { models }) {
      return models.Event.findByPk(id);
    },
    async events(root, args, { models }) {
      return models.Event.findAll();
    }
  },

  Mutation: {
    async addEmployee(
      root,
      {
        first_name,
        last_name,
        department_company,
        employee_sh_id
      },
      { models }
    ) {
      return models.Employee.create({
        first_name,
        last_name,
        department_company,
        employee_sh_id
      });
    },

    async addContractor(
      root,
      {
        first_name,
        last_name,
        department_company,
        escort_required,
      },
      { models }
    ) {
      return models.Contractor.create({
        first_name,
        last_name,
        department_company,
        escort_required,
      });
    },

    async addEvent(
      root,
      { person_id, location, escort },
      { models }
    ) {
      return models.Event.create({
        person_id,
        location,
        escort
      });
    },

  Person: {
    __resolveType: person => {
      if (person.employee) {
        return "Employee";
      }
      return "Contractor";
    }
  },

  Employee: {
    events: (parent, args, context, info) => parent.getEvents(),
  },

  Contractor: {
    events: (parent, args, context, info) => parent.getEvents(),
  }
};

你还需要接口吗?

接口和联合等抽象类型背后的主要目的是它们允许特定字段解析为一组类型中的一个。如果您有 ContractorEmployee 类型并希望特定字段为 return 任一类型,则添加接口或联合来处理该场景是有意义的:

type Event {
  contractors: [Contractor!]!
  employees: [Employee!]!
  people: [Person!]! # could be either
}

如果您不需要此功能,则您实际上不需要任何抽象类型。 (旁注:您 可以 使用接口来强制共享字段的类型之间的一致性,但此时您只是将它们用作验证工具及其存在可能不会影响您设计底层数据层的方式)。

没有灵丹妙药

处理关系数据库中的继承 can be tricky 并没有放之四海而皆准的答案。使用 Sequelize 或其他 ORM 时,事情甚至变得很奇怪,因为您的解决方案也必须在该特定库的限制内工作。这里有几种不同的方法可以解决这个问题,但目前还不是一个详尽的列表:

  • 如果您只有几个 return 类型为 Person 的字段,您可以使用单独的 table 和单独的模型并自行合并结果。类似于:
people: async (event) => {
  const [employees, contractors] = await Promise.all([
    event.getEmployees(),
    event.getContractors(),
  ])
  const people = employees.concat(contractors)
  // Sort here if needed
  return people
}

这确实意味着您现在正在查询数据库两次,并且可能会花费额外的时间进行数据库本来会为您完成的排序。但是,这意味着您可以为承包商和雇员维护单独的 table 和模型,这意味着查询这些实体非常简单。

  • 将承包商和雇员集中在一个 table 下,使用某种 type 字段来区分两者。然后,您可以 帮助您在 Sequelize 中适当地建模关系:
Event.hasMany(models.Person, { as: 'employees', scope: {type: 'EMPLOYEE'} ... })
Event.hasMany(models.Person, { as: 'contractors', scope: {type: 'CONTRACTOR'} ... })
Event.hasMany(models.Person, { as: 'people', /** no scope **/ ... })

这很有效,即使它“感觉”不到将所有内容合而为一 table。您必须记住正确确定关联和查询的范围。

  • 如果您将 Sequelize 严格用作 ORM 而不是从您的 Sequelize 模型生成数据库(即不调用 sync),也可以将 view 建模为 Sequelize 模型.编写和维护视图有点麻烦,但这将允许您为员工和承包商保留单独的 table,同时从其他两个创建虚拟 table 可用于查询所有人员.