zngly / wp-graphql-acf-mutations

WP GraphQl ACF Mutations Plugin
Other
7 stars 1 forks source link

Does this work for creating items? #8

Closed james-ingold closed 2 years ago

james-ingold commented 2 years ago

Hey thanks for creating this great library. I was able to update a custom post type with a mutation and custom fields but it doesn't seem like it works for creating items. Does this plugin only work for using an update mutation?

I've got a custom post type => add_action('init', function () { register_post_type('item', [ 'public' => true, 'label' => 'Items', 'show_in_graphql' => true, 'graphql_single_name' => 'Item', 'graphql_plural_name' => 'Items' ]); });

The update mutation updateItem allows me to update the custom fields on this type but createItem doesn't add the post correctly. The post is added but the custom fields aren't in the database.

zngly-vlad commented 2 years ago

@james-ingold

Hello, yes. The acf-meta values should also be available for creating and updating. Can you elaborate more on the issue you are facing by providing the acf fields code as well :)

james-ingold commented 2 years ago

Hello, thanks for the reply! Sorry I'm a bit of a wordpress noob so I might be overlooking something simple. What do you mean by acf fields code? I might be missing that part. I have a custom post type and then I added acf fields to it in the wordpress admin panel and then added this plugin. If I have an item created already, I can use the updateItem mutation to update the acf fields and that works. However if I use the createItem mutation, none of those acf fields are saved and the item doesn't appear in the Items list in wordpress. I see the item in the wp_posts table with a status of available but there are no rows for the meta fields for that item, I can see rows for the meta data for the one that was created from the wordpress admin panel.

zngly-vlad commented 2 years ago

@james-ingold

I have just verified using the examples below and it does work. One point to note is to use status: PUBLISH in order to see it using a simple query.

As to why you cant see it in the postmeta, finding the post meta for a specific post_id. this will filter it :)

Hope this helps

register_post_type(
        "github_test",
        [
            "label" => __("Github Test", "github-test"),
            "public" => true,
            "publicly_queryable" => true,
            "show_in_graphql" => true,
            "graphql_single_name" => "gTest",
            "graphql_plural_name" => "gTests"
        ]
    );
acf_add_local_field_group(array(
    'key' => 'group_627a40880bc1e',
    'title' => 'Github_Issue_Test',
    'fields' => array(
        array(
            'key' => 'field_6357a5685c42a',
            'label' => 'extra name',
            'name' => 'extra_name',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'show_in_graphql' => 1,
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'github_test',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'show_in_rest' => 0,
    'show_in_graphql' => 1,
    'graphql_field_name' => 'github_meta_fields',
    'map_graphql_types_from_location_rules' => 0,
    'graphql_types' => '',
));
query MyQuery3 {
  gTests {
    nodes {
      title
      github_meta_fields {
        extraName
      }
    }
  }
}

mutation GithubTestCreate {
  createGTest(input: {status: PUBLISH, title: "test1", extraName: "extraName1"}) {
    gTest {
      github_meta_fields {
        extraName
      }
    }
  }
}
james-ingold commented 2 years ago

Oh my gosh, I think the issue was that I had a custom field named statuscausing the issue or maybe it was just posting as a PUBLISH status but I am good now, it is working. Thank you so much for your help!