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

Allow access to $contentfulEntries.includes #11

Closed
mismith opened this issue Sep 12, 2015 · 10 comments
Closed

Allow access to $contentfulEntries.includes #11

mismith opened this issue Sep 12, 2015 · 10 comments

Comments

@mismith
Copy link

mismith commented Sep 12, 2015

I don't know if this is a new change on contentful's part or not, but I see in the response packet from, say, a call like this https://cdn.contentful.com/spaces/XXX/entries?access_token=XXX&content_type=XXX now returns an includes property alongside items, and it would be nice to be able to access that directly, in addition to continuing to auto-populate the linked Resources

Sample response:

{
  "sys": {
    "type": "Array"
  },
  "total": 1,
  "skip": 0,
  "limit": 100,
  "items": [
    {
      "sys": {…},
      "fields": {
        "title": "…",
        "downloads": [
          {
            "sys": {
              "type": "Link",
              "linkType": "Asset",
              "id": "…"
            }
          }
        ],
      }
    },
    …
  ],
  "includes": {
    "Asset": [
      {
        "sys": {…},
        "fields": {
          "file": {…},
          "title": "…"
        }
      },
      …
    ]
  }
}
@jvandemo
Copy link
Owner

@mismith — The contentful-entries directive automatically resolves linked resources if they are included in the includes key of the Contentful response.

Behind the scenes, the directive queries the Contentful API, walks over all the linked resources in the items and resolves them with the data from the includes if present.

So the heavy lifting is already done for you automatically and you should already have all information available in the items array.

Does that make sense? Thanks!

@mismith
Copy link
Author

mismith commented Sep 14, 2015

It totally does make sense and that's exactly what I figured it was doing, but I guess there must be some bug because this is failing for me using the latest (v2.0.0) build. I'll try to throw together a plunkr showing the problem when I get a sec.

@jvandemo
Copy link
Owner

@mismith — A plnkr would be very useful indeed. Thanks in advance!

@mismith
Copy link
Author

mismith commented Sep 14, 2015

Haha, I just spent 30 mins making a plunk before realizing that the code I was trying on Friday works this morning unchanged... I figured it must've been a bug in this library but now I'm leaning more towards some kind of service outtage on contentful's side? That would also explain why the Links were being pulled from the includes property sometimes but not others. Hopefully this doesn't happen again but if it does I've got a barebones plunk ready to go for figuring things further. Sorry to bug you!

@mismith mismith closed this as completed Sep 14, 2015
@jvandemo
Copy link
Owner

@mismith — Thanks for the update.

No worries at all, feel free to re-open the issue if needed.

Have a great day!

@mismith
Copy link
Author

mismith commented Sep 14, 2015

Hmmm, it's happening again. Here's that plunk: http://plnkr.co/edit/Glk72SJH6Tn4ryqD58sp?p=preview

I notice that the raw response from contentful doesn't seem to include the includes property, but I'm not sure why not. Even explicitly calling https://cdn.contentful.com/spaces/i117p4sejxck/entries/4ZpCW2HqcE046g6u0QwKSW?access_token=f91a6fa12aeecf1b2bf88151211eb2a5f8fccaf6ecf087ac61388bc8e0ccac63&include=1 doesn't seem to make a difference.

@mismith mismith reopened this Sep 14, 2015
@jvandemo
Copy link
Owner

@mismith — Contentful does not include linked resources when querying for a single entry. It only includes linked resources when querying for multiple entries (even if only one entry is returned).

From the Contentful documentation:

A reason to query an Entry by ID instead of retrieving the Entry itself might be because you want to include resolved links.

In terms of the Contentful API:

In terms of this library

  • contentful-entry will not include linked resources
  • contentful-entries will include linked resources

As a workaround you can use the contentful-entries to only get one entry like this:

<section contentful-entries="'sys.id=some-id'">
  {{ $contentfulEntries.items[0] }}
</section>

In essence it is caused by the way the Contentful API works and not by the inner workings of this library.

Does that explain the inconsistent behavior you are experiencing?

I've used your space and access token in the links above, so make sure to remove the access token for security reasons.

@mismith
Copy link
Author

mismith commented Sep 15, 2015

That totally explains it; sorry for my oversight. Thanks for this awesome library.

@mismith mismith closed this as completed Sep 15, 2015
@jvandemo
Copy link
Owner

@mismith — No problem, thank you for your feedback. Much appreciated!

jvandemo added a commit that referenced this issue Sep 16, 2015
@jvandemo
Copy link
Owner

A link to this issue has been added to the README in case other people are wondering why they experience similar behavior.

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

2 participants