Apollo GraphQl 模型属性未访问 ReactJs 中的对象



我正在使用Apollo GraphQl Query在ReactJS中实现类别和子类别显示。

我尝试使用与字段相同的表作为类别。 id, category_name, category_img, category_parent_id ( ID 来自同一表(, category_status,

typeDefs 和解析器如下

类别.js

const typeDefs = gql`
extend type Query {
getSingleCategory(id: ID): allCategory
}
`;
type allCategory {
id: ID!
category_name: String
category_img: String
category_parent_id: Int
category_status: Status
}
const resolvers = {
Query: {
getSingleCategory: async (parent, args, context, info) => {
var data = await db.category.findOne({
where: {
id: args.id,
},
include: [
{
model: db.category,
as: "children",
attributes: [["category_name", "children_name"]],
nested: true,
required: false,
},
],
required: false,
});
return data;
},
},
},

GraphQl 中的模型

module.exports = function (sequelize, DataTypes) {
var category = sequelize.define(
"category",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
category_name: {
type: DataTypes.STRING(256),
allowNull: false,
},
category_img: {
type: DataTypes.STRING(256),
allowNull: false,
},
category_parent_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
// WorkingDays hasMany Users n:n
model: "category",
key: "children",
},
},
category_status: {
type: DataTypes.ENUM("Acitve", "Inactive"),
allowNull: false,
},
},
{
tableName: "category",
timestamps: false,
}
);
category.associate = function (models) {
models.category.belongsTo(models.category, {
onDelete: "CASCADE",
foreignKey: "category_parent_id",
as: "children",
targetKey: "id",
});
};
return category;
};

在 ReactJs类别中.ts

export const GET_CATEGORYBY_ID = gql`
query($catId: ID!) {
getSingleCategory(id: $catId) {
id
category_name
category_img
category_parent_id
category_status
}
}
`;

我正在尝试访问 {data.getSingleCategory} ,我得到了所有参数,但无法从与parent_name相同的表中获取children_name。

任何人都可以告诉我我的问题是什么,我无法从同一个表中访问该children_name作为属性,或者有任何其他方法,以便我们可以从同一表中访问类别/子类别并将其显示到 reactjs 模板。

未在类型中[单独]定义,未在父类型中使用/定义[作为'子'属性?只是从响应中过滤掉。

最新更新