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-page-siblings-connection.php
Created July 13, 2023 14:47
Register a page siblings connection in WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_connection( [
'fromType' => 'Page',
'toType' => 'Page',
'connectionTypeName' => 'PageSiblings',
'fromFieldName' => 'siblings',
'resolve' => function( $page, $args, $context, $info ) {
$parent = $page->parentDatabaseId ?? null;
@jasonbahl
jasonbahl / wp-graphql-smart-cache-custom-even-listener.php
Created June 1, 2023 16:33
Listen to custom events and call purge
add_action( 'graphql_cache_invalidation_init', static function( \WPGraphQL\SmartCache\Cache\Invalidation $invalidation ) {
add_action( 'updated_option', static function( $option, $value, $original_value ) use ( $invalidation ) {
// phpcs:ignore
if ( ! isset( $_POST['_acf_screen'] ) || 'options' !== $_POST['_acf_screen'] ) {
return;
}
// phpcs:ignore
add_filter( 'graphql_query_analyzer_graphql_keys', function( $graphql_keys, $return_keys, $skipped_keys, $return_keys_array, $skipped_keys_array ) {
$keys_array = explode( ' ', $return_keys );
if ( empty( $keys_array ) || ! in_array( 'operation:GetPostBySlug', $keys_array, true ) ) {
return $graphql_keys;
}
if ( ( $key = array_search('list:tag', $keys_array ) ) !== false ) {
unset( $keys_array[$key] );
add_action( 'graphql_return_response', function( $filtered_response, $response, $schema, $operation, $query, $variables, $request, $query_id ) {
$errors = [];
if ( ! is_array( $filtered_response ) && ! empty( $filtered_response->errors ) && is_array( $filtered_response->errors ) ) {
$errors = $filtered_response->errors;
} else if ( is_array( $filtered_response ) && ! empty( $filtered_response['errors'] ) && is_array( $filtered_response['errors'] ) ) {
$errors = $filtered_response['errors'];
}
function _graphql_acf_sanitize_flexible_content_resolver( $value, $acf_field, $context, $type_name ) {
if ( ! isset( $value['acf_fc_layout'] ) ) {
return null;
}
$type_registry = $context->type_registry;
$field_type_name = $type_name . '_' . ucfirst( \WPGraphQL\ACF\Config::camel_case( $acf_field['name'] ) );
@jasonbahl
jasonbahl / wp-graphql-acf-override-resolver.php
Last active May 11, 2023 15:33
Override a resolver for wp-graphql-acf v0.6.1 and older to prevent orphaned IDs from causing errors.
function _graphql_acf_sanitize_post_object_resolver( $value ) {
if ( ! $value instanceof \WPGraphQL\Model\Post ) {
return $value;
}
if ( 'publish' !== $value->status ) {
return $value;
}
// Add Hobbies to GraphQl
add_action('graphql_init', function () {
$hobbies = [
// 'type' => 'String',
'type' => ['list_of' => 'String'],
'description' => __('Custom field for user mutations', 'your-textdomain'),
'resolve' => function ($post) {
$hobbies = get_post_meta($post->ID, 'hobbies', true);
return !empty($hobbies) ? $hobbies : '';
},
add_filter( 'graphql_pre_resolve_field', function( $default, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver ) {
// if the field is not the 'tags' field, and the
if ( ! ( 'tags' === $field_key && 'post' === strtolower( $type_name ) ) ) {
return $default;
}
$empty = [
'edges' => [],
'nodes' => [],
@jasonbahl
jasonbahl / register-connection-to-interfaces.php
Created March 6, 2023 19:18
testing registering a connection to an interface that other interfaces implement
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'testType', [
'type' => 'TestType',
'resolve' => function() {
return [
'__typename' => 'TestType',
'test' => 'Test Field Value...',
'id' => 'root:testType:1'
];
<?php
/**
* Plugin Name: WPGraphQL Smart Cache for Litespeed
* Description: Allows WPGraphQL Smart Cache to work for WordPress sites hosted on environments using Litespeed Cache
* Author: WPGraphQL
* Author URI: http://www.wpgraphql.com
* Text Domain: wp-graphql-smart-cache-litespeed
* Version: 0.0.1
* Requires at least: 5.0
* Tested up to: 6.1