Skip to content

Instantly share code, notes, and snippets.

View jasonbahl's full-sized avatar
:octocat:

Jason Bahl jasonbahl

:octocat:
View GitHub Profile
@jasonbahl
jasonbahl / wp-graphql.php
Created February 5, 2019 20:36
pseudo code to filter mutations to not execute if they're duplicate requests. . .see: https://github.com/wp-graphql/wp-graphql/issues/602#issuecomment-460791863
add_action( 'graphql_before_resolve_field', function( $source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info, $field_resolver, $type_name, $field_key, $field ) {
if ( 'RootMutation' === $info->parentType->name ) {
$type = \WPGraphQL\TypeRegistry::get_type( $type_name );
if ( isset( $type ) && ! empty( $field = $type->getField( $field_key ) ) ) {
if ( isset( $field->config['isPublic'] ) && true === $field->config['isPublic'] ) {
if ( true === true ) {
throw new Exception( __( 'This mutation has been executed too many times in the allowed timeframe', 'wp-graphql' ) );
}
@jasonbahl
jasonbahl / add-custom-fields-to-user-mutations.php
Created February 6, 2019 19:19
Add custom fields to User Mutations in WPGraphQL
add_action( 'graphql_register_types', function() {
$args = [
'type' => 'String',
'description' => __( 'Custom field for user mutations', 'your-textdomain' ),
'resolve' => function( \WP_User $user ) {
return get_user_meta( $user->ID, 'custom_field_meta_key', true );
}
];
@jasonbahl
jasonbahl / disable-frontent.php
Last active August 27, 2024 17:03
Disable frontend but allow REST, CRON and GraphQL Requests
<?php
add_action( 'parse_request', 'disable_front_end', 99 );
function disable_front_end() {
global $wp;
/**
* If the request is not part of a CRON, REST Request, GraphQL Request or Admin request,
add_action( 'graphql_register_types', 'register_dog_type' );
function register_dog_type() {
register_graphql_object_type( 'Dog', [
'description' => __( "Man's best friend", 'your-textdomain' ),
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the dog', 'your-textdomain' ),
],
add_action( 'graphql_register_types', function() {
$post_types = WPGraphQL::$allowed_post_types;
if ( ! empty( $post_types ) && is_array( $post_types ) ) {
foreach ( $post_types as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
/**
* Get the Type name with ucfirst
@jasonbahl
jasonbahl / posts-to-posts-connections-wp-graphql.php
Created April 9, 2019 19:32
Example showing a connection from one Post Type to another, resolving with related IDs stored in Post Meta
add_action( 'init', function() {
register_post_type( 'physician', [
'hierarchical' => false,
'label' => __( 'Physicians', 'wp-graphql' ),
'public' => true,
'show_in_graphql' => true,
'graphql_single_name' => 'Physician',
'graphql_plural_name' => 'Physicians',
] );
register_post_type( 'disease', [
@jasonbahl
jasonbahl / register-circular-types-in-wpgraphql.php
Created April 26, 2019 19:03
This shows an example of registering circular types in WPGraphQL v0.3.2
add_action( 'graphql_register_types', function() {
$myCustomType = [
'someField' => 'Some value...',
];
register_graphql_object_type( 'MyCustomType', [
'fields' => [
'someField' => [
'type' => 'string',
@jasonbahl
jasonbahl / order-by-acf-like-count.php
Last active July 10, 2024 20:41
Shows how to add a custom order value to a connection to order by a custom field.
add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {
$values['LIKE_COUNT'] = [
'value' => 'like_count',
'description' => __( 'The number of likes on the post', 'wp-graphql' ),
];
return $values;
} );
@jasonbahl
jasonbahl / wpgraphql-connection-to-another-post-type.php
Created May 30, 2019 12:17
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,
@jasonbahl
jasonbahl / post-attachments-connection.php
Created June 28, 2019 15:06
Register attachments connection for posts
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'Post',
'toType' => 'MediaItem',
'fromFieldName' => 'attachments',
'resolveNode' => function( $id, $args, $context ) {
return \WPGraphQL\Data\DataSource::resolve_post_object( $id, $context );
},
'resolve' => function( $post, $args, $context, $info ) {