angeloashmore / gatsby-plugin-local-search

Gatsby plugin for providing client-side search for data available in Gatsby's GraphQL layer using a variety of engines
MIT License
51 stars 29 forks source link

Query graphql returning empty result #26

Open IgordeOliveira opened 4 years ago

IgordeOliveira commented 4 years ago

I can't use a query that contains the metadata of my pages, because for the lib it is empty but when I query in the graphql IDE it returns me all the results, does it have something to do with the order of execution? how to fix this?

my config

module.exports = {
  // A unique name for the search index. This should be descriptive of
  // what the index contains. This is required.
  name: 'pages_metadata',

  // Set the search engine to create the index. This is required.
  // The following engines are supported: flexsearch, lunr
  engine: 'flexsearch',

  // Provide options to the engine. This is optional and only recommended
  // for advanced users.
  //
  // Note: Only the flexsearch engine supports options.
  // engineOptions: 'speed',

  // GraphQL query used to fetch all data for the search index. This is
  // required.
  query: `{
    allPagesIndexable {
      nodes {
        path
        metadata {
          content
          keywords
          title
          type
        }
      }
    }
  }`,
  // Field used as the reference value for each document.
  // Default: 'id'.
  ref: 'path',

  // List of keys to index. The values of the keys are taken from the
  // normalizer function below.
  // Default: all fields
  index: ['title', 'keywords', 'content'],

  // List of keys to store and make available in your UI. The values of
  // the keys are taken from the normalizer function below.
  // Default: all fields
  // store: ['id', 'path', 'title'],

  // Function used to map the result from the GraphQL query. This should
  // return an array of items to index in the form of flat objects
  // containing properties to index. The objects must contain the `ref`
  // field above (default: 'id'). This is required.
  normalizer: ({ data }) => {
    console.log(data.allPagesIndexable) // return [Object: null prototype] { nodes: [] }
    return data.allPagesIndexable.nodes.map(node => ({
      path: node.path,
      ...node.metadata
    }))
  }
}

example of result of my query in IDE:

{
  "data": {
    "allPagesIndexable": {
      "nodes": [
        {
          "path": "/curso/graduacao/contador-global",
          "metadata": {
            "content": null,
            "keywords": null,
            "title": null,
            "type": "Página"
          }
        },
        {
          "path": "/curso/graduacao/contador-global/informacoes-adicionais",
          "metadata": {
            "content": null,
            "keywords": null,
            "title": null,
            "type": "Página"
          }
        },
...

I created this separate node because I was having "metadata does not exist in context" problems when the query was executed by this lib how i create the node:

exports.onCreateNode = ({ node, actions, createNodeId, createContentDigest }) => {
  const { createNode } = actions
  // Transform the new node here and create a new node or
  // create a new node field
  if (node.internal.type === "SitePage" && node.context && node.context.metadata) {
    const page = {
      path: node.path,
      metadata: node.context.metadata
    }
    createNode({
      ...page,
      id: createNodeId(page.path),
      internal: {
        type: `PagesIndexable`,
        contentDigest: createContentDigest(page)
      }
    })
  }
}