Skip to content

Instantly share code, notes, and snippets.

View jasonbahl's full-sized avatar
:octocat:

Jason Bahl jasonbahl

:octocat:
View GitHub Profile
// 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
@jasonbahl
jasonbahl / wp-graphql-post-type-order.php
Last active November 22, 2022 23:31
Compatibility for the Custom Post Type order plugin and WPGraphQL
<?php
/**
* Plugin Name: WPGraphQL Post Type Order Fix
* Description: Fixes a bug with WPGraphQL pagination when the "Post Type Order" plugin is also active and in use
*/
add_action( 'pre_get_posts', function () {
// access the custom post type order class
global $CPTO;
add_action( 'init_graphql_request', function() {
$analyzer = null;
add_action( 'graphql_determine_graphql_keys', function( \WPGraphQL\Utils\QueryAnalyzer $query_analyzer, $query ) use ( &$analyzer ) {
$analyzer = $query_analyzer;
}, 10, 2 );
add_filter( 'graphql_query_analyzer_get_runtime_nodes', function( $nodes ) use ( &$analyzer ) {
@jasonbahl
jasonbahl / faust-automated-persisted-queries-plugin.js
Created October 25, 2022 15:13
Faust.js plugin for adding Automated Persisted Query support. Plays nice with https://github.com/wp-graphql/wp-graphql-smart-cache
import {createPersistedQueryLink} from '@apollo/client/link/persisted-queries';
import {HttpLink} from "@apollo/client";
import {sha256} from 'crypto-hash';
const linkChain = createPersistedQueryLink({ sha256 }).concat(
new HttpLink({ uri: process.env.WPGRAPHQL_URL }),
);
class PersistedQueriesPlugin {
apply({ addFilter }) {
add_action( 'graphql_register_types', function() {
register_graphql_interface_type( 'Food', [
'description' => __( 'An item of food for sale', 'your-textdomain' ),
'interfaces' => [ 'Node', 'NodeWithTitle' ],
'fields' => [
'price' => [
'type' => 'String',
'description' => __( 'The cost of the food item', 'your-textdomain' ),
'resolve' => function( $food ) {
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Food', 'price', [
'type' => 'String',
'description' => __( 'The cost of the food item', 'your-textdomain' ),
'resolve' => function( $food ) {
return get_post_meta( $food->databaseId, 'price', true );
}
]);
class UniquePosts {
protected $loaded_posts = [];
protected $amount_requested = 0;
public function __construct() {
add_filter( 'graphql_connection_amount_requested', [ $this, 'filter_amount_requested' ], 10, 2 );
add_filter( 'graphql_connection_nodes', [ $this, 'connection_nodes' ], 10, 2 );