Recipes

WPGraphQL recipes are practical snippets that show how to customize WPGraphQL with actions, filters, and functions.

Authorization

Make all Users Public

The following snippets allow for Users with no published content to be shown in public (non-authenticated) WPGraphQL query results. For a more detailed write-up, read the blog post: Allowing WPGraphQL to show unpublished…
View recipe

Making Menus and Menu Items public

By default, Menus and Menu Items that are not assigned to a Menu Location are considered private, meaning they are not exposed in non-authenticated WPGraphQL Queries. If you want to expose Menus and Menu Items that are n…
View recipe

Showing Post Type labels in public queries

WPGraphQL respects WordPress core access control rights. This means that data that is only available to authenticated users in the WordPress admin is only available to authenticated users making GraphQL requests. Sometim…
View recipe

Custom Fields

Add Edit Link to All Post Types

The following snippet shows how to add the “Edit” link as a GraphQL field to all post types: add_action( 'graphql_register_types', function() { register_graphql_field( 'ContentNode', 'editLink', [ 'type' => 'String', 'de…
View recipe

Add field for unencoded content

The following adds a field to the NodeWithContentEditor interface to get the unencoded content for a post: add_action( 'graphql_register_types', function() { register_graphql_field( 'NodeWithContentEditor', 'unencodedCon…
View recipe

Add field to output URLs for Sitemap

The following code is an example of how you can create a field called allUrls that will output site URLs that could be used to generate a sitemap. The resolver uses mostly hard-coded data, but shows what a potential solu…
View recipe

Add Primary Category field for The SEO Framework plugin

The following adds a field called primaryCat field when using The SEO Framework WordPress Plugin add_action( 'init', function() { register_graphql_connection([ 'fromType' => 'Post', 'toType' => 'Category', 'fromFieldName…
View recipe

Deprecating a field in the Schema

Sometimes it can be helpful to deprecate a field in the Schema without removing it altogether. This snippet shows how to deprecate the `Post.excerpt` field. You can use this technique to deprecate other fields. // Filter…
View recipe

List of Key Values

This is an example showing how to return a list of keys and values where the keys and values are both strings. add_action( 'graphql_register_types', function() { register_graphql_object_type( 'keyValue', [ 'description'…
View recipe

Register a basic Mutation

This snippet shows how to register a basic GraphQL Mutation with a single input field, a single output field, and the input is simply returned as the value for the output. add_action( 'graphql_register_types', function()…
View recipe

Register field as a list of strings

The below code registers a field called listOfStrings that returns a list of strings as the result: add_action( 'graphql_register_types', function() { register_graphql_field( 'RootQuery', 'listOfStrings', [ 'type' => [ '…
View recipe

Register GraphQL Field with Argument

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' =>…
View recipe

Register object and field for custom list of users

The following code creates an object type called StuntPerformer and creates a field on the RootQuery called stuntPerformers that returns a custom list of users. In this case, the list of users are the admins of the websi…
View recipe

Custom Post Types

Popular Posts

The following code allows you to query for popular posts. It’s still up to you to determine the best way to store popular posts, but this example assumes a meta_key is involved. Beware though, meta queries can be expensi…
View recipe

Register Connection to Attached Media

This code shows how to register a connection to attached media in WPGraphQL add_action( 'graphql_register_types', function() { register_graphql_connection([ 'fromType' => 'ContentNode', 'toType' => 'MediaItem', 'fromFiel…
View recipe

Tag to Content Node Connection

The following code registers a connection from Tags to ContentNodes. A field name called contentNodes will be added to the Tag type to make it easy to view all Posts that are tagged with that specific term. add_action( '…
View recipe

Execution

Remove Extensions from GraphQL Response

This snippet removes the “extensions” from the GraphQL response: add_filter( 'graphql_request_results', function( $response ) { // If it's an ExecutionResult object, we need to handle it differently if ( $response instan…
View recipe

Extensions

Changing the Server Debug Flag

The following snippets show how to change the Debug Flag for the GraphQL Server execution. By default, the function callstack trace is not included with errors data unless users are logged in. If you wanted to track this…
View recipe

Queries

Filter to add restricted field on Model

Labels on Post Types are not publicly exposed by WordPress. They are attributes for use in the Admin, and are treated with respect to proper access to the admin. To see the labels, the user requesting them must be authen…
View recipe

Query the Homepage

In WordPress, the homepage can be a Page or an archive of posts of the Post post_type (which is represented by WPGraphQL as a “ContentType” node). This query allows you to query the homepage, and specify what data you wa…
View recipe

Using GraphQL Fragments in PHP

You can execute GraphQL queries in PHP. In this case, we even show using a GraphQL Fragment. add_action( 'init', function() { $results = graphql([ 'query' => ' { posts { nodes { ...PostFields } } } fragment PostFields on…
View recipe

WP Admin

Add WP Admin Color Schemes to WPGraphQL

The following example code allows you to add WP Admin Color Schemes data to WPGraphQL. add_action( 'graphql_register_types', function() { $mock_colors = ' { "fresh":{ "name":"Default", "url":false, "colors":[ "#222", "#3…
View recipe

Debugging JWT Authentication

This snippet outputs the $_SERVER superglobal so we can see if the Authorization token is being passed to the server or not. add_action( 'init', function() { if ( is_graphql_http_request() ) { wp_send_json( [ 'server' =>…
View recipe

Log WPGraphQL requests to error log

This snippet logs all WPGraphQL requests to the error log add_action( 'do_graphql_request', function( $query, $operation, $variables, $params) { error_log( wp_json_encode( [ 'query' => $query, 'operation' => $operation,…
View recipe

Update WPGraphQL Endpoint URL

You can modify the WPGraphQL endpoint in code with the following: function my_new_graphql_endpoint() { return 'cutepuppies'; }; add_filter( 'graphql_endpoint', 'my_new_graphql_endpoint' ); This will change the graphql en…
View recipe

Uncategorized

ACF Nav Menu Plugin Support

This adds support (native) for the ACF Nav Menu field plugin ( https://github.com/jgraup/advanced-custom-fields-nav-menu-field ). This also requires WPGraphQL for Advanced Custom Fields . add_filter( 'wpgraphql_acf_regis…
View recipe

Filter Connection Args

This filters connection args by checking the field name the connection is coming from as well as the type the connection is coming from. add_filter( 'graphql_connection_query_args', function( $args, \WPGraphQL\Data\Conne…
View recipe

Page Siblings Connection

Below is a connection that adds the ability to query page siblings (i.e. pages that share the same parent) add_action( 'graphql_register_types', function() { register_graphql_connection( [ 'fromType' => 'Page', 'toType'…
View recipe