Search code examples
graphqlsequelize.jsapollo

Cannot pass custom result from resolver to Graphql


I am trying to fetch data with sequelize with an attribute and pass it to graphql. The result is fine in console but the graphql query is returning null for the attribute field.

my resolver

    getUnpayedLessons: async (_, args, { models }) => {
  const { Attendance, Student } = models;
  return await Attendance.findAll({
    include: {
      model: Student,
    },
    where: {
      fk_lessonsSerieId: { [Op.is]: null },
    },
    attributes: ["id", [sequelize.fn("count", sequelize.col("absenceFlag")), "unpayedLessons"]],
    group: ["student.id"],
  });
},

query

getUnpayedLessons {
  id
  unpayedLessons
  student {
    id
    firstName
    lastName
  }
}

schema

type UnpayedLessons {
  id: Int
  unpayedLessons: Int
  student: Student
  }

extend type Query {
  getUnpayedLessons: [UnpayedLessons]
  }

and this is the console.log of the resolver when I run the query

    [
   attendance {
    dataValues: { id: 2, unpayedLessons: 8, student: [student] },
    _previousDataValues: { id: 2, unpayedLessons: 8, student: [student] },
    _changed: Set {},
    _options: {
      isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
     },
]

and from graphql

    {
  "data": {
    "getUnpayedLessons": [
      {
        "id": 2,
        "unpayedLessons": null,
        "student": {
          "id": 2,
          "__typename": "Student"
        },
        "__typename": "UnpayedLessons"
      },
     ]
    }
   }

Any idea how I can have unpayedLessons passed to graphql?


Solution

  • To debug this you need to check what is returned from DB, the shape:

    const values = await Attendance.findAll({...
    console.log( values );
    // adapt structure to match query requirements
    // finally return
    return values;