Skip to content

Instantly share code, notes, and snippets.

View jasonbahl's full-sized avatar
:octocat:

Jason Bahl jasonbahl

:octocat:
View GitHub Profile
<?php
/**
* Config for WPGraphQL ACF
*
* @package wp-graphql-acf
*/
namespace WPGraphQL\ACF;
use WPGraphQL\Data\DataSource;
add_filter( 'graphql_post_object_connection_query_args', function( $args ) {
$args['no_found_rows'] = false;
return $args;
} );
add_filter( 'graphql_connection_page_info', function( $page_info, $connection ) {
$page_info['total'] = null;
if ( $connection->get_query() instanceof \WP_Query ) {
if ( isset( $connection->get_query()->found_posts ) ) {
$page_info['total'] = (int) $connection->get_query()->found_posts;
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'activeTheme', [
'type' => 'Theme',
'description' => __( 'The currently active theme for the site', 'your-textdomain' ),
'resolve' => function() {
$theme = wp_get_theme();
return new Theme( $theme );
}
] );
} );
@jasonbahl
jasonbahl / register-custom-post-type-resolver.php
Last active July 26, 2019 15:38
This shows how to register a Custom Post Type to show in GraphQL, and how to register a root field to access a list of nodes. Demonstration for helping someone in Slack.
add_action( 'init', function(){
register_post_type( 'stores', [
'label' => 'Stores',
'show_in_graphql' => true,
'hierarchical' => true,
'graphql_single_name' => 'store',
'graphql_plural_name' => 'stores',
'public' => true,
'supports' => [ 'title', 'thumbnail', 'excerpt' ],
] );
@jasonbahl
jasonbahl / Form.js
Created October 9, 2019 21:47
hacky idea for graphql form submissions
import React from 'react'
import {
TextField,
TextFieldFragment
ImageField,
ImageFieldFragment
}
class Form extends React.Component {
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'allUrls', [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'A list of all urls. Helpful for rendering sitemaps', 'your-textdomain' ),
'resolve' => function() {
// Start collecting all URLS
$meta_urls = array( get_home_url() );
$all_posts = [ 'site.com/post', 'site.com/post-2' ];
@jasonbahl
jasonbahl / attachedMedia-connection
Last active June 4, 2020 13:51
Showing how to register a connection to attached media in WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'ContentNode',
'toType' => 'MediaItem',
'fromFieldName' => 'attachedMedia',
'connectionArgs' => \WPGraphQL\Connection\PostObjects::get_connection_args(),
'resolve' => function( \WPGraphQL\Model\Post $source, $args, $context, $info ) {
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, 'attachment' );
$resolver->setQueryArg( 'post_parent', $source->ID );
@jasonbahl
jasonbahl / register-graphql-field-with-argument.php
Created February 26, 2020 16:37
This is an example of registering a field with an argument showing how to use the argument in a resolver.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'myNewField', [
'type' => 'String',
'args' => [
'myArg' => [
'type' => 'String',
],
],
'resolve' => function( $source, $args, $context, $info ) {
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'listOfStrings', [
'type' => [ 'list_of' => 'String' ],
'resolve' => function() {
return [
'String One',
'String Two'
];
}
add_action( 'graphql_register_types', function() {
register_graphql_object_type(
'stuntPerformer',
[
'description' => __( 'Stunt PErformer', 'bsr' ),
'fields' => [
'firstName' => [
'type' => 'String',
'description' => 'fisrt name'