Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasonbahl/f01df134beebe48f2c8f126b0ea0adb0 to your computer and use it in GitHub Desktop.
Save jasonbahl/f01df134beebe48f2c8f126b0ea0adb0 to your computer and use it in GitHub Desktop.
Shows a WPGraphQL connection between 2 post types
/**
* Register the Post Type for storing the logs
*/
add_action( 'init', function() {
register_post_type( 'graphql_mutation_log', [
'label' => __( 'Mutation Logs', 'wp-syndication-debugger' ),
'public' => false,
'exclude_from_search' => true,
'show_ui' => current_user_can( 'manage_options' ) ? true : false,
'show_in_graphql' => true,
'graphql_single_name' => 'MutationLog',
'graphql_plural_name' => 'MutationLogs',
'hierarchical' => true,
] );
});
/**
* Register custom fields on the `MutationLog` Type in the Schema
*/
add_action( 'graphql_register_types', function() {
register_graphql_fields( 'MutationLog', [
'graphQLQuery' => [
'type' => 'String',
'resolve' => function( $post ) {
$query = get_post_meta( $post->ID, 'query', true );
return ! empty( $query ) ? $query : null;
}
],
'graphQLMutationName' => [
'type' => 'String',
'resolve' => function( $post ) {
$mutation = get_post_meta( $post->ID, 'mutation', true );
return ! empty( $mutation ) ? $mutation : null;
}
],
'currentUser' => [
'type' => 'User',
'resolve' => function( $post, $args, $context, $info ) {
$user_id = get_post_meta( $post->ID, 'current_user', true );
$user_id = json_decode( $user_id );
return ! empty( $user_id ) ? DataSource::resolve_user( $user_id, $context ) : null;
}
],
'graphQLVariables' => [
'type' => 'String',
'resolve' => function( $post, $args, $context, $info ) {
$variables = get_post_meta( $post->ID, 'variables', true );
return ! empty( $variables ) ? $variables : null;
}
],
'graphQLOperation' => [
'type' => 'String',
'resolve' => function( $post ) {
$operation = get_post_meta( $post->ID, 'operation', true );
return ! empty( $operation ) ? $operation : null;
}
],
'headers' => [
'type' => [ 'list_of' => 'String' ],
'resolve' => function( $post ) {
$headers = get_post_meta( $post->ID, 'headers', true );
$headers = (array) json_decode( $headers );
$return = [];
if ( ! empty( $headers ) && is_array( $headers ) ) {
foreach ( $headers as $key => $value ) {
$return[ $key ] = $key . ' ___ ' . $value;
}
}
return ! empty( $return ) ? $return : null;
}
],
'createdDate' => [
'type' => 'String',
'resolve' => function( $post, $args, $context, $info ) {
$created_date = get_post_meta( $post->ID, 'created_date', true );
$created_date = json_decode( $created_date );
return ! empty( $created_date ) ? $created_date : null;
}
],
'doingAutosave' => [
'type' => 'Boolean',
'resolve' => function( $post, $args, $context, $info ) {
$doing_autosave = get_post_meta( $post->ID, 'doing_autosave', true );
$doing_autosave = json_decode( $doing_autosave );
return true === $doing_autosave ? $doing_autosave : false;
}
],
'isUpdate' => [
'type' => 'Boolean',
'resolve' => function( $post, $args, $context, $info ) {
$is_update = get_post_meta( $post->ID, 'is_update', true );
$is_update = json_decode( $is_update );
return true === $is_update ? $is_update : false;
}
],
'updatedDate' => [
'type' => 'String',
'resolve' => function( $post, $args, $context, $info ) {
$udpate_date = get_post_meta( $post->ID, 'update_date', true );
$udpate_date = json_decode( $udpate_date );
return ! empty( $udpate_date ) ? $udpate_date : null;
}
]
] );
} );
/**
* Register the connection between Posts and the Mutation Logs
*/
add_action( 'graphql_register_types', function() {
register_graphql_connection( [
'fromType' => 'Post',
'toType' => 'MutationLog',
'fromFieldName' => 'mutationLogs',
'resolveNode' => function( $id, $args, $context, $info ) {
return DataSource::resolve_post_object( $id, $context );
},
'resolve' => function( Post $post, $args, $context, $info ) {
$resolver = new PostObjectConnectionResolver( $post, $args, $context, $info, 'graphql_mutation_log' );
$resolver->setQueryArg( 'post_parent', $post->ID );
$resolver->setQueryArg( 'post_type', 'graphql_mutation_log' );
$resolver->setQueryArg( 'post_status', 'any' );
return $resolver->get_connection();
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment