Skip to content

Instantly share code, notes, and snippets.

View jasonbahl's full-sized avatar
:octocat:

Jason Bahl jasonbahl

:octocat:
View GitHub Profile
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' );
@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;