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 / functions.php
Created May 2, 2022 16:03
show wp-template-part and wp-template-part-area in GraphQL
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'wp_template_part' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'TemplatePart';
$args['graphql_plural_name'] = 'TemplateParts';
}
return $args;
@jasonbahl
jasonbahl / graphql-request-logger.php
Created February 3, 2022 16:20
WPGraphQL Request Logger
<?php
/**
* Plugin Name: WPGraphQL Request Logger
* Description: This plugin logs WPGraphQL Requests to a Custom Post Type. This is a debugging tool. Not intended for production use. Also, not fully tested. Use at your own risk.
* Plugin Author: Jason Bahl
* Author URI: https://www.wpgraphql.com
*/
add_action( 'init', function() {
register_post_type( 'graphql_request_logs', [
@jasonbahl
jasonbahl / wp-graphql-full-site-editing-config.php
Created January 14, 2022 17:40
Adding Gutenberg Full Site Editing settings to WPGraphQL
// NOTE, THIS IS VERY EXPERIMENTAL. TAKE THIS WITH A BIG GRAIN OF SALT, BUT DO WHAT YOU WILL WITH IT.
register_graphql_object_type("ThemeSettings", [
'description' => 'Theme Settings',
'fields' => [
'primaryColor' => [
'type' => 'string',
'description' => 'Primary Color',
'resolve' => function ($settings) {
$colors = $settings->settings->color->palette->theme;
add_filter( 'graphql_pre_resolve_field', function( $default, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver ) {
if ( 'content' === $field_key && 'Post' === $type_name ) {
return 'your override value';
}
return $default;
}, 10, 9 );
const { hooks, useAppContext } = wpGraphiQL
const { useState } = wp.element
const DemoButton = props => {
const { GraphiQL, graphiql } = props;
const [ count, setCount ] = useState( 0 )
return (
/**
* This is an example showing how to add a custom action
* to the operation action menu
*/
hooks.addFilter(
"graphiql_operation_action_menu_items",
"graphiql-extension",
(menuItems, props) => {
const { Menu } = props;
const { hooks } = wpGraphiQL;
import LZString from 'lz-string'
hooks.addFilter( 'graphiql_after_graphiql', 'wp-graphiql-extension', (res, props) => {
const encoded = LZString.compressToEncodedURIComponent(props.query)
const decoded = LZString.decompressFromEncodedURIComponent(encoded)
res.push(
<div style={{maxWidth: `200px`}}>
@jasonbahl
jasonbahl / query-posts
Created September 16, 2021 21:43
Query for posts, authors and terms
{
posts {
nodes {
id
title
author {
node {
id
firstName
avatar {
@jasonbahl
jasonbahl / update-wp-graphql-woocommerce-resolveNode.php
Created July 29, 2021 15:18
Allow WPGraphQL for WooCommerce resolveNode callbacks to work with WPGraphQL v1.15+
add_filter( 'graphql_pre_resolve_field', function( $nil, $source, $args, \WPGraphQL\AppContext $context, \GraphQL\Type\Definition\ResolveInfo $info, $type_name, $field_key, $field, $field_resolver ) {
if ( 'RootQueryToShippingMethodConnection' === $type_name ) {
if ( 'nodes' === $field_key ) {
if ( ! empty( $source['nodes'] ) && is_array( $source['nodes'] ) ) {
$nodes = [];
foreach ( $source['nodes'] as $node ) {
$nodes[] = \WPGraphQL\WooCommerce\Data\Factory\Factory::resolve_shipping_method( $node );
}
@jasonbahl
jasonbahl / functions.php
Created July 27, 2021 04:27
Create new posts with unique authors
add_action( 'init', function() {
return;
$users = graphql([
'query' => '
{
users(first:100) {
nodes {
databaseId
}
}