我尝试使用 WPGraphQL 向用户添加自定义用户字段。因此,我尝试在官方 WPGraphQL 文档https://docs.wpgraphql.com/extending/fields/#register-fields-to-the-schema中重新创建示例:
add_action('graphql_init', function () {
$hobbies = [
'type' => ['list_of' => 'String'],
'description' => __('Custom field for user mutations', 'your-textdomain'),
'resolve' => function ($user) {
$hobbies = get_user_meta($user->userId, 'hobbies', true);
return !empty($hobbies) ? $hobbies : [];
},
];
register_graphql_field('User', 'hobbies', $hobbies);
register_graphql_field('CreateUserInput', 'hobbies', $hobbies);
register_graphql_field('UpdateUserInput', 'hobbies', $hobbies);
});
我已经将类型从 更改\WPGraphQL\Types::list_of( \WPGraphQL\Types::string() )
为['list_of' => 'String']
。
如果我现在执行 updateUser 突变,我的爱好不会得到更新。我做错了什么?
突变:
mutation MyMutation {
__typename
updateUser(input: {clientMutationId: "tempId", id: "dXNlcjox", hobbies: ["football", "gaming"]}) {
clientMutationId
user {
hobbies
}
}
}
输出:
{
"data": {
"__typename": "RootMutation",
"updateUser": {
"clientMutationId": "tempId",
"user": {
"hobbies": []
}
}
}
}