Предельная глубина для рекурсивного запроса схемы GraphQL с использованием решателя graphql-sequelize (Node.js, express -graphql) - PullRequest
0 голосов
/ 24 января 2020

У меня есть 2 Модели, Пользователь и Пост. Я хочу иметь возможность получать информацию о пользователе при запросе к сообщению и иметь возможность получать все сообщения пользователя при запросе пользователя.

они связаны следующим образом:

User.hasMany(Post, {
    foreignKey: 'user',
    as: 'posts'
});
Post.belongsTo(User, {
    foreignKey: 'id',
    sourceKey: 'user',
    as: 'userObject'
})

Post.addScope('defaultScope', {
    include: [{ model: User, as: 'userObject' }],
}, { override: true })

User.addScope('defaultScope', {
    include: [{ model: Post, as: 'posts' }],
}, { override: true })

Вот мои модели

Пользователь. js

module.exports.userType = new GQL.GraphQLObjectType({
    name: 'User',
    fields: () => {
        const { postType } = require('../Post/Post');
        return {
            id: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'user unique id'
            },
            ci_username: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                unique: true,
                description: 'case INSENSITIVE username of the user'
            },
            username: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'case SENSITIVE username of the user'
            },
            password: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'password for the user'
            },
            first_name: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'first name of user'
            },
            last_name: {
                type: GQL.GraphQLString,
                description: 'last name of user (optional)'
            },
            profile_picture: {
                type: GQL.GraphQLString,
                description: 'profile picture for the user'
            },
            posts: {
                type: GQL.GraphQLList(postType),
                description: 'list of users posts'
            }
        }
    },
})

/** define User model for the database */
module.exports.User = db.define('user', {
    id: {
        type: Sequelize.UUID,
        primaryKey: true, 
        unique: true,
    },
    ci_username: {
        type: Sequelize.STRING,
        unique: true,
    },
    username: Sequelize.STRING,
    password: Sequelize.STRING,
    first_name: Sequelize.STRING,
    last_name: Sequelize.STRING,
    profile_picture: Sequelize.STRING,
}, {
    // Tells sequelize not to query the "CreatedAt" or "UpdatedAt" Columns
    timestamps: false
})

Пост. js

module.exports.postType = new GQL.GraphQLObjectType({
    name: 'Post',
    fields: () => {
        const { userType } = require('../User/User');
        return {
            id: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'post unique id'
            },
            name: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'name of the post'
            },
            user: {
                type: userType,
                description: 'user object of who created the post'
            },
            created_at: {
                type: new GQL.GraphQLNonNull(GQL.GraphQLString),
                description: 'the datetime the post was created',
            }
        }
    },
})

/** define User model for the database */
module.exports.Post = db.define('post', {
    id: {
        type: DataTypes.UUID,
        primaryKey: true, 
        unique: true,
    },
    name: DataTypes.STRING,
    user: {
        type: DataTypes.STRING,
        references: {
            model: 'users',
            key: 'id'
        }
    },
    created_at: {
        type: DataTypes.TIME,
        defaultValue: DataTypes.NOW
    }
}, {
    // Tells sequelize not to query the "CreatedAt" or "UpdatedAt" Columns
    timestamps: false
})

Вот мои запросы:

allUsers. js

const allUsers = {
    type: new GQL.GraphQLList(userType),
    args: {
        username: {
            description: 'username of the user',
            type: GQL.GraphQLString,
        },
        // An arg with the key limit will automatically be converted to a limit on the target
        limit: {
            type: GQL.GraphQLInt,
            default: 10
        },
        // An arg with the key order will automatically be converted to a order on the target
        order: {
            type: GQL.GraphQLString
        }
    },
    // use graphql-sequelize resolver with the User model from database
    resolve: resolver(User)
}

allPosts. js

const allPosts = {
    type: new GQL.GraphQLList(postType),
    args: {
        username: {
            description: 'username of the user',
            type: GQL.GraphQLString,
        },
        // An arg with the key limit will automatically be converted to a limit on the target
        limit: {
            type: GQL.GraphQLInt,
            default: 10
        },
        // An arg with the key order will automatically be converted to a order on the target
        order: {
            type: GQL.GraphQLString
        }
    },
    // use graphql-sequelize resolver with the Post model from database
    resolve: resolver(Post)
}

В настоящее время я получаю Maximum call stack size exceeded. Я предполагаю, что resolver в запросах рекурсивно получает информацию о сообщениях и пользователях бесконечно.

Кто-нибудь знает какой-нибудь способ установить ограничение глубины для резольвера? Или просто невозможно получить рекурсивный запрос, подобный этому?

1 Ответ

1 голос
/ 24 января 2020

Вы должны удалить область по умолчанию из включенной модели, как показано здесь следующим образом:

Post.addScope('defaultScope', {
    include: [{ model: User.scope(null), as: 'userObject' }],
}, { override: true })

User.addScope('defaultScope', {
    include: [{ model: Post.scope(null), as: 'posts' }],
}, { override: true })

Для поддержки дополнительной глубины вам потребуется реализовать средства распознавания для соответствующие поля, например:

function resolve (user) {
  if (user.posts) {
    return user.posts
  }
  return user.getPosts()
}
...