Vue.js 使用 GraphQl 分页
Vue.js with GraphQl pagination
我第一次使用 Graphql vue.js 但无法获得分页
在职的。
vue 组件如下所示:
<template>
<ApolloQuery :query="require('./breeds.gql')" :variables="{ page }" v-slot="{ result: { loading, error, data }, query }">
<div v-if="data">
<div class="flex flex-wrap lg:-m-2" v-if="data.breeds">
<breed class="gutter-three-items h-64" :breed="breed" :key="breed.id" v-for="breed in data.breeds"></breed>
</div>
<div v-if="data.breeds && !isLoading">
<h3 class="text-primary p-2 md:p-0">
Geen resultaat
</h3>
</div>
<div class="flex justify-start w-full">
<load-more container="scroll-breeds" :loading="isLoading" @load="page++"> </load-more>
</div>
</div>
</ApolloQuery>
</template>
<script>
export default {
data() {
return {
page: 1,
};
}
</script>
breeds.gql 看起来像这样:
query Breeds($page: Int!) {
breeds(page: $page) {
id
name_nl
slug_nl
full_overview_image
total_posts
is_activated
total_dogs
}
}
然而,当我查看我的网络选项卡时,响应是这样的:
Cannot query field "id" on type "BreedPaginator
我这里哪里做错了?
我的后端使用这个 https://github.com/nuwave/lighthouse。
架构如下所示:
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-01-01 13:00:00`."
scalar DateTime @scalar(class: "Nuwave\Lighthouse\Schema\Types\Scalars\DateTime")
"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\Lighthouse\Schema\Types\Scalars\Date")
type Breed {
id: ID,
name_nl: String
slug_nl: String
full_overview_image: String
total_posts: String
is_activated: Boolean
total_dogs: String
}
type Query {
breeds: [Breed] @paginate
}
请帮帮我!
通过使用 @paginate
指令,您可以将字段类型转换为特定于原始字段类型的分页器类型。这在 the docs:
中有解释
This directive is meant to be used on root query fields:
type Query {
posts: [Post!]! @paginate
}
The schema definition is automatically transformed to this:
type Query {
posts(first: Int!, page: Int): PostPaginator
}
"A paginated list of Post items."
type PostPaginator {
"A list of Post items."
data: [Post!]!
"Pagination information about the list of items."
paginatorInfo: PaginatorInfo!
}
And can be queried like this:
{
posts(first: 10) {
data {
id
title
}
paginatorInfo {
currentPage
lastPage
}
}
}
我第一次使用 Graphql vue.js 但无法获得分页 在职的。
vue 组件如下所示:
<template>
<ApolloQuery :query="require('./breeds.gql')" :variables="{ page }" v-slot="{ result: { loading, error, data }, query }">
<div v-if="data">
<div class="flex flex-wrap lg:-m-2" v-if="data.breeds">
<breed class="gutter-three-items h-64" :breed="breed" :key="breed.id" v-for="breed in data.breeds"></breed>
</div>
<div v-if="data.breeds && !isLoading">
<h3 class="text-primary p-2 md:p-0">
Geen resultaat
</h3>
</div>
<div class="flex justify-start w-full">
<load-more container="scroll-breeds" :loading="isLoading" @load="page++"> </load-more>
</div>
</div>
</ApolloQuery>
</template>
<script>
export default {
data() {
return {
page: 1,
};
}
</script>
breeds.gql 看起来像这样:
query Breeds($page: Int!) {
breeds(page: $page) {
id
name_nl
slug_nl
full_overview_image
total_posts
is_activated
total_dogs
}
}
然而,当我查看我的网络选项卡时,响应是这样的:
Cannot query field "id" on type "BreedPaginator
我这里哪里做错了?
我的后端使用这个 https://github.com/nuwave/lighthouse。
架构如下所示:
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-01-01 13:00:00`."
scalar DateTime @scalar(class: "Nuwave\Lighthouse\Schema\Types\Scalars\DateTime")
"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\Lighthouse\Schema\Types\Scalars\Date")
type Breed {
id: ID,
name_nl: String
slug_nl: String
full_overview_image: String
total_posts: String
is_activated: Boolean
total_dogs: String
}
type Query {
breeds: [Breed] @paginate
}
请帮帮我!
通过使用 @paginate
指令,您可以将字段类型转换为特定于原始字段类型的分页器类型。这在 the docs:
This directive is meant to be used on root query fields:
type Query { posts: [Post!]! @paginate }
The schema definition is automatically transformed to this:
type Query { posts(first: Int!, page: Int): PostPaginator } "A paginated list of Post items." type PostPaginator { "A list of Post items." data: [Post!]! "Pagination information about the list of items." paginatorInfo: PaginatorInfo! }
And can be queried like this:
{ posts(first: 10) { data { id title } paginatorInfo { currentPage lastPage } } }