Sequelize/Apollo returns null on one to many relationship

307 Views Asked by At

I am working on a project with Express/ApolloServer/Sequelize/Postgres. I have two tables, one containing Sheep and the other containing Breeds. Breeds table is just an id and breed_name column. Every Sheep row has a Breed id as a foreign key. When I try to query the breed name for a sheep, the server returns a null value. My TypeDefs:

const typeDefs = gql`
  type Sheep {
    sheep_id: Int!
    tag_id: String!
    scrapie_id: String
    name: String!
    weight_at_birth: Int
    date_deceased: String
    dob: String!
    sex: String!
    purchase_date: String
    father: Sheep
    mother: Sheep
    breed_id: Breed!
    color: Color!
    marking: Marking!
    date_last_bred: String
    created_at: String
    updated_at: String
    dams_children: Sheep
    sires_children: Sheep
  }
  type Breed {
    id: Int!
    breed_name: String!
  }
  
  type Query {
    get_sheep_by_id(sheep_id: Int!): Sheep
    get_sheep_by_tag(tag_id: String!): Sheep
    get_all_sheep: [Sheep!]!
    get_all_breeds: [Breed!]!
  }

My resolvers:

const db = require("./models/index");

const Sheep = db.models.Sheep;
const Breed = db.models.Breed;
const resolvers = {
  Query: {
    get_all_sheep: async () => {
      return await Sheep.findAll({
        include: [
          {
            model: Breed,
          },
          {
            model: Sheep,
            as: "mother",
            include: [{ model: Breed }],
          },
          {
            model: Sheep,
            as: "father",
            include: [{ model: Breed }],
          }
        ],
      });
    },

My associations:

Breed.hasMany(Sheep), { foreignKey: "breed_id" };
Sheep.belongsTo(Breed);
Sheep.belongsTo(Sheep, { foreignKey: "dam", as: "mother" });
Sheep.belongsTo(Sheep, { foreignKey: "sire", as: "father" });
module.exports = db;

When I run the following query i get null in the breed_name, even though the value is correct and I can pull it manually through a postgres query.

query Get_all_sheep {
  get_all_sheep {
    breed_id {
      breed_name
    }
  }
}

I tried adding the following to my resolver but it didn't help:

include: [
          {
            model: Breed,
            as: "breed",
          }, 

I have been searching high and low and can't find a solution that would work. Any pointer int he right direction is appreciated.

EDIT: I actually figured it out, I needed to modify my includes to do {model: Breed, as "breed"} and the issue was resolved. However, now i am encountering a separate issue with the create mutation. When I run the mutation, the object returned contains duplicate foreign keys for breed, color and marking ids lilke so:

sheep {
  dataValues: {
    sheep_id: '40',
    tag_id: 'testMutation3',
    dob: '2022-10-18',
    sex: 'm',
    purchase_date: null,
    breed_id: '3',
    color_id: '3',
    marking_id: '3',
    scrapie_id: null,
    name: 'Bull3',
    weight_at_birth: null,
    date_deceased: null,
    date_last_bred: null,
    updatedAt: 2022-04-29T17:40:28.619Z,
    createdAt: 2022-04-29T17:40:28.619Z,
    sire: null,
    dam: null,
    breedId: 3,
    colorId: 3,
    markingId: 3
  },
1

There are 1 best solutions below

5
On

You need to indicate the same foreignKey option in both associations:

Breed.hasMany(Sheep, { foreignKey: "breed_id" });
Sheep.belongsTo(Breed, { foreignKey: "breed_id" });