There has been no solution from Contentful other than to create the object yourself. This is what we use (TypeScript): function CreateLinkSysObject(linkToId: string): object { return { sys: { type: "Link", linkType: "Entry", id: linkToId }}}
Then you link the reference field to that object with Contentful Management API
I have spent some days trying to make a simple script that sends data from one system into contentful.
In my opinion the product lacks vital documentation. This turns simple tasks into detective work which in turn is time consuming.
Once we upped the include value we got the full linked entry rather than just the reference id. Then we used normalizr to flatten everything out into an easier to manage data object. It’s especially helpful when you need to perform any processing on the data upfront. For example we use it to build url strings for direct linking by joining slugs of each referenced parent all the way up our model’s tree. We also found this helpful shape the data object by removing the separate fields and sys object and keeping only what we needed. Rather than:
We could just ask Contentful for the fields we use but we have found it easier to get everything and when we want to change something our schema, not our API requests, are our source of truth.
The API response also has an includes array as well as the items array that contains all the linked entries and assets (up to the depth of the include option). You could find the referenced entry by searching that includes array in a similar way normalizr produces lookup arrays but you lose on the ability to process the data.
If you have any more questions about this I’m happy to help. I just went through the pain of implementing this so it’s pretty fresh on my mind.
High level approach: we define a normalizer schema for each content type we use which applies any transformations we need to perform. This includes building urls, linking children to parents, and flattening the data structure.
Find me on twitter @krothenbaum, send me a DM there and I’m happy to discuss further.
Cheers for the information @kyle.r . I’ve followed you on Twitter now “mabai”, but I’m unable to send a message
I’ll write the questions I have here as I’m sure they will help others:
I’ve just recently inherited a deeply nested Contenful project. I don’t always know ahead of time how big the nesting structure will be. How do you handle something like this currently, do you request with the highest includes value always?
Do you use any libraries you recommended for data handling, once your data has been through the “normalizer”? I can see “normalizer” only handles the structuring and doesn’t really expose any api for CRUD on the data right?