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
}
CodePudding user response:
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")