Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hsimah/999dfe74928384313bd758bcb9d5db1c to your computer and use it in GitHub Desktop.
Save hsimah/999dfe74928384313bd758bcb9d5db1c to your computer and use it in GitHub Desktop.
wpgraphql_register_custom_mutation_inputs.php
<?php
$graphql_singular_name = 'Post';
$graphql_field_type = 'String';
$field_config = [
'name' => 'The human readable ACF field name',
'graphql_name' => 'The GraphQL field name, eg first_name',
];
add_filter('graphql_input_fields', function ($fields, $type_name) use ($graphql_singular_name, $graphql_field_type, $field_config) {
if ($type_name === "Create{$graphql_singular_name}Input" || $type_name === "Update{$graphql_singular_name}Input") {
// add field as input untested with non-scalar fields
['name' => $name, 'graphql_name' => $graphql_name] = $field_config;
$fields[$graphql_name] = [
'type' => $graphql_field_type,
'description' => $name,
];
}
return $fields;
}, 10, 2);
add_action('graphql_post_object_mutation_update_additional_data', function (int $post_id, array $input) use ($field_config) {
// update the meta value
['id' => $id, 'graphql_name' => $graphql_name] = $field_config;
rwmb_set_meta($post_id, $id, $input[$graphql_name]);
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment