contentful.js
                                
                                 contentful.js copied to clipboard
                                
                                    contentful.js copied to clipboard
                            
                            
                            
                        Circular references when include >= 2
Expected Behavior
in SSR, should be able to stringify contentful response objects without circular references error. Original issue here.
Actual Behavior
In my current data model an Article can contain references to other relatedArticles. When using SSR (nextjs) I get an error regarding circular references. Currently my resolveDepth is set to 2. When the resolveDepth is 1, I do not get the issue, however, I want one level deeper.
Context
I can get around this issue by using a stringify safe option like https://github.com/moll/json-stringify-safe, however, this seems unnecessary and unnecessarily expensive.
Proposed Solns
- When parsing the response objects create unique references.
- create an API that allows you to override resolveDepth for specific fields on the response. (this would allow me to fetch those entities by their id directly instead of having them be resolved in the initial request)
What's the resolveDepth param? I can't see any docs anywhere on it.
@jtomaszewski I believe by "resolveDepth" @TroutZen meant the "include" parameter
Same problem here. It's a strange behavior, or so it seems.
Imagine that we have the following:
A ---references---> B ---references---> A
Current Behavior:
Even if you use the include parameter to limit the depth of resolved links, it will still return the last entry A fully resolved instead of a link.
Expected Behavior:
It would respect the include parameter and return the last A entry as a link.
Note
I have seen multiple issues opened about this problem, even closed ones from a few years ago. Is there any expectation to tackle this problem?
I encountered the same issue while working with SSR. After going through the sdk code for several hours, I managed to find a workaround.
So what happens is that contentful-resolve-response creates the circular references automatically when you call getEntries() or getEntry(). If you try to stringify the return value of one of those functions, you'll get the circular reference error.
You can disable this behaviour in the global configuration with the resolveLinks flag, or you can disable it on an individual query level.
If you disable resolveLinks, you'll be able to serialize the return value form getEntries. After serializing it, you can run it trough the link resolver manually.
I'm working with Angular and have a wrapper around the contentful SDK so I can't paste actual code, but this should give you a rough hint on how I got it working. (No guarantees that this code even runs ;) )
import resolveResponse from 'contentful-resolve-response'
const query = {
      content_type: 'asdf',
      resolveLinks: false
}
const entryCollection = await cdaClient.getEntries(query)
// You can stringify this all you want
const jsonCollection = JSON.stringify(entryCollection)
const parsedCollection = JSON.parse(jsonCollection)
// Either of these work for me
const resolvedCollection = resolveResponse(parsedCollection)
const resolvedCollection = resolveResponse(entryCollection)
closing the issue, if you still need help with this please reach out to Contentful support