Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Returning Multiple References #85

Closed
chuise opened this issue Apr 20, 2016 · 9 comments
Closed

Returning Multiple References #85

chuise opened this issue Apr 20, 2016 · 9 comments
Assignees
Labels

Comments

@chuise
Copy link

chuise commented Apr 20, 2016

Pardon my ignorance on this... I'm having a hard time determining how to pull back multiple content types that reference other content types. For example... We have integration that belong to an integration_category (ex. Salesforce belongs to CRM category). It seems that if I use the following cda_query all other content types are ignored:

f.cda_query     = { content_type: 'integration', include: 1 }

For instance, if I have 10 integration content types and 5 integration_category content types only the integration content type records are returned and placed in the data directory.


---
:id: 4H0AaatPYQ0UYM6G6iceUI
:name: Google Adwords
:url: google-adwords
:status: Live
:category:
  :id: zbrcVl6ORwusomU6uo2iS
  :name: Advertising

That's the output I would expect. It's perfect, but I only get one of the content types back. Using the following I manage to get all of the content types, but it doesn't seem that I get any of the reference data from content types that have relationships to each other:

activate :contentful do |f|
  f.access_token  = ENV['CONTENTFUL_API']
  f.space         = { stitch: 'SPACE_ID'}
  f.cda_query     = { include: 1 }
  f.content_types = { integration: 'integration', integration_category: 'integration_category', resource: 'resource', resource_category: 'resource_category' }
end

My integration content type ends up looking like this:


---
:id: 4H0AaatPYQ0UYM6G6iceUI
:name: Google Adwords
:url: google-adwords
:status: Live
:category:
  :id: zbrcVl6ORwusomU6uo2iS

Note that the "name" field is no longer available.

From reading the documentation is seems like include: 1 should add the appropriate information to related objects, but I could be misinterpreting that. I would expect that the resulting YAML for all of my content types that have references to other content types results in something similar to the first example. What am I doing wrong here?

@dlitvakb
Copy link
Contributor

dlitvakb commented Apr 20, 2016

Hey @chuise,

The second query is good, only thing I would change is include: 1 to include: 2, that would fetch content nested 1 level deeper, which would mean resolving your links.

Try experimenting with increasing the levels of includes until you reach the amount of resolution you need. Maximum is include: 10 though 2 should suffice.

Hope this helps

@dlitvakb dlitvakb self-assigned this Apr 20, 2016
@chuise
Copy link
Author

chuise commented Apr 21, 2016

Hey @dlitvakb. Thanks for the response. I've played around with various include depths (all the way to 10), but when running this query:

activate :contentful do |f|
  f.access_token  = ENV['CONTENTFUL_API']
  f.space         = { stitch: 'SPACE_ID'}
  f.cda_query     = { include: 2 }
  f.content_types = { integration: 'integration', integration_category: 'integration_category', resource: 'resource', resource_category: 'resource_category' }
end

I still end up with something like:

---
:id: 4H0AaatPYQ0UYM6G6iceUI
:name: Google Adwords
:url: google-adwords
:status: Live
:category:
  :id: zbrcVl6ORwusomU6uo2iS

It's clearly pulling in some of the integration_category data as category. The ID is showing up, but I would have expected more data to be present. What I would expect to be returned is:

---
:id: 4H0AaatPYQ0UYM6G6iceUI
:name: Google Adwords
:url: google-adwords
:status: Live
:category:
  :id: zbrcVl6ORwusomU6uo2iS
  :name: Advertising
  :description: Integrations for advertising apps.

Is include only supposed to bring back the id of the linked content or all of the content?

Thanks.

@timcreatewell
Copy link

Hi @dlitvakb , I've got the same question as @chuise except I'm playing around with the experimental middleman v4 branch... Keen to hear your thoughts on this one though!

@dlitvakb
Copy link
Contributor

Hey @chuise & @timcreatewell,

In both cases the include is supposed to be including the contents of the references.

Make sure that if you are not using the Preview API, all content is published.

You can also debug the query yourself in the console by doing:

require 'contentful'

client = Contentful::Client.new(
  space: 'your_space_id',
  access_token: 'your_access_token',
  dynamic_entries: :auto
)

client.entries(include: 2)

Make sure that returns what you expect, if not, please ping me back again.

Also, please take a look at the examples repository, which is making use of nested relationships here: https://github.com/contentful/contentful_middleman_examples/tree/master/examples/multiple_content_types

Cheers

@chuise
Copy link
Author

chuise commented Apr 22, 2016

Hey @dlitvakb,

Running the code you supplied in the console yields:

#<Contentful::Array: @properties={:total=>3, :limit=>100, :skip=>0, :items=>[#<Contentful::DynamicEntry[resource_category]: @fields={:name=>"Whitepapers"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"OMITTED"}>, :id=>"5SmbJmGwCWYi8wYegcAu0Q", :type=>"Entry", :createdAt=>#<DateTime: 2016-04-20T12:58:15+00:00 ((2457499j,46695s,516000000n),+0s,2299161j)>, :updatedAt=>#<DateTime: 2016-04-20T12:58:15+00:00 ((2457499j,46695s,516000000n),+0s,2299161j)>, :revision=>1, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"resource_category"}>, :locale=>"en-US"}>, #<Contentful::DynamicEntry[integration_category]: @fields={:name=>"Advertising"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"OMITTED"}>, :id=>"zbrcVl6ORwusomU6uo2iS", :type=>"Entry", :createdAt=>#<DateTime: 2016-04-20T12:49:48+00:00 ((2457499j,46188s,233000000n),+0s,2299161j)>, :updatedAt=>#<DateTime: 2016-04-20T12:49:48+00:00 ((2457499j,46188s,233000000n),+0s,2299161j)>, :revision=>1, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"integration_category"}>, :locale=>"en-US"}>, #<Contentful::DynamicEntry[integration]: @fields={:name=>"Google Adwords", :url=>"google-adwords", :description=>"Google AdWords is the home for all of your ad spend data. RJMetrics connects to your Google AdWords account and replicates your data to our warehouse so that you can start combining your ad data with revenue data to take your marketing ROI calculations to the next level.", :status=>"Live", :category=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Entry", :id=>"zbrcVl6ORwusomU6uo2iS"}>} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"OMITTED"}>, :id=>"4H0AaatPYQ0UYM6G6iceUI", :type=>"Entry", :createdAt=>#<DateTime: 2016-04-20T12:39:36+00:00 ((2457499j,45576s,457000000n),+0s,2299161j)>, :updatedAt=>#<DateTime: 2016-04-20T12:52:47+00:00 ((2457499j,46367s,613000000n),+0s,2299161j)>, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"integration"}>, :locale=>"en-US"}>]} @sys={:type=>"Array"}> 

It looks like it's pulling down the integration_category and the integration and it's linking the integration_category as category, but it doesn't seem like the full integration_category content is pulled down in the integration record.

I'm attempting to access the category by:

<%= integration.category.name %>

Is the issue with what's being returned or how I'm attempting to access this? Also, we're not using the Preview API.

Thanks again for the assist on this.

@chuise
Copy link
Author

chuise commented Apr 22, 2016

This is one of the oddest things I've encountered... Our integration content type included an image field. In my test entry I left this blank since it was not required. For the sake of continuing testing I added the image and then middleman contentful. Everything magically works. I didn't change a single thing, other than add content to a non-required field. Not only did linked content start showing up for my integration content type, but also my resource content type. Not sure why this would have happened. All of the content was previously published and literally nothing change except adding that image. I'm baffled, but it works. Thanks for the help @dlitvakb.

@dlitvakb
Copy link
Contributor

Hehe! 😅 Glad to be of Help! 👍

No idea how that could be related, if you see this behaviour repeating, can you open a support ticket? This might be something API related that we haven't found.

Cheers

@chuise
Copy link
Author

chuise commented Apr 22, 2016

UPDATE

@dlitvakb you posted your last comment as I was typing this so I apologize.

I ran a little test to see if the image was the culprit and here is what happened.

  1. Removed all of my Integration entries
  2. Removed the image from integration content type and saved
  3. Created a new Integration entry and published
  4. middleman contentful

All linked content disappears from the Integration entry:

---
:id: 6DQqA1ycQEKeIGMkMMcwOu
:name: Twitter Ads
:url: twitter-ads
:description: Some stuff goes here.
:status: Live
:integration_category:
  :id: zbrcVl6ORwusomU6uo2iS

Just for the hell of it I added back the image field to the integration content type. Updated the entry with an image, published and pulled into middleman and everything works as expected:

---
:id: 6DQqA1ycQEKeIGMkMMcwOu
:name: Twitter Ads
:url: twitter-ads
:description: Some stuff goes here.
:status: Live
:integration_category:
  :id: zbrcVl6ORwusomU6uo2iS
  :name: Advertising
:logo:
  :title: Twitter Logo
  :url: "//assets.contentful.com/k49d63tr8kcn/6zihTYp3A4Mo848gEw8oAq/5c8674380cbd86975721504a7d42ed82/twitter-color-01.svg"

Very odd behavior. Luckily at least one of our content types contains an image 👍 . I can open a ticket on the API.

@dlitvakb
Copy link
Contributor

Hey @chuise,

Thanks for the detailed explanation

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants