pristas-peter / wp-graphql-gutenberg-acf

Expose acf blocks through graphql
MIT License
62 stars 29 forks source link

Singular vs Plural Queries returning different ACF field data #24

Open tomstorms opened 3 years ago

tomstorms commented 3 years ago

I've created multiple ACF Blocks and noticed that the singular vs plural queries are providing different results. The singular query is providing the correct data.

I'm looking to output the dynamicContent (which contains the rendered ACF block with the data) but the ACF fields itself is returning different data.

Screen Shot 2020-11-10 at 9 22 02 am

Screen Shot 2020-11-10 at 9 22 23 am

In WordPress, I do have the following plugins installed: WP Gatsby, WP GraphiQL, WP GraphQL, WP GraphQL Gutenberg, WP GraphQL Gutenberg ACF, WPGraphQL Custom Post Type UI, WPGraphQL for Advanced Custom Fields

Happy to provide more context

bpofficial commented 3 years ago

@tomstorms how did you fix this? I've run into this same issue updating the same project right now.

Context for @pristas-peter WP Gatsby 1.0.11 WP GraphiQL 1.0.1 WP GraphQL 1.5.3 WP GraphQL Gutenberg 0.3.8 WP GraphQL Gutenberg ACF 0.3.0 (fa60321) WPGraphQL for Advanced Custom Fields 0.5.3

tomstorms commented 3 years ago

It's been awhile, but back then I found the root cause was in the ACF plugin itself.

ACF looks to have its own store. When you call get_field() from a Gutenberg block it eventually leads to /advanced-custom-fields-pro/includes/acf-value-functions.php and the acf_get_value() and/or acf_format_value() function. When it receives the block id on first run, it will store value of the blocks first post, then subsequent calls are retrieved from the store. Commenting out the store seems to resolve the issue. Someone else raised a similar bug (https://github.com/AdvancedCustomFields/acf/issues/320) but disabling the store completely worked for me.

Hope this is helpful.

unnamed

bpofficial commented 3 years ago

@tomstorms you're a legend. I'm gonna leave a comment on the plugin for future devs 😂

jeanfredrik commented 2 years ago

I solved this issue in my project by adding these filters in an mu-plugin:

<?php

use WPGraphQLGutenberg\Blocks\Block;

add_action("acf/init", function () {
  add_filter(
    "graphql_acf_get_root_id",
    function ($id, $root) {
      if ($root instanceof Block) {
        $block_data = $root["attributes"]["data"];
        $block_id = $root["attributes"]["id"];
        $store = acf_get_store("values");
        foreach (array_keys($block_data) as $field_name) {
          // Reset the cached values. ACF doesn’t check metadata if there’s
          // already a cached value in the store
          $store->remove("$block_id:$field_name");
        }
      }
      // We don’t toutch the ID here. The filter in the main plugin does that.
      return $id;
    },
    9, // Before the filter in the main plugin
    2
  );

  // This is not really necessary, but feels like good practice to clean-up
  // after we’re done. This filter runs after `get_field`.
  add_filter(
    "graphql_acf_field_value",
    function ($value, $acf_field, $root, $id) {
      if ($root instanceof Block) {
        $block_id = $root["attributes"]["id"];
        acf_reset_meta($block_id);
      }
      return $value;
    },
    10,
    4
  );
});

I can make PR with these fixes if that’s of interest?