从GraphQL API填充数据库

发布于 2025-02-12 15:53:36 字数 1333 浏览 0 评论 0原文

主题名称所说,我正在搜索数据库,其中数据库来自已存在的数据库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 技术交流群。

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

发布评论

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

评论(1

何以笙箫默 2025-02-19 15:53:36

对于那些在这里遇到相同问题的人来说,您好是我如何解决问题。

首先,我创建了一个异步功能来发布动漫。

async function savedAnime(anime){
  const res = await fetch('api/anime', {
    method: 'Post',
    body: JSON.stringify(anime),
  });
if(!res.ok){
  throw new Error('Something went wrong');
}
  return await  res.json();
}

然后,我添加了一个提交输入,该输入在其OnClick事件中将以显示动漫标题的方式映射媒体,但在这里将数据存储在数据库中。
之后,我用数据来创建一个要存储的变量。最后,我调用我在发布动漫之前创建的功能,并通过该功能通过该变量。

这几乎全部。

      <input type="submit" value="Save Animes" onClick={ async () => {
        try{
        medias.map(async (value) => {
          const anime = {
            title: value.title.userPreferred,
            coverImage: value.coverImage.medium,
          };
          console.log(anime);
          savedAnime(anime); 
        })
        } catch(err){
          console.error(err);
        }
      }}/>

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.

async function savedAnime(anime){
  const res = await fetch('api/anime', {
    method: 'Post',
    body: JSON.stringify(anime),
  });
if(!res.ok){
  throw new Error('Something went wrong');
}
  return await  res.json();
}

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.

      <input type="submit" value="Save Animes" onClick={ async () => {
        try{
        medias.map(async (value) => {
          const anime = {
            title: value.title.userPreferred,
            coverImage: value.coverImage.medium,
          };
          console.log(anime);
          savedAnime(anime); 
        })
        } catch(err){
          console.error(err);
        }
      }}/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文