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 / 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;
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 {