0

i'm trying to add an searchindex at buildtime in my gatsby + apollo project.
I tried following following this article:

I get stuck because in the example they are using the MarkdownRemark integrated in the gatsby starter. However I want to use the data I fetched from Apollo (using gatsby-source-graphql).

My query that I need to fetch looks something like this:

{
  apollo {
    allExampleData {
      name
      ...
    }
  }
}

The turorial uses the context.nodeModel to fetch the MarkdownRemark which are all separate nodes. In my case there is 1 GraphQLSource node (= apollo) and several collections under it (fe: allExampleData). When trying to fetch the GraphQLSource I don't see any children.

Code I use to fetch the data:

// gatsby-node.js
exports.createResolvers = ({ cache, createResolvers }) => {
  createResolvers({
    Query: {
      LunrIndex: { // = name of the index
        type: GraphQLJSONObject,
        resolve: (source, args, context, info) => {
          // fetching apollo node
          const apolloNode = context.nodeModel.getAllNodes({
            type: `GraphQLSource`,
          })
          // ... create index etc ...
        }
    }
})

This is the response I see of variable apolloNode:

// console.log(apolloNode)
{
  id: '6b0123db-62eb-5c8c-9465-c75ecc7526f9',
  typeName: 'Apollo',
  fieldName: 'apollo',
  parent: null,
  children: [],
  internal: {
    type: 'GraphQLSource',
    contentDigest: '0380f5a271ee2bec2ab7a95ec173714a',
    ignoreType: true,
    counter: 55,
    owner: 'gatsby-source-graphql'
  }
}

Is there any way to fetch the underlying allIndustries based on this? Or is there another way I can push data during build time into lunr?

control-panel
  • 255
  • 6
  • 17
  • Have you try getting the children nodes directly? i.e `getAllNodes({ type: 'ExampleData' })` – Derek Nguyen Jul 23 '20 at 11:19
  • I did, but since its not at the top level it returns an empty array. I also tried: getAllNodes({ type: 'GraphQLSource' }).getAllNodes({ type: 'ExampleData' }) which is not allowed (context.nodeModel.getAllNodes(...).getAllNodes is not a function) – control-panel Jul 23 '20 at 11:35
  • I think we're missing a bit more info to help you out, if you could put together a minimal reproducible example I can take a look – Derek Nguyen Jul 23 '20 at 12:41

0 Answers0