Add Edit Link to All Post Types

The following snippet shows how to add the “Edit” link as a GraphQL field to all post types:

add_action( 'graphql_register_types', function() {

	register_graphql_field( 'ContentNode', 'editLink', [
		'type' => 'String',
		'description' => __( 'Link to edit the content', 'your-textdomain' ),
		'resolve' => function( \WPGraphQL\Model\Post $post, $args, $context, $info ) {
		   return get_edit_post_link( $post->databaseId );
		}
	]);

} );

This could then be queried like so:

Screenshot of a GraphQL Query for posts with their editLink
Screenshot of a GraphQL Query for posts with their editLink

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.