spicywebau / craft-neo

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

Asset field is still returning empty when using GraphQL Query #534

Closed market-south closed 2 years ago

market-south commented 2 years ago

Description

In addition to issue #519, the graphQL query is still returning an empty asset field, whereas the asset field outside of the Neo block is returning an array of asset data(please refer below query)

As per the comment on Nov 18, 2021 author says naming the asset handle without underscore works for him. In my case, the asset handle is in came case "sectionImages", yet the array is empty.

Steps to reproduce

  1. Setup a neo field with a Neo field block
  2. Name the handle in camelCase or with underScore
  3. Add an asset field to the block
  4. Query the field with the block inside an entry and try to request the image field (response below)
  5. When you add the field outside of the neo block (landingImage field) and query it works fine. (response below)

Other information

Full Request Query

 {
  entries(section: "ourStory") {
    id
    title
    pageHeading
    introductionParagraph
    introductionSlogan
    landingImage {
      id
      url
      alt: title
    }
    body: bodyBuilder {
      sectionId
      sectionTitle
      sectionHeading
      sectionDescription
      sectionImages {
        id
        url
        alt: title
      }
      sectionLink
      backgroundColour
      sectionLayout
      sectionLayoutWidth
      sectionPadding
      masonryImages
    }
  }
}

Full Response

{
  "data": {
    "entries": [
      {
        "id": "3",
        "title": "Our Story",
        "pageHeading": "Blue River Dairy Story",
        "introductionParagraph": "<p>Duis leo tellus, egestas nec egestas ac, viverra in dui praesent maximus lacus in dolor faucibus.</p>",
        "introductionSlogan": "<p>Praesent varius, risus a ullamcorper commodo, augue felis ultrices ligula, viverra viverra ex sem ullamcorper sapien. Nulla lectus sapien, efficitur quis enim vitae, lacinia finibus nisl.</p>",
        "landingImage": [
          {
            "id": "27",
            "url": "https://admin.blueriverdairy.co.nz/images/ourstory.png",
            "alt": "Ourstory"
          }
        ],
        "body": [
          {
            "sectionId": "who_we_are",
            "sectionTitle": "Who we are",
            "sectionHeading": "heading test",
            "sectionDescription": "<p>Praesent varius, risus a ullamcorper commodo, augue felis ultrices ligula, viverra viverra ex sem ullamcorper sapien. Nulla lectus sapien, efficitur quis enim vitae.</p>",
            "sectionImages": [],
            "sectionLink": "<a href=\"#\" target=\"_blank\" rel=\"noopener noreferrer\">test link</a>",
            "backgroundColour": "#ffffff",
            "sectionLayout": "imageText",
            "sectionLayoutWidth": "6040",
            "sectionPadding": false,
            "masonryImages": true
          },
          {
            "sectionId": "test",
            "sectionTitle": "test",
            "sectionHeading": "test",
            "sectionDescription": "<p>test</p>",
            "sectionImages": [],
            "sectionLink": "<a href=\"#\" target=\"_blank\" rel=\"noopener noreferrer\">test</a>",
            "backgroundColour": "#ffffff",
            "sectionLayout": "imageText",
            "sectionLayoutWidth": "5050",
            "sectionPadding": true,
            "masonryImages": false
          }
        ]
      }
    ]
  }
}

In the response, you could see the "landingImage" asset field(outside of Neo Block) is returning(id, url, title as alt) whereas "sectionImages" inside Neo block array is returning empty

ttempleton commented 2 years ago

I've retested in GraphiQL and asset fields in Neo fields do seem to be working for me.

Can you confirm if it works as expected for you with an asset field in a Matrix field?

market-south commented 2 years ago

Hi @ttempleton

Thanks for the reply.

I have tested to query the asset field in a Matrix field and the response has the array of data as expected. 1) Created a test matrix field "test" with asset field "testimg". (please refer below)

Full request

 entries(section: "ourStory") {
    id
    title
    pageHeading
    introductionParagraph
    introductionSlogan
    landingImage {
      id
      url
      alt: title
    }
    body: bodyBuilder {
      sectionId
      sectionTitle
      sectionHeading
      sectionDescription
      sectionImages {
        id
        url
        alt: title
      }
      sectionLink
      backgroundColour
      sectionLayout
      sectionLayoutWidth
      sectionPadding
      masonryImages
    }
    test 
    {
      testimg
      {
        id
        url
        title
      }
    }
  }

Full response

{
  "data": {
    "entries": [
      {
        "id": "3",
        "title": "Our Story",
        "pageHeading": "Blue River Dairy Story",
        "introductionParagraph": "<p>Duis leo tellus, egestas nec egestas ac, viverra in dui praesent maximus lacus in dolor faucibus.</p>",
        "introductionSlogan": "<p>Praesent varius, risus a ullamcorper commodo, augue felis ultrices ligula, viverra viverra ex sem ullamcorper sapien. Nulla lectus sapien, efficitur quis enim vitae, lacinia finibus nisl.</p>",
        "landingImage": [
          {
            "id": "27",
            "url": "https://admin.blueriverdairy.co.nz/images/ourstory.png",
            "alt": "Ourstory"
          }
        ],
        "body": [
          {
            "sectionId": "who_we_are",
            "sectionTitle": "Who we are",
            "sectionHeading": "heading test",
            "sectionDescription": "<p>Praesent varius, risus a ullamcorper commodo, augue felis ultrices ligula, viverra viverra ex sem ullamcorper sapien. Nulla lectus sapien, efficitur quis enim vitae.</p>",
            "sectionImages": [],
            "sectionLink": "<a href=\"#\" target=\"_blank\" rel=\"noopener noreferrer\">test link</a>",
            "backgroundColour": "#ffffff",
            "sectionLayout": "imageText",
            "sectionLayoutWidth": "6040",
            "sectionPadding": false,
            "masonryImages": true
          },
          {
            "sectionId": "test",
            "sectionTitle": "test",
            "sectionHeading": "test",
            "sectionDescription": "<p>test</p>",
            "sectionImages": [],
            "sectionLink": "<a href=\"#\" target=\"_blank\" rel=\"noopener noreferrer\">test</a>",
            "backgroundColour": "#ffffff",
            "sectionLayout": "imageText",
            "sectionLayoutWidth": "5050",
            "sectionPadding": true,
            "masonryImages": false
          }
        ],
        "test": [
          {
            "testimg": [
              {
                "id": "38",
                "url": "https://admin.blueriverdairy.co.nz/images/twocol1.png",
                "title": "Twocol1"
              }
            ]
          }
        ]
      }
    ]
  }
}
ttempleton commented 2 years ago

When running the Neo query, are you running that in GraphiQL, or if not, what platform are you using to run the query?

market-south commented 2 years ago

I am using GraphiQL explorer in Craft

ttempleton commented 2 years ago

Thanks for confirming that. If possible, could you please send your composer.json/lock and database backup to plugins@spicyweb.com.au, and we'll have a look at it.

market-south commented 2 years ago

Hi, sent an email with hightail link to the files as required.

Hope this helps

ttempleton commented 2 years ago

Thanks for sending that. I was able to reproduce this on your database, then was able to reproduce on mine... my apologies, I had been using inline fragments in my own tests, as I didn't realise it was ever possible to not use fragments to access custom fields. It turned out that when I added inline fragments to your example query and ran it outside of dev mode, the query worked as expected. In short, unfortunately the solution here is that you're going to need to use inline fragments in your query. A bit more in-depth explanation below:

https://github.com/craftcms/cms/issues/7165#issuecomment-755981068 seems to suggest that not using fragments may stop working outside of dev mode at some point in the future and is only currently allowed for performance reasons, and that it (correctly) causes an error in dev mode. Given the error in dev mode in particular, I'm interpreting that Craft issue as meaning that not using inline fragments in your query isn't supported.

I believe that the reason it works for Matrix, and doesn't work for Neo, is related to Neo using global fields as subfields whereas Matrix currently doesn't. From playing around with the Craft code that handles this, it seemed that not using inline fragments made it lose track of Matrix-nested asset fields, which then couldn't be eager loaded and had to be reloaded from the database instead, guaranteeing correct results. On the other hand, Neo-nested asset fields were kept track of, but then just didn't contain any assets since there was no inline fragment to tell it which block type's asset field to eager load.