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

Circular-references in returned entry #1893

Open
Crafoord opened this issue Apr 25, 2023 · 3 comments
Open

Circular-references in returned entry #1893

Crafoord opened this issue Apr 25, 2023 · 3 comments

Comments

@Crafoord
Copy link

Expected Behavior

When getting a resolved entry I'm expecting it to be free of circular references. (I also tried getEntries but same problem)

const currentEntry =
  await contentfulClient.withoutUnresolvableLinks.getEntry(key.id, {
    include: 10, // I want it to resolve as deep as possible
  });

Actual Behavior

I'm getting an object with circular-references, for testing I'm trying to do JSON.stringify for currentEntry resolving in:

"error": {
  "name": "TypeError",
  "message": "Converting circular structure to JSON\n    --> starting at object with constructor 'Object'\n    |     property 'fields' -> object with constructor 'Object'\n    |     property 'relatedArticles' -> object with constructor 'Array'\n    |     ...\n    |     property 'relatedArticles' -> object with constructor 'Array'\n    --- index 0 closes the circle"
}

My entry (Article) has a field relatedArticles that has the following circular reference:

myEntryA
  relatedArticles: [
    anotherArticleB:
      relatedArticles: [
        myEntryA <-- problem!
      ]
  ]

There is not really any documentation about this, in earlier versions there was toPlainObject that was supposed to fix this but It never worked, thus the upgrade of version... I've been looking into this issue but not success...

Possible Solution

Add some documentation regarding this. I tried mimicking "fixes" from the linked issue above but from what I can tell that is implemented in this version of the SDK already?

Steps to Reproduce

  1. Create an entry with a circular reference
  2. Use getEntry or getEntries to get that entry (with include 10)
  3. Try JSON.stringify

Context

I'm trying to keep a copy of our (resolved) contentful entries in dynamodb.

Environment

  • Language Version: v16.20.0
  • Package Manager Version: 8.19.4
  • Package Version: 10.1.0
  • Which API are you using?: Delivery
@dannesart
Copy link

Having the same issue. But I use include: 2. I get:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'fields' -> object with constructor 'Object'
| property 'sections' -> object with constructor 'Array'
| ...
| property 'contentBanners' -> object with constructor 'Array'
--- index 0 closes the circle

I tried to find simular tickets with solutions. But all is closed due to inactivity.
It feels like include: 1, works. But as soon you put 2 or more. Contentful tries to get entries on a eternal level.
using
"contentful": "^9.2.5",

@drooJohnson
Copy link

Still facing this issue, and it is making it impossible to pull any entries from a contentType that links to other pages, because those pages can reference the contentType that links to other pages.

Which in turn makes it impossible to pull the pages themselves, since they can contain the other contentType to link to other pages.

I've tried solutions where I'm trying to trim out everything but the sys.id on the linking contentType, but that doesn't seem to be possible. Those linking contentType entries can appear in multiple other contentTypes, and iterating through the response doesn't seem to fix the problem.

If there were some way to say that the "Link," contentType should only ever grab like, its text field, and its sys.id so I can fetch the rest of its data later, that would fix the problem. But I can't see any way to do that. Which means I can't see any way to link to other pages by reference, and providing those links by URL feels dangerous because that's a field you need to change in two places — once on the page to be linked to, and once in the "Link."

@tjbp
Copy link

tjbp commented Nov 17, 2023

If it helps anybody, I used json-cycle to convert the circular references into static keys that can be represented in object notation, then again in the client to repopulate the circular references.

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

4 participants