从GraphQL API填充数据库
主题名称所说,我正在搜索数据库,其中数据库来自已存在的数据库graphql api 。
我的项目使用 next.js , PostgreSQL , apollo graphql 和 prisma 连接整体。我设法从API检索数据并显示这些数据。现在,我只想将这些数据存储在我的Postgres数据库中。因此,您知道我正在使用客户端渲染,而不是其他方法(SSR,SSG)。
我发现的所有文档都是关于创建API然后发布到数据库的所有文档,这不是我的案例或文档 create & createMany 查询,但带有硬编码数据。我的研究并不能帮助我看到清晰的事物,所以我来这里找到了愿意指导我的人。如果有线索,我会很感激。谢谢=)!
在这里说明一个代码:
import { useQuery, gql } from "@apollo/client";
// query to retrieve 3 animes
const QUERY = gql`
query GetFirstsThree{
Page(page: 1, perPage: 3) {
media {
title {
userPreferred
}
}
}
}
`;
export default function AnimList() {
const { data, loading, error } = useQuery(QUERY);
if (loading) {
return <h2>Loading...</h2>;
}
if (error) {
return null;
}
const medias = data.Page.media;
return (
<div className="mainGrid">
{medias.map((value) => {
return (
<p>{value.title.userPreferred} </p>
)
})}
</div>
);
}
I'm searching, as the topic name says, to populate a database with datas coming from an already exist graphql API.
My project is using Next.js, PostgreSQL, Apollo for GraphQL and Prisma to connect the whole. I managed to retrieve datas from the API and display these. Now I just want to store these datas in my Postgres database. So you know I'm using the Client-Side Rendering and not the other methods (SSR, SSG).
All documentations I found was about creating an API then post to the database which is not my case or documentation about create & createMany queries but with hardcoded datas. My research doesn't help me to see clear so I come here to find anyone who is willing to guide me. If have a clue I'll appreciate. Thank you =) !
To illustrate here a piece of code :
import { useQuery, gql } from "@apollo/client";
// query to retrieve 3 animes
const QUERY = gql`
query GetFirstsThree{
Page(page: 1, perPage: 3) {
media {
title {
userPreferred
}
}
}
}
`;
export default function AnimList() {
const { data, loading, error } = useQuery(QUERY);
if (loading) {
return <h2>Loading...</h2>;
}
if (error) {
return null;
}
const medias = data.Page.media;
return (
<div className="mainGrid">
{medias.map((value) => {
return (
<p>{value.title.userPreferred} </p>
)
})}
</div>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些在这里遇到相同问题的人来说,您好是我如何解决问题。
首先,我创建了一个异步功能来发布动漫。
然后,我添加了一个提交输入,该输入在其OnClick事件中将以显示动漫标题的方式映射媒体,但在这里将数据存储在数据库中。
之后,我用数据来创建一个要存储的变量。最后,我调用我在发布动漫之前创建的功能,并通过该功能通过该变量。
这几乎全部。
Hello for those who have the same issue here is how i resolved my problem.
First I have created a async function to post my Anime.
Then I added a submit input, which on its onClick event will map the medias the same way I did to display the anime's title, but here to store the datas in the database.
After, I create a variable with my datas that I want to store. Finally, I call the function I've created before to post my Anime, on which I pass the variable.
And that pretty much all.