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 / gutenberg-graphql-middleware
Created May 2, 2018 21:03
Gutenberg GraphQL Middleware
const { addFilter } = wp.hooks;
const { withState } = wp.components;
class GraphQLMiddleware {
constructor(config) {
this.query = config.attributes.graphql.query;
this.mutation = {};
this.config = _.extend( {}, config );
}
@jasonbahl
jasonbahl / gutenberg-page-template-switcher.js
Created May 4, 2018 21:01
Gutenberg Page Template Switcher
import gql from 'graphql-tag'
import { client } from '../utils/apollo-client'
const { parse } = wp.blocks;
const { select, subscribe, dispatch } = wp.data;
const GET_PAGE_BLOCK_TEMPLATE = gql`
query GET_PAGE_BLOCK_TEMPLATE($id: ID!, $template: String) {
page( id: $id ) {
blockTemplate( pageTemplate: $template )
}
}
@jasonbahl
jasonbahl / gutenberg-wpgraphql-page-block-template.php
Created May 4, 2018 21:24
Gutenberg Page Block Template field
add_filter( 'graphql_page_fields', function( $fields ) {
$fields['blockTemplate'] = [
'type' => \WPGraphQL\Types::string(),
'description' => __( 'The Block template defined for the page', 'oshpd-guten-blocks' ),
'args' => [
'pageTemplate' => [
'type' => \WPGraphQL\Types::string(),
'description' => __( 'The page template associated with the block template', 'oshpd-guten-blocks' ),
],
@jasonbahl
jasonbahl / gutenberg-page-template-switcher-not-working.js
Created May 7, 2018 17:47
Gutenberg Page Template Switcher (Not working)
import gql from 'graphql-tag'
import { client } from '../utils/apollo-client'
const { parse } = wp.blocks;
const { select, subscribe, dispatch } = wp.data;
const GET_PAGE_BLOCK_TEMPLATE = gql`
query GET_PAGE_BLOCK_TEMPLATE($id: ID!, $template: String) {
page( id: $id ) {
blockTemplate( pageTemplate: $template )
}
}
@jasonbahl
jasonbahl / gutenberg-wpgraphql-page-template-graphql-fields.php
Created May 7, 2018 17:48
Gutenberg WPGraphQL - Page Template GraphQL Fields
/**
* Adds a field to the Page type in the GraphQL Schema to allow for the page's "blockTemplate" to be fetched and
* returned.
*/
add_filter( 'graphql_page_fields', function( $fields ) {
$fields['blockTemplate'] = [
'type' => \WPGraphQL\Types::string(),
'description' => __( 'The Block template defined for the page', 'oshpd-guten-blocks' ),
'args' => [
@jasonbahl
jasonbahl / wpgraphql-list-roles-and-capabilities.php
Created May 15, 2018 20:28
List what user roles are associated with each capability
add_filter( 'graphql_root_queries', function( $fields ) {
$fields['userRoles'] = [
'type' => \WPGraphQL\Types::list_of( new \WPGraphQL\Type\WPObjectType([
'name' => 'UserRole',
'fields' => [
'name' => [
'type' => \WPGraphQL\Types::string(),
],
'capabilities' => [
add_action( 'init', function () {
register_post_type( 'app_audio_track', [
'label' => 'App Audio Track',
'public' => true,
'show_in_graphql' => true,
'graphql_single_name' => 'AppAudioTrack',
'graphql_plural_name' => 'AppAudioTracks',
] );
} );
@jasonbahl
jasonbahl / like-posts.php
Created December 10, 2018 22:06
Adding a "likePost" mutation for WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Post', 'likeCount', [
'type' => 'Int',
'description' => __( 'The number of likes for the post', 'your-textdomain' ),
'resolve' => function( $post ) {
$likes = get_post_meta( $post->ID, 'likes', true );
return isset( $likes ) ? (int) $likes : 0;
}
] );
@jasonbahl
jasonbahl / audio-connection.php
Created January 25, 2019 18:46
Showcases how to register custom connections with WPGraphQL. See:
add_action( 'init', function() {
register_post_type( 'audio-track', [
'public' => true,
'label' => __( 'Audio Tracks', 'wp-graphql' ),
'show_in_graphql' => true,
'graphql_single_name' => 'AudioTrack',
'graphql_plural_name' => 'AudioTracks'
] );
register_post_type( 'audio-playlist', [
@jasonbahl
jasonbahl / filter-fields-to-require-auth.php
Last active April 9, 2020 16:30
Shows how to filter fields in the WPGraphQL Schema to require authentication before resolving.
add_action( 'graphql_register_types', function() {
/**
* Define the Types and their Fields that we want to filter
*/
$fields_to_require_auth = [
'Post' => [
'author'
],
'Comment' => [