Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query graphql returning empty result #26

Open
IgordeOliveira opened this issue Aug 24, 2020 · 0 comments
Open

Query graphql returning empty result #26

IgordeOliveira opened this issue Aug 24, 2020 · 0 comments

Comments

@IgordeOliveira
Copy link

IgordeOliveira commented Aug 24, 2020

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)
      }
    })
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant