Skip to content

Instantly share code, notes, and snippets.

@kaelri
Last active May 7, 2021 18:15
Show Gist options
  • Select an option

  • Save kaelri/c4babb96ce742b0ccd9260bacf4fe742 to your computer and use it in GitHub Desktop.

Select an option

Save kaelri/c4babb96ce742b0ccd9260bacf4fe742 to your computer and use it in GitHub Desktop.
<?php
namespace RunThroughHistory\WPGraphQL\Type\Object;
use WPGraphQL\Model\User as UserModel;
use RunThroughHistory\Interfaces\Hookable;
class User implements Hookable {
public function register_hooks() {
add_action( 'graphql_register_types', [ $this, 'register_fields' ] );
}
public function register_fields() {
register_graphql_fields( 'User', [
'prefersColorScheme' => [
'type' => 'String',
'description' => __( 'User\'s preferred color theme', 'run-through-history' ),
'resolve' => function( UserModel $user ) {
return wp_get_user_meta( $user->fields['userId'], 'prefers_color_scheme' );
},
],
] );
}
}
<?php
namespace RunThroughHistory\WPGraphQL\Mutation;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use RunThroughHistory\Interfaces\Hookable;
class UserUpdate implements Hookable {
public function __construct() {
}
public function register_hooks() {
add_action( 'graphql_register_types', [ $this, 'register_input_fields'] );
add_action( 'graphql_before_resolve_field', [ $this, 'validate_fields' ], 10, 7 );
add_action( 'graphql_user_object_mutation_update_additional_data', [ $this, 'save_additional_data' ], 10, 3 );
}
public function register_input_fields() {
register_graphql_fields( 'UpdateUserInput', [
'prefersColorScheme' => [
'type' => [ 'non_null' => 'String' ],
'description' => __( 'User\'s preferred color theme', 'run-through-history' ),
],
] );
}
/**
* Fire an action BEFORE the field resolves
*
* @param mixed $source Source passed down the Resolve Tree.
* @param array $args Args for the field.
* @param AppContext $context AppContext passed down the ResolveTree.
* @param ResolveInfo $info ResolveInfo passed down the ResolveTree.
* @param mixed $field_resolver Field resolver.
* @param string $type_name Name of the type the fields belong to.
* @param string $field_key Name of the field.
* @param FieldDefinition $field Field Definition for the resolving field.
*/
public function validate_fields( $source, array $args, AppContext $context, ResolveInfo $info, $field_resolver, string $type_name, string $field_key ) : void {
if ( 'RootMutation' !== $type_name || ! $this->is_update_user_mutation( $field_key ) ) {
return;
}
if ( ! $this->is_preferred_color_scheme_valid( $args ) ) {
throw new UserError( 'An invalid color scheme name was provided. Accepted values are \'light\' or \'dark\'.' );
}
}
private function is_preferred_color_scheme_valid( array $args ) : bool {
return in_array( $args['input']['prefersColorScheme'], [ 'light', 'dark' ] );
}
/**
* @param int $user_id The ID of the user being mutated.
* @param array $input The input for the mutation.
* @param string $mutation_name The name of the mutation (ex: create, update, delete).
*/
public function save_additional_data( int $user_id, array $input, string $mutation_name ) : void {
if ( ! $this->is_update_user_mutation( $mutation_name ) ) {
return;
}
$prefers_color_scheme_sanitized = sanitize_text_field( $input['prefersColorScheme'] );
update_user_meta( $user_id, 'prefers_color_scheme', $prefers_color_scheme_sanitized );
}
private function is_update_user_mutation( string $mutation_name ) : bool {
return 'updateUser' === $mutation_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment