contentful-big-data 🥦
Use Contentful's CDA and GraphQL to do big queries

About
Have you ever made a CDA request to Contentful, assuming you got all the data, only to later found out you had a total of 1001 entries and Contentful only supports 1000?
This module is used to wrap around Contentful's CDA and GraphQL to make the most effective use of both of the APIs to perform powerful searches and more conservative response selectors.
Disclaimers
- I do not work for Contentful. I am a developer who professionally and personally use the service and this is something I was seeing as a "missing link" to get the data in its entirety.
- Use at your own risk. Contentful has limitations on how many API requests you can do in any given period. If you are charged for overages I recommend you pay attention to how you use this tool as it is meant to do a lot of queries.
Usage
- First add this library to your node project:
npm i contentful-big-data
- Use the library like so
import CBD from 'contentful-big-data'
const cbd = CBD({
space: 'space-id-here'
key: 'space-key-here'
})
const results = await cbd.fetch({
'fields.author[in]': 'authorid1,authorid2,authorid3'
}, `{
name
image { url }
}`)
console.log(results)
Features
This library comes from a need due to limitations with Contentful
- Request size too large
- Response size too large
- Response collection too narrow (not the full set)
- Response includes
null
entries
Because of these limitations, this library has been created to satisfy these shortcomings.
- Multi-Query - The concept revolves around making a CDA request with
limit=0
with the purpose of getting the count of results. This tells us how many additional requests are needed to complete the dataset. - Multi-Service - Instead of using the CDN or GraphQL, use both. The CDN is great for requesting all the IDs, and the GraphQL is useful for limiting the response size.
- Retry-Oriented - Sometimes you hit a limit with Contentful. This could be from the size of the response, or because you hit the rate limiter. If that happens we should adjust where appropriate and try again.
- Size-Aware - There are limitations on both the request and response sizes. We should break apart requests that are too large, and dynamically respond if we get a reponse too large error from Contentful. (coming soon)
Examples
The following examples can be used as a guide in your own implementation.
const allEntries = await cbd.fetch({
content_type: 'page'
}, `{
title
}`)
const secondPage = await cbd.fetch({
content_type: 'page',
skip: 100,
limit: 100
}, `{
title
}`)
const linkedPages = await cbd.fetch({
content_type: 'page',
'fields.author.sys.id[in]': ['qwerty','yuiop']
}, `{
title
url
thumbnail {
url
width
height
}
}`)
Docs
CBD (constructor)
const cbd = CBD(config={
space: 'space-id-here',
key: 'space-key-here',
previewKey: 'preview-key-here',
env: 'master',
retry: 3,
failSilently: false
})
Note - You can populate both key
and previewKey
but if you only plan to use the tool for one purpose, only one key is required.
CBD.fetch
cbd.fetch(
{
custom: 'see below'
},
`{
fields
goHere
graphQL {
entries
}
seeSelectorsBelow
}`,
{
isPreview: false,
retry: 3,
verbose: false,
failSilently: false,
})
References