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.

Page Siblings Connection

Below is a connection that adds the ability to query page siblings (i.e. pages that share the same parent)

View Recipe

Allow login mutation to be public when the endpoint is fully restricted

If you’ve configured your WPGraphQL settings to “Limit the execution of GraphQL operations to authenticated requests”, this will block all root operations unless the user making the request is already authenticated.

If you’re using a GraphQL mutation to authenticate, such as the one provided by WPGraphQL JWT Authentication, you might want to allow the login mutation to still be executable by public users, even if the rest of the API is restricted.

This snippet allows you to “allow” the login mutation when all other root operations are restricted.

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 want in response if the homepage is a page, or if the homepage is a ContentType node.

View Recipe

Make all Users Public

The following snippets allow for Users with no published content to be shown in public (non-authenticated) WPGraphQL query results.

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.

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.

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 not assigned to menu locations to public GraphQL Queries, you can use the following snippet:

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.

View Recipe

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.

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.

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.

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.

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.

View Recipe

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!

View Recipe