use*_*377 6 apollo reactjs graphql next.js apollo-client
我很难理解基于变量的动态路由。我能够使用 Next.js 获取集合中的项目列表,但无法获取具有动态路由的单个页面的单个项目及其字段。
我有一个带有 GraphQL API 的 KeystoneJS 无头 CMS。我正在尝试创建一个简单的博客,其中包含帖子列表和单独的帖子页面。我已经能够查询并返回帖子列表,但我需要根据 slug 字段获取单个帖子,以便可以在 访问它/posts/[slug].js。
我一直在使用 Apollo Client 来处理查询。我有一个apolloClient.js连接到 API 的文件:
// apolloClient.js
import { ApolloClient, InMemoryCache } from "@apollo/client";
export default new ApolloClient({
uri: "http://localhost:3000/admin/api",
cache: new InMemoryCache(),
});
Run Code Online (Sandbox Code Playgroud)
我有一个post.service.js文件来查询API:
// post.service.js
import { gql } from "@apollo/client";
import apolloClient from "../_utils/apolloClient";
export async function getAll() {
return apolloClient
.query({
query: gql`
query {
allPosts {
id
term
slug
}
}
`,
})
.then((result) => result.data.allPosts);
}
export async function getBySlug(slug) {
return apolloClient
.query({
query: gql`
query {
Post(slug: $slug) {
id
title
lead
body
}
}
`,
})
.then((result) => {
return result.data.Post;
});
}
Run Code Online (Sandbox Code Playgroud)
最后,posts/[slug].js我试图像这样返回数据:
//[slug].js
import Head from "next/head";
import { ApolloClient, InMemoryCache, gql } from "@apollo/client";
import { getAll, getBySlug } from "../../_services/post.service";
export default function Post({ post }) {
return (
<div>
<Head>
<title>Launchpad</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1>{post.term}</h1>
</main>
<footer>
<p>{post.lead}</p>
</footer>
</div>
);
}
export async function getStaticPaths() {
const posts = await getAll();
const paths = posts.map((post) => ({
params: { id: post.slug },
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const post = await getBySlug(params.id);
return { props: { post } };
}
Run Code Online (Sandbox Code Playgroud)
显然,这是行不通的。我一定不能将变量(我假设是 slug)正确地传递到查询中,并且在阅读了几个教程后我仍然无法理解它。有人看到我做错了什么吗?
在对象getStaticPaths返回的键中params需要匹配动态路由命名。就您而言,由于您正在使用posts/[slug].js路线,因此您需要params以格式返回{ slug: post.slug }。
export async function getStaticPaths() {
const posts = await getAll();
const paths = posts.map((post) => ({
params: { slug: post.slug }, // Rename to `slug`
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const post = await getBySlug(params.slug); // Rename to `params.slug`
return { props: { post } };
}
Run Code Online (Sandbox Code Playgroud)
编辑:关于请求问题,以下更改getBySlug应该使其按预期工作。
export async function getBySlug(slug) {
return apolloClient
.query({
query: gql`
query Post($slug: String){
post(slug: $slug) {
id
title
lead
body
}
}
`,
variables: {
slug
}
})
.then((result) => {
return result.data.Post;
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4922 次 |
| 最近记录: |