Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
🖥

Jason Bahl jasonbahl

💻
🖥
View GitHub Profile
View wp-graphql-popular-posts-connection.php
View wp-graphql-acf-nav-menu-field.php
add_filter( 'wpgraphql_acf_register_graphql_field', function( $field_config, $type_name, $field_name, $config ) {
if ( isset( $config['acf_field']['type'] ) && 'nav_menu' === $config['acf_field']['type'] ) {
$field_config['type'] = 'Menu';
$field_config['resolve'] = function( $root, $args, \WPGraphQL\AppContext $context, $info ) use ( $config ) {
$menu_id = get_field( $config['acf_field']['key'], $root->databaseId );
return ! empty( $menu_id ) ? $context->get_loader( 'term' )->load_deferred( $menu_id ) : null;
};
}
return $field_config;
}, 10, 4 );
View add-unencoded-content-field-to-post-types-with-editor.php
add_action( 'graphql_register_types', function() {
register_graphql_field( 'NodeWithContentEditor', 'unencodedContent', [
'type' => 'String',
'resolve' => function( $post ) {
$content = get_post( $post->databaseId )->post_content;
return ! empty( $content ) ? apply_filters( 'the_content', $content ) : null;
}
]);
});
@jasonbahl
jasonbahl / wp-graphql-filter-connection-args.php
Created Sep 2, 2020
This filters the connection args for specific connections
View wp-graphql-filter-connection-args.php
add_filter( 'graphql_connection_query_args', function( $args, \WPGraphQL\Data\Connection\AbstractConnectionResolver $connection ) {
// Get the Info from the resolver
$info = $connection->getInfo();
// Get the field name being queried
$field_name = $info->fieldName;
// If the resolver isn't for the children field, return the $args and move on
if ( 'children' !== $field_name ) {
@jasonbahl
jasonbahl / wp-graphql-gutenberg-test.php
Created Sep 1, 2020
This was some initial work done to test Gutenberg support with WordPress 5.5 server registry
View wp-graphql-gutenberg-test.php
$blocks = \WP_Block_Type_Registry::get_instance();
$registered_blocks = $blocks->get_all_registered();
if ( ! empty( $registered_blocks ) ) {
register_graphql_interface_type( 'ContentEditorBlock', [
'description' => __( 'Blocks used by the content editor', 'wp-graphql' ),
'resolveType' => function( $block ) {
return isset( $block['name'] ) ? $this->get_type( graphql_format_type_name( $block['name'] ) ) : null;
},
'fields' => [
@jasonbahl
jasonbahl / debugging-wp-graphql-jwt-authentication.php
Created Aug 31, 2020
This snippet outputs the $_SERVER superglobal so we can see if the Authorization token is being passed to the server or not.
View debugging-wp-graphql-jwt-authentication.php
add_action( 'init', function() {
if ( is_graphql_http_request() ) {
wp_send_json( [ 'server' => $_SERVER ] );
}
} );
View primary-cat-connection-with-seo-framework.php
add_action( 'init', function() {
register_graphql_connection([
'fromType' => 'Post',
'toType' => 'Category',
'fromFieldName' => 'primaryCat',
'oneToOne' => true,
'resolve' => function( \WPGraphQL\Model\Post $post, $args, $context, $info ) {
$primary_term = null;
@jasonbahl
jasonbahl / wpgraphql-list-of-key-values.php
Last active Aug 4, 2020
This is an example showing how to return a list of keys and values where the keys and values are both strings.
View wpgraphql-list-of-key-values.php
add_action( 'graphql_register_types', function() {
register_graphql_object_type( 'keyValue', [
'description' => __( 'Keys and their values, both cast as strings', 'your-textdomain' ),
'fields' => [
'key' => [
'type' => 'String',
],
'value' => [
'type' => 'String',
View tag-to-content-node-connection.php
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'Tag',
'toType' => 'ContentNode',
'fromFieldName' => 'contentNodes',
'resolve' => function( \WPGraphQL\Model\Term $source, $args, $context, $info ) {
// Get all post types allowed in GraphQL
$post_types = WPGraphQL::get_allowed_post_types();
// Instantiate a new PostObjectConnectionResolver class
You can’t perform that action at this time.