Register a basic Mutation

This snippet shows how to register a basic GraphQL Mutation with a single input field, a single output field, and the input is simply returned as the value for the output.

add_action( 'graphql_register_types', function() {

	register_graphql_mutation( 'testMutation', [
		'inputFields' => [
			'phoneNumber' => [
				'type' => [ 'non_null' => 'String' ],
			],
		],
		'outputFields' => [
			'phoneNumber' => [
				'type' => 'String',
			],
		],
		'mutateAndGetPayload' => function( $input ) {

			return [
				'phoneNumber' => $input['phoneNumber'] ?? null,
			];

		}
	]);

} );

Published by Jason Bahl

Jason is a Principal Software Engineer at WP Engine based in Denver, CO where he maintains WPGraphQL. When he's not writing code or evangelizing about GraphQL to the world, he enjoys escaping from escape rooms, playing soccer, board games and Fortnite.