Homepage
Link to WPGraphQL on Github
Link to WPGraphQL on WordPress.org

Recipes

WPGraphQL Recipes are bite-size morsels that can help satisfy your craving for a productivity boost when working with WPGraphQL. Here you will find helpful tips and tricks for extending the WPGraphQL Schema, or otherwise modifying the WPGraphQL API.

Changing the Server Debug Flag

The following snippets show how to change the Debug Flag for the GraphQL Server execution.

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.

Custom Fields

Register Connection to Attached Media

This code shows how to register a connection to attached media in WPGraphQL

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.

Register field as a list of strings

The below code registers a field called listOfStrings that returns a list of strings as the result:

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.

Using GraphQL Fragments in PHP

You can execute GraphQL queries in PHP. In this case, we even show using a GraphQL Fragment.

Update WPGraphQL Endpoint URL

You can modify the WPGraphQL endpoint in code with the following:

Add WP Admin Color Schemes to WPGraphQL

The following example code allows you to add WP Admin Color Schemes data to WPGraphQL.

Log WPGraphQL requests to error log

This snippet logs all WPGraphQL requests to the error log

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.

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.

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 Primary Category field for The SEO Framework plugin

The following adds a field called primaryCat field when using The SEO Framework WordPress Plugin

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.

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 field for unencoded content

The following adds a field to the NodeWithContentEditor interface to get the unencoded content for a post:

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 expensive!