graphql mutation in wpgraphql
mutation MyMutation {
registerUser(
input: {username: "abacus", email: "abacus@gmail.com", password: "abacus123", firstName: "abacusjames", lastName: "nito"}
) {
user {
auth {
authToken
refreshToken
authTokenExpiration
refreshTokenExpiration
}
id
roles {
nodes {
name
}
}
}
}
}
===========================================
contact form 7 submission with rate limiter ===>
add_action('rest_api_init', function () {
register_rest_route('wp/v2', '/cf7/submit', array(
'methods' => 'POST',
'callback' => 'mcbot_cf7_rest_submit',
'permission_callback' => '__return_true',
'args' => array(
'form_id' => array(
'required' => true,
'type' => 'integer',
'description' => 'Contact Form 7 form ID'
),
'name' => array(
'required' => true,
'type' => 'string',
'description' => 'Sender name'
),
'email' => array(
'required' => true,
'type' => 'string',
'description' => 'Sender email'
),
'phone' => array(
'required' => false,
'type' => 'string',
'description' => 'Phone number'
),
'message' => array(
'required' => false,
'type' => 'string',
'description' => 'Message content'
),
)
));
});
function mcbot_cf7_rest_submit(WP_REST_Request $request)
{
$form_id = $request->get_param('form_id');
$contact_form = WPCF7_ContactForm::get_instance($form_id);
if (!$contact_form) {
return new WP_REST_Response([
'status' => 'error',
'message' => 'Invalid form ID'
], 404);
}
// Extract only allowed params
$allowed = [
'name',
'email',
'phone',
'message'
];
$data = [];
foreach ($allowed as $field) {
if ($request->get_param($field)) {
$data[$field] = sanitize_text_field($request->get_param($field));
}
}
// Inject into $_POST to simulate CF7 submission
foreach ($data as $key => $val) {
$_POST[$key] = $val;
}
$result = $contact_form->submit();
return new WP_REST_Response([
'status' => $result['status'],
'message' => $result['message'],
'invalid_fields' => $result['invalid_fields'] ?? []
], 200);
}