0

Is it possible to get categories with related entries with one graphQL query?

I have the following query:

query MyQuery {
  categories(relatedToEntries: {section: "entryType"}) {
    id
    title
  }
}

Giving me this result:

{
  "data": {
    "categories": [
      {
        "id": "253",
        "title": "Category One"
      },
      {
        "id": "257",
        "title": "Category Two"
      }
    ]
  }
}

I would like something like the following:

{
  "data": {
    "categories": [
      {
        "id": "253",
        "title": "Category One",
        "entries": [
           {
              "id": 1,
              "title": "Title"
         }
      },
      {
        "id": "257",
        "title": "Category Two",
        "entries": [
           {
              "id": 1,
              "title": "Title"
         }
    ]
  }


}

 

1 Answer 1

-1

Perhaps something like this?

I don't know the structure of your data but this would be my best 'educated guess'.

I renamed the different ID's to make it a little more clear in the response.

query MyQuery {
  categories(relatedToEntries: {section: "entryType"}) {
    categoryId: id
    title
    entries(relatedToCategories: categoryId) {
      entryId: id,
      title
    }
  }
}
4
  • This does not seem to work. If i put a similar entries query below categories with an hard-code ID i get results, but it does not produce results or errors when located inside the categories query.
    – Jørgen
    Apr 26, 2022 at 13:41
  • That would suggest that categoryId is not actually passed into the nested entries() query. I'm... not sure why tbh. Apr 27, 2022 at 8:59
  • Does not work with an hard-coded ID inside the categories query either.. it looks like GQL is basically ignoring the entire entries() query when inside categories()
    – Jørgen
    Apr 27, 2022 at 9:09
  • I'm starting to wonder how your GraphQL is set up. I think you need to start there and look it over. Following the the docs over at craftcms.com/docs/3.x/graphql.html, my solution looks correct. Apr 28, 2022 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.