laravel-graphql滤波器查询

发布于 2025-02-11 11:52:31 字数 1921 浏览 1 评论 0 原文

如何定义仅显示发布帖子的帖子查询? 我不想在情况下使用这样的情况

 posts(@where(operator: "=", "publish", "1")): [Post!]!  @paginate

我希望服务器帖子的查询仅在默认情况下仅发布发布的帖子

客户端:

$: posts = queryStore({
    client: getContextClient(),
    query: gql`
        query ($first: Int!, $page: Int!) {
            posts(first: $first, page: $page) {
                data {
                    id
                    title
                    image
                    excerpt
                    slug
                    view
                    like
                    is_bookmarked
                    is_liked
                    author
                    postCategories {
                        id
                        title
                    }
                }
                paginatorInfo {
                    total
                    currentPage
                    lastPage
                    perPage
                    hasMorePages
                }
            }
        }
    `,
    variables: { first, page },
    requestPolicy: 'cache-and-network',
})

服务器架构:

type Query {
    posts: [Post!]!  @paginate 

}

type PostComment{
    id: ID!
    post_id: ID!
    user_id: ID
    author: String
    comment: String!
    like: Int!
    dislike: Int!
    report_count: Int!
    publish: Boolean!
    is_liked: Boolean
    is_bookmarked: Boolean
    post: Post! @belongsTo
    created_at: String
    updated_at: String

}

type Post {
    id: ID!
    title: String!
    slug: String!
    view: Int!
    like: Int!
    body: String!
    image: String
    excerpt: String
    author: String
    publish: Boolean!
    highlight: Boolean!
    is_liked: Boolean
    created_at: String
    is_bookmarked: Boolean
    postCategories: [PostCategory!]! @belongsToMany
    postComments: [PostComment!]! @hasMany

}

how can i define a posts query that show only published post?
i dont want to use where condition like this.

 posts(@where(operator: "=", "publish", "1")): [Post!]!  @paginate

i want the server posts's query take only published post by default

client:

$: posts = queryStore({
    client: getContextClient(),
    query: gql`
        query ($first: Int!, $page: Int!) {
            posts(first: $first, page: $page) {
                data {
                    id
                    title
                    image
                    excerpt
                    slug
                    view
                    like
                    is_bookmarked
                    is_liked
                    author
                    postCategories {
                        id
                        title
                    }
                }
                paginatorInfo {
                    total
                    currentPage
                    lastPage
                    perPage
                    hasMorePages
                }
            }
        }
    `,
    variables: { first, page },
    requestPolicy: 'cache-and-network',
})

server schema:

type Query {
    posts: [Post!]!  @paginate 

}

type PostComment{
    id: ID!
    post_id: ID!
    user_id: ID
    author: String
    comment: String!
    like: Int!
    dislike: Int!
    report_count: Int!
    publish: Boolean!
    is_liked: Boolean
    is_bookmarked: Boolean
    post: Post! @belongsTo
    created_at: String
    updated_at: String

}

type Post {
    id: ID!
    title: String!
    slug: String!
    view: Int!
    like: Int!
    body: String!
    image: String
    excerpt: String
    author: String
    publish: Boolean!
    highlight: Boolean!
    is_liked: Boolean
    created_at: String
    is_bookmarked: Boolean
    postCategories: [PostCategory!]! @belongsToMany
    postComments: [PostComment!]! @hasMany

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2025-02-18 11:52:31

您可以使用 laravel scopes 。在帖子模型中添加本地scepepepept:

public function scopePublished($query)
{
    return $query->where('publish', '=', 1);
}

然后通过@paginate

 posts: [Post!]! @paginate(scopes: "published")

you can use laravel scopes. Add a local scopePublished to the post model:

public function scopePublished($query)
{
    return $query->where('publish', '=', 1);
}

Then use it inside your schema by @paginate:

 posts: [Post!]! @paginate(scopes: "published")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文