Last active
October 19, 2021 14:37
-
-
Save kellenmace/21ac4723d6147e6e67349d17b7eac011 to your computer and use it in GitHub Desktop.
WPGraphQL User Meta Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyCoolApp; | |
use WPGraphQL\Model\User; | |
class UserFields { | |
public function register_hooks() { | |
add_action( 'graphql_register_types', [ $this, 'register_fields' ] ); | |
} | |
public function register_fields() { | |
register_graphql_fields( 'User', [ | |
'favoriteColor' => [ | |
'type' => 'String', | |
'description' => __( 'User\'s favorite color', 'my-cool-app' ), | |
'resolve' => function( User $user ) { | |
$favorite_color = get_user_meta( $user->fields['userId'], 'favorite_color', true ); | |
return $favorite_color ?: null; | |
}, | |
], | |
'favoriteFood' => [ | |
'type' => 'String', | |
'description' => __('User\'s favorite food', 'my-cool-app'), | |
'resolve' => function ( User $user ) { | |
$favorite_food = get_user_meta( $user->fields['userId'], 'favorite_food', true ); | |
return $favorite_food ?: null; | |
}, | |
], | |
] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment