跳转到主要内容

0342-recipes-index


title: Recipes

Recipes

Recipes are practical examples that show how to customize WPGraphQL. Related actions, filters, and functions are inferred from recipe markdown (mentions of known hook and function names from docs/generated/hooks-index.json and docs/generated/functions-index.json), merged with any explicit relatedActions / relatedFilters / relatedFunctions lists in frontmatter.

Authorization

  • 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 y…
  • 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…
  • 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…
  • 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…

Custom Fields

Custom Post Types

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…

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…

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…
  • 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…
  • 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…

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…
  • 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' =>…
  • 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,…
  • 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…

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…
  • 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…
  • Fix pagination conflict with the “Advanced Taxonomy Terms Order” plugin
    • When using the Advanced Taxonomy Terms Order plugin along with WPGraphQL, you might experience some issues with paginated queries. This snippet should help correct the conflict: add_action( 'pre_get_terms', function() {…
  • Fix pagination conflict with the “Post Type Order” plugin
    • When using the Post Type Order plugin along with WPGraphQL, you might experience some issues with paginated queries. This snippet should help correct the conflict: add_action( 'pre_get_posts', function () { // access the…
  • 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'…