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-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 );
add_action( 'wpgraphql_cache_purge_nodes', function( $key = 'purge', $nodes = [] ) {
$pipedream_debugging = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'pipedream_post', 'off', 'purge_debugging' ) : 'off';
$enabled = $pipedream_debugging === 'on';
if ( ! $enabled || empty( $nodes ) ) {
return;
}
$args = [
@jasonbahl
jasonbahl / add-argument-to-graphql-field.php
Created June 15, 2022 17:24
This shows how to add an argument to a field in graphql and use it in a resolver.
add_filter( 'graphql_RootQuery_fields', function( $fields ) {
if ( isset( $fields['posts'] ) ) {
$args = is_array( $fields['posts']['args'] ) ? $fields['posts']['args'] : [];
$args['myCustomArg'] = [
'type' => 'String',
'description' => __( 'This is a field that was added via a filter', 'your-textdomain' ),
];
$fields['posts']['args'] = $args;
}
@jasonbahl
jasonbahl / parse-query-to-types.php
Last active May 25, 2022 21:01
Parses a GraphQL Query and returns an array of Types that were requested
<?php
/**
* Given the Schema and a query string, return a list of GraphQL Types that are being asked for
* by the query.
*
* @param Schema $schema The WPGraphQL Schema
* @param string $query The query string
*
* @return array
@jasonbahl
jasonbahl / wp-graphql-auth-callbacks.php
Created May 13, 2022 18:57
Using the auth callbacks when registering fields to the Schema.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'privateField', [
'type' => 'String',
'resolve' => function() {
return 'some private data';
},
'auth' => [
'callback' => function() {
return current_user_can( 'edit_posts' );