Created
January 5, 2021 12:31
-
-
Save ajmalafif/07c021359083a22d67d262dc6093db4c to your computer and use it in GitHub Desktop.
Algolia + Sanity.io + Gatsby queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const escapeStringRegexp = require("escape-string-regexp") | |
// const pagePath = `content` | |
// const indexName = `Pages` | |
const pageQuery = `{ | |
pages: allMdx { | |
edges { | |
node { | |
id | |
frontmatter { | |
title | |
} | |
fields { | |
slug | |
} | |
excerpt(pruneLength: 5000) | |
} | |
} | |
} | |
}` | |
const postQuery = `{ | |
posts: allSanityPost { | |
edges { | |
node { | |
id | |
title | |
slug { | |
current | |
} | |
_rawExcerpt | |
} | |
} | |
} | |
}` | |
function pageToAlgoliaRecord({ node: { id, frontmatter, fields, ...rest } }) { | |
return { | |
objectID: id, | |
...frontmatter, | |
...fields, | |
...rest, | |
} | |
} | |
function postToAlgoliaRecord({ node: { id, slug, ...rest } }) { | |
return { | |
objectID: id, | |
...slug, | |
...rest, | |
} | |
} | |
const queries = [ | |
{ | |
query: pageQuery, | |
transformer: ({ data }) => data.pages.edges.map(pageToAlgoliaRecord), | |
indexName: `Pages`, | |
settings: { attributesToSnippet: [`excerpt:20`] }, | |
}, | |
{ | |
query: postQuery, | |
transformer: ({ data }) => data.posts.edges.map(postToAlgoliaRecord), | |
indexName: `Posts`, | |
settings: { attributesToSnippet: [`excerpt:20`] }, | |
}, | |
] | |
module.exports = queries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For future reference, some idea for a better alternative approach by Corey Ward (@coreyward) from Sanity.io Slack channel: