如何为Gatsby构建重新获取graphql数据?

时间:2020-04-10 12:33:07

标签: graphql gatsby

我有一个gatsby-theme React应用程序,该应用程序使用gatsby-source-graphql从后端应用程序中获取数据。我已经在refetchInterval: 20文件中设置了gatsby-config.js。我的gastby-config.js文件如下

module.exports = {
  siteMetadata: {
    title: 'Gatsby Theme Mytheme',
    ...
  },
  pathPrefix: `/gtm`,
  plugins: [
    {
      ...
      ...
    {
      resolve: `gatsby-source-graphql`,
      options: {
        fieldName: `cms`,
        url: `http://127.0.0.1:7000/api/graphiql`,
        typeName: `CMSData`,
        refetchInterval: 20,
      },
    },
    `gatsby-plugin-sharp`,
    `gatsby-transformer-sharp`,
  ],
};

在开发模式(gatsby develop中运行应用程序时,每隔20秒,我就能看到(console.log)在后端CMS应用程序中所做的更改。但是更改无法在构建(gatsby构建)上看到/显示。因此,构建后,无论我在后台进行的更改如何,都不会反映在我的前端应用程序中。那么,如何为我的gatsby构建动态地从后端应用程序获取更改?

我用于获取后端GraphQL数据的组件为

import { useStaticQuery, graphql } from "gatsby";

export function myComponent() {
  const cmsMenu = useStaticQuery(graphql`
    {
      cms {
        allMenus {
          edges {
            node {
              menuName
            }
          }
        }
      }
    }
  `)
  console.log("CMS menu from MenuProvider", cmsMenu); 
}

0 个答案:

没有答案