wp-graphql / wpgraphql-acf

Re-architecture of WPGraphQL for ACF
GNU General Public License v3.0
73 stars 10 forks source link

Fields inside repeater return null and/or empty arrays: Link, File and Post Object #208

Open gnathang opened 1 month ago

gnathang commented 1 month ago

Description

Hiya, I'm using Next.js and WPGraphQL/Apollo in a headless WP system using Colby Faycock's great starter.

I think this issue has already been flagged, but I think that nested Link, File and Post Object field types return null, or produce empty edges and nodes arrays, in the case of Post Object.

With the Link field it seems that the field name is being renamed 'pageLink' every time. (In my fields, this field is named as slideLink). All other ACF field types I've used so far seem to work fine.

An example ACF structure, if it helps: Flex (field group) > Flex (flexible content) > Text Slides (flexible content layout) > Slides (repeater) > SlideLink (link field).

I'm not sure if the 'make non-null' option has anything to do this this or not!

Here's some screenshots in GraphiQL IDE in the order Link, File and Post Object (with console log).

Thank you!

empty link field empty file field empty arrays from post object

empty array from post object - console

Steps to reproduce

  1. upload the field group 'flex' from the JSON file attached below
  2. choose the 'Text Slides' layout to test the slideLink (link) fields inside the 'slides' repeater. (please note in this example, the slideLink field name always changes to pageLink in the schema, hence I have used it here.
  3. choose the 'Video' layout to test the videoFileUpload (file) field.
  4. choose the 'Wayfinder' flex to test the pagePost (post object) field.
  5. query these using the pictures like this: empty link field
empty file field empty arrays from post object

here's the query i'm using in my next.js app:

query PageByUri($uri: ID!) { page(id: $uri, idType: URI) { flex { fieldGroupName flex { ... on FlexFlexTextSlidesLayout { fieldGroupName layout thisIsALink { target url title } slides { bigTitle smallTitle textBody video { cursor node { sourceUrl(size: BANNER) srcSet(size: BANNER) uri sizes(size: BANNER) } } image { cursor node { altText sizes(size: BANNER) sourceUrl(size: BANNER) srcSet(size: BANNER) uri } } } } ... on FlexFlexWayfinderLayout { fieldGroupName introText layout smallTitle title link { target title url } signposts { fieldGroupName pagePost { edges { cursor } nodes { ... on Post { id featuredImage { cursor node { altText srcSet(size: BANNER) sourceUrl(size: BANNER) } } title(format: RAW) postId link } slug uri ... on Page { featuredImage { cursor node { altText sourceUrl(size: BANNER) srcSet(size: BANNER) uri } } content(format: RAW) id } } } } } } } } } __

PHP or JSON export of the ACF Field Group(s)

acf-export-2024-06-03.json

Additional context

No response

WPGraphQL Version

Version 1.26.0

WPGraphQL For ACF Version

Version 2.2.0

ACF (Advanced Custom Fields) Version. Free or Pro?

7.3.0.1, Pro

WordPress Version

6.5.3

PHP Version

8.4 (tried on 8.0 too, but didn't work!)

Additional enviornment details

No response

Please confirm that you have searched existing issues in the repo.

Please confirm that you have disabled ALL plugins except for WPGraphQL, WPGraphQL For ACF, ACF, etc.

gnathang commented 1 month ago

please let me know what other info you need for the reproduction :)

ttstauss commented 1 month ago

This issue is holding us back from migrating to the new plugin. Here's a very minimal reproduction if that's helpful (our implementation is via blocks but seems to still be an issue with a normal acf field group which this includes): https://www.dropbox.com/scl/fi/llfjlg1vm0akppue0ulpy/ACF-Repeater-Fields.zip?rlkey=g7z0t11zu78on7lq52puf1g30&st=43twx36g&dl=0 (this is a zip generated from LocalWP). The username and password is test.

jasonbahl commented 1 month ago

@gnathang when I tried to reproduce, I started by importing your ACF Field Group, but I get the following debug message:

The ACF Field or Field Group could not be added to the schema. GraphQL Field and Type names cannot start with a number

          "key": "layout_620295a0f3b18",
          "name": "2_col_text",
          "label": "2 Column Text",

I will fix that field group config and try again, but wanted to point that out.

jasonbahl commented 1 month ago

@gnathang after updating the name of the layout to "two_col_text" I then saved data in the flex field:

CleanShot 2024-06-10 at 11 09 29@2x

And was able to query and see results:

CleanShot 2024-06-10 at 11 09 18@2x


I tried to reproduce with the following:

jasonbahl commented 1 month ago

@ttstauss it looks like the issue with the "Page Link" field is different than the issue opened here.

It looks like WPGraphQL for ACF is mistakenly mapping the PageLink field type to a connection, but that's not actually how the ACF Page Link Field is intended to be used. see: https://www.advancedcustomfields.com/resources/page-link/#template-usage

The page link field is intended to return a link (string) and not an object representing the page.

I think we should open a separate issue to fix the Type mapping for the ACF "Page Link" field type as it should return a string and not a Connection as it currently does, as ACF does not return data that can be used to resolve a connection, instead it returns a url.

jasonbahl commented 1 month ago

@ttstauss I opened a new issue related to the PageLink field specifically: https://github.com/wp-graphql/wpgraphql-acf/issues/213

ttstauss commented 1 month ago

Thanks, @jasonbahl! That makes total sense.

gnathang commented 1 month ago

thanks both! will try this out now.

is it this same problem that makes the file and post object field types return null also?

and does it have anything to do with being inside a repeater? because elsewhere it seems to work fine!

jasonbahl commented 1 month ago

is it this same problem that makes the file and post object field types return null also?

@gnathang can you elaborate? fields on repeaters appear to be working as expected for me:

I can add data to a repeater within a flex field using the ACF Field Group export you shared:

Image

And I can query and get values, as expected, for the fields:

Image

If you're getting null values, I would recommend:

if there's an issue with file and post object field types, it would be good to provide an isolated reproduction in a new issue. Can you reproduce it with just WPGraphQL + ACF + WPGraphQL for ACF active and nothing else? Does it happen with just a simple field group or only with more complex field group setups? For example, I've seen some issues arise because different field groups have the same graphql_field_name as another field group and things get weird.

gnathang commented 3 weeks ago

Sorry for the slow reply. I have now returned some values for the Page Link field! Having trouble with null results elsewhere, but will reach out to Discord and discussions first. Thank you all for your help!

josephfusco commented 2 weeks ago

Hey @gnathang, if you can expand on the troubles you are having, we would love to see if we can get this resolved. The more info you can share the better 🙌🏼