试图将从飞机台获取的数据连接到gatsby graphql到algolia,插件不起作用

时间:2018-10-11 17:33:23

标签: javascript reactjs algolia gatsby airtable

https://github.com/algolia/gatsby-plugin-algolia

我运行构建时此插件似乎在我的gatsby-config中不起作用(不填充我的algolia索引)-我已经使用algoliasearch和json文件将数据推入了我的索引中,但是我希望每次构建时都能自动连接它-因此数据始终与空气表数据保持最新。

我已经通过github上的文档(放在我的gatsby-config.js文件中)尝试了“ gatsby-plugin-algolia”方法

const myQuery = `{
  allSitePage {
    edges {
      node {
        # try to find a unique id for each node
        # if this field is absent, it's going to
        # be inserted by Algolia automatically
        # and will be less simple to update etc.
        objectID: id
        component
        path
        componentChunkName
        jsonName
        internal {
          type
          contentDigest
          owner
        }
      }
    }
  }
}`;

const queries = [
  {
    query: myQuery,
    transformer: ({ data }) => data.allSitePage.edges.map(({ node }) => node), 
    indexName: 'cardDemo',
  },
];

module.exports = {
  plugins: [
        {
      resolve: 'gatsby-source-airtable-linked',
      options: {
        apiKey: process.env.MY_API_KEY,
        tables: [
          {
            baseId: process.env.MY_BASE_ID,
            tableName: 'Streams',
            tableView: 'DO NOT MODIFY',
          },
        ],
      },
    },
    {
      resolve: 'gatsby-plugin-algolia',
      options: {
        appId: process.env.MY_AGOLIA_APP_ID,
        apiKey: process.env.MY_AGOLIA_API_KEY,
        indexName: 'cardDemo',
        queries,
        chunkSize: 1000000,
      },
    },
  ],
};

我还通过airtable为我正在组件上使用的更具体的实例提供了'myQuery'的显示,如下所示

const myQuery = `{
      items: allAirtableLinked(
      filter: {
        table: { eq: "Items" }
      }
    ) {
      edges {
        node {
          id
          data {
            title
            thumbnail_url
            thumbnail_alt_text
            url_slug
            uberflip_stream_id
            uberflip_id
          }
        }
      }
    }
    }`;

如果有人正在运行此插件,并且该插件可以正常工作,我肯定可以使用一些提示使其正常工作(此软件包上没有太多文档)

谢谢!

1 个答案:

答案 0 :(得分:2)

弄清楚了!任何遇到此问题的人,请执行以下步骤:

  1. 检查您是否具有正确的API密钥
  2. 检查Translator方法是否更改为与graphql中查询的对象匹配。我必须更改为:

transformer: ({ data }) => data.items.edges.map(({ node }) => node) 

  1. 检查您的查询是否在graphql中工作,确保其语法正确,并提取正确的数据。我使用的查询是

const pageQuery = `query {
  items: allAirtableLinked(
    filter: {
      table: { eq: "Items" }
      data: { hubs: { eq: "cf4ao8fjzn4xsRrD" } }
    }
  ) {
    edges {
      node {
        id
        data {
          title
          thumbnail_url
          thumbnail_alt_text
          duration
          url_slug
          asset_type
          uberflip_stream_id
          uberflip_id
        }
      }
    }
  }
}`;

  1. 最后,如果您将查询抽象化并查询到src中的ultil目录,然后将其输入配置文件中以使其更干净,则它会更干净:

i got this idea from this repo, very helpful! check out this example