spicywebau / craft-neo

A Matrix-like field type for Craft CMS that uses existing fields
Other
403 stars 63 forks source link

How to approach eager-loading in Craft 5 #872

Closed Mosnar closed 2 months ago

Mosnar commented 3 months ago

What question would you like to ask?

When migrating to Craft 5, I ran into some issues with eager-loaded blocks, which makes sense since eagerloading is quite different in Craft 5. I tried using "eagerly" with load relations on Neo blocks, but didn't have any luck. Can we get some updated documentation on how to handle eagerloading going forward?

Mosnar commented 3 months ago

Upon closer inspection, this feel like a bug to me. This errors keeps cropping up when trying to eagerload things:

Too few arguments to function craft\base\Element::setEagerLoadedElements(), 2 passed in /var/www/html/cms/vendor/spicyweb/craft-neo/src/elements/Block.php on line 529 and exactly 3 expected
ttempleton commented 3 months ago

Yes, this is a bug. I'll dig into it when I have time and make sure we're supporting eager loading in Craft 5 properly.

josephfernicola commented 2 months ago

I'm also getting that same error message when trying to fetch an Image asset field within a Neo block, using the GraphQL API. I'm currently using Neo version 5.0.0-beta.8 and Craft 5 headless mode. This was my query:

fragment seo on Ether_SeoData {
  title
  description
  social {
    twitter {
      title
      description
      image {
        url
      }
    }
    facebook {
      title
      description
      image {
        url
      }
    }
  }
}

query HomePageQuery {
  page: entry(section: "home") {
    id
    title
    url
    dateCreated @formatDateTime(format: "Y-m-d")
    dateUpdated @formatDateTime(format: "Y-m-d")
    __typename
    ... on home_Entry {
      description
      images {
        url
        title
      }
      textLink {
        ... on HyperLinkInterface {
          text
          url
        }
      }
      seo {
        ...seo
      }
      blocks {
        __typename
        ... on blocks_contactForm_BlockType {
          __typename
          textTitle
          description
          image {
            url
            title
          }
        }
      }
    }
  }
  services: entries(section: "service") {
    title
    slug
    ... on service_Entry {
      category {
        slug
      }
    }
  }
}

When I remove the image field within the Contact Form block from the query, the response is successful. But when I keep it in I get the error:

Too few arguments to function craft\\base\\Element::setEagerLoadedElements(), 2 passed in /var/www/html/vendor/spicyweb/craft-neo/src/elements/Block.php on line 529 and exactly 3 expected
ttempleton commented 2 months ago

This is fixed now in 5.0.0-beta.9.

A note regarding eagerly() - the Craft documentation mentions that native attributes don't currently support eagerly(), and this includes element type attributes like children. So you will still need to perform your initial load of Neo blocks from a field with the useMemoized() loop as you do now, and avoid using eagerly() on child block queries or other 'block relative' queries.