如何使用续集从graphql中的映射表访问数据

编程入门 行业动态 更新时间:2024-10-12 10:26:35

如何使用<a href=https://www.elefans.com/category/jswz/34/1754605.html style=续集从graphql中的映射表访问数据"/>

如何使用续集从graphql中的映射表访问数据

我是新手,我有一个用户表,地址表和地址类型表,如下所示。user可以有2个不同的address,分别是永久地址和当前地址,并且地址类型(permanent或current)在表address type中指定。

我已经尝试根据架构从解析器中的映射表(address_type)访问数据,并从用户->地址表设置hasMany关系,但是graphql显示未找到关联错误。我们如何正确地获取关系以便获得映射地址类型名称。

        type User{
          id:Int
          name:String             
        }

        type Address {
          id: ID!
          user_id:Int
          city: String
          addr_type:AddressType
        }

        type AddressType{
         id : Int
         name:String (permanent|current)
        }

表定义

            module.exports = function(sequelize, DataTypes) {
            return sequelize.define('user', {
            id: {
                    type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true
                },
                name: {
                    type: DataTypes.INTEGER,  allowNull: false, 
                }, 
            }, {
                tableName: 'user',
                timestamps: false
            });
        };


        module.exports = function(sequelize, DataTypes) {
            return sequelize.define('address', {
            id: {
                    type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true
                },
                user_id: {
                    type: DataTypes.INTEGER,  allowNull: false, field:"addr_type"   
                },
                addr_type: {
                    type: DataTypes.INTEGER,  allowNull: false, field:"addr_type"   
                },
                city: {
                    type: DataTypes.STRING,  allowNull: false,
                }, 

            }, {
                tableName: 'address',
                timestamps: false

            });
        };

        module.exports = function(sequelize, DataTypes) {
            return sequelize.define('address_types', {
            id: {
                    type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true
                },
                name: {
                    type: DataTypes.STRING, allowNull: false, 
                },
            }, {
                tableName: 'address_type',
                timestamps: false

            });
        };

关系

db.user.hasMany(db.address,{foreignKey: 'user_id'});
db.address.belongsTo(db.user,{foreignKey: 'user_id'});
db.address.belongsTo(db.address_types,{foreignKey: 'addr_type'});

解析器代码

            userts: async (obj, args, context, info ) =>    User.findAll( {
                        where: { user_status: 1 },          
                ,
                raw: true,
                nest: true,
        } ).then(userts => {
        const response = userts.map(usert => {                       
        return{
        // i have 15 fields for a user, if i can access the schema of the corresponsing resolver i can dynamically build the response out put

                   id: usert.id,
                   firstName: usert.firstName,
                   lastName: usert.lastName,
                   middleName: usert.middleName,


        }
        })                      
            return response;
        }), 
回答如下:

您应该关闭选项raw以获取关联的对象,并使用include选项指示要加载的关联模型。

User.findAll( {
  where: { user_status: 1 },          
                include: [{ 
                  model: Address,
                  include: AddressType
                }],
                raw: false,
                nest: true,
        }

更多推荐

如何使用续集从graphql中的映射表访问数据

本文发布于:2024-05-07 06:24:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:续集   如何使用   数据   graphql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!