Apollo GraphQl 模型属性未访问 ReactJs 中的对象
Apollo GraphQl model attribute is not accessing object in ReactJs
我正在使用 Apollo GraphQl Query 在 ReactJs 中实现类别和子类别显示。
我尝试对字段使用与 类别 相同的 table。
ID,
category_name,
category_img,
category_parent_id(来自同一个 table 的 id),
category_status,
typeDefs 和 resolver 如下
Category.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 中
category.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 相同的 table 获得 children_name .
任何人都可以告诉我问题是什么,我无法访问 children_name 作为来自相同 table 的属性,或者有任何其他方式,以便我们可以访问 category/subcategory相同 table 并将其显示到 reactjs 模板。
未在类型中 [单独] 定义,在父类型中未 used/defined [如 'children' 属性?],未在查询中请求...只是从响应中过滤掉。
我正在使用 Apollo GraphQl Query 在 ReactJs 中实现类别和子类别显示。
我尝试对字段使用与 类别 相同的 table。 ID, category_name, category_img, category_parent_id(来自同一个 table 的 id), category_status,
typeDefs 和 resolver 如下
Category.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 中 category.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 相同的 table 获得 children_name .
任何人都可以告诉我问题是什么,我无法访问 children_name 作为来自相同 table 的属性,或者有任何其他方式,以便我们可以访问 category/subcategory相同 table 并将其显示到 reactjs 模板。
未在类型中 [单独] 定义,在父类型中未 used/defined [如 'children' 属性?],未在查询中请求...只是从响应中过滤掉。