wp-graphql-acf icon indicating copy to clipboard operation
wp-graphql-acf copied to clipboard

mutation createPost don't support acf input field?

Open shipcake opened this issue 4 years ago • 6 comments

Hi. I use Wpgraphql and i have problem. how to defind custom acf type in input field?

shipcake avatar Feb 15 '21 09:02 shipcake

I have a same question too. Can't find any info. @jasonbahl seems that creation and update is not supported by this plugin, isn't it?

Yuribtr avatar Feb 24 '21 22:02 Yuribtr

Hey I have an example repository here wp-acf-mutations. Basically you need to run the filters to add the input fields to the schema based on your acf fields.

saleebm avatar Feb 25 '21 00:02 saleebm

Hi @saleebm, many thanks for pointing me out. If ACF and CPT UI is present the problem solves in few lines of code in functions.php. It is amazing!

use WPGraphQL\AppContext;
use WPGraphQL\Model\User;

add_action( 'graphql_input_fields', function ( $fields, $type_name, $config ) {	
	if ( $type_name === 'UpdateArticlesInput') {
		$fields = array_merge( $fields, [		
			'name' => [ 'type' => 'String' ],
		] );
	}

	return $fields;
}, 20, 3 );

add_action( 'graphql_post_object_mutation_update_additional_data', function ( $post_id, $input, $mutation_name, $context, $info ) {
	if ( isset( $input['name'] ) ) {
		update_field('field_6036d0504e27d', $input['name'], $post_id);
	}
}, 10, 5 ); 

Yuribtr avatar Feb 25 '21 22:02 Yuribtr

Hello! Mutations are not currently on the near-term roadmap for this plugin. We do hope to add support in the future, but I don't know when. I've labeled this "Future Feature" and moved it to the "ACF Mutations" milestone for future consideration.

jasonbahl avatar Mar 05 '21 21:03 jasonbahl

Hi @saleebm, many thanks for pointing me out. If ACF and CPT UI is present the problem solves in few lines of code in functions.php. It is amazing!

use WPGraphQL\AppContext;
use WPGraphQL\Model\User;

add_action( 'graphql_input_fields', function ( $fields, $type_name, $config ) {	
	if ( $type_name === 'UpdateArticlesInput') {
		$fields = array_merge( $fields, [		
			'name' => [ 'type' => 'String' ],
		] );
	}

	return $fields;
}, 20, 3 );

add_action( 'graphql_post_object_mutation_update_additional_data', function ( $post_id, $input, $mutation_name, $context, $info ) {
	if ( isset( $input['name'] ) ) {
		update_field('field_6036d0504e27d', $input['name'], $post_id);
	}
}, 10, 5 ); 

Where do you find the field_6036d0504e27d in the database?

nagman avatar Jun 29 '22 17:06 nagman

Ok I found it => in the wp_postmeta table, search for _your_acf_field_slug (add the _ at the beginning) in the meta_key column, and take the corresponding meta_value.

Thanks a lot @Yuribtr! You solved my dead end!

nagman avatar Jun 29 '22 17:06 nagman

👋🏻 We are re-building WPGraphQL for ACF over here: https://github.com/wp-graphql/wpgraphql-acf.

I've created a new issue to track mutation support (and explain some current blockers). see: https://github.com/wp-graphql/wpgraphql-acf/issues/109

I'm going to close this in favor of tracking it in the new repo.

jasonbahl avatar Nov 01 '23 15:11 jasonbahl