Enable query locking for WPGraphQL by implementing persisted GraphQL queries.

Overview

πŸ”’ WP GraphQL Lock

This plugin enables query locking for WPGraphQL by implementing persisted GraphQL queries.

Persisted GraphQL queries allow a GraphQL client to optimistically send a hash of the query instead of the full query; if the server has seen the query before, it can satisfy the request.

Once the server knowns all the possible queries the plugin can lock it down disallowing any unwanted queries that are possibly malicious. This can greatly improve the server security and can even protect against unpatched vulnerabilities in some cases.

Alternatively you can pre-generate the query IDs from your client source code with the GraphQL Code Generator plugin and load the IDs with the graphql_lock_load_query filter.

In addition to enabling query locking this saves network overhead and makes it possible to move to GET requests instead of POST. The primary benefit of GET requests is that they can be easily cached at the edge (e.g., with Varnish, nginx etc.).

This plugin requires WPGraphQL 0.2.0 or newer.

Compatibility

Apollo Client provides an easy implementation of persisted queries:

https://github.com/apollographql/apollo-link-persisted-queries#automatic-persisted-queries

This plugin aims to be compatible with that implementation, but will work with any client that sends a queryId alongside the query. Make sure your client also sends operationName with the optimistic request.

Implementation

When the client provides a query hash or ID, that query will be persisted in a custom post type. By default, this post type will be visible in the dashboard only to admins.

Query IDs are case-insensitive (i.e., MyQuery and myquery are equivalent).

Installation

If you use composer you can install it from packagist

composer require valu/wp-graphql-lock

Otherwise you can clone it from Github to your plugins using the stable branch

cd wp-content/plugins
git clone --branch stable https://github.com/valu-digital/wp-graphql-lock.git

Filters

graphql_lock_load_query

  • Load the queries from a custom location
  • The query ID is passed as the second parameter

Example:

add_filter( 'graphql_lock_load_query', function( string $query, string $query_id ) {
    $queries = json_decode( file_get_contents( __DIR__ . '/.persisted-query-ids/server.json' ), true );
    return $queries[ $query_id ] ?? null;
}, 10, 2 );

Note: You should prefer using hidden directories / files to avoid exposing the lock file via your webserver.

graphql_lock_post_type

  • Default: 'graphql_query'
  • The custom post type used to persist queries. If empty, queries will not be persisted.

graphql_lock_show_in_graphql

  • Default: false
  • Whether the custom post type will itself be exposed via GraphQL. Enabling allows insight into which queries are persisted.
query PersistedQueryQuery {
  persistedQueries {
    nodes {
      id
      title
      content(format: RAW)
    }
  }
}

If you'd like to further customize the custom post type, filter register_post_type_args.

Lock mode

When it's active no new queries can be saved and only the saved ones can be used. This can greatly improve security as attackers cannot send arbitrary queries to the endpoint.

Lock mode can be activated by setting graphql_lock_locked option to true:

update_option( 'graphql_lock_locked', true );
add_filter( 'option_graphql_lock_locked', function() {
    return 'production' === WP_ENV;
}, 10 , 1 );

Settings

There's a settings screen for managing the option

settings

Acknowledgements

This plugin is based on the Quartz persisted Queries plugin.

Contributing

Read CONTRIBUTING.md

You might also like...
Adds Settings to the Custom Post Type UI plugin to show Post Types in WPGraphQL

DEPRECATION NOTICE πŸš€ Custom Post Type UI v1.9.0 introduced formal support for WPGraphQL!!! πŸ‘‹ With that, this plugin is being deprecated and will no

WPGraphQL FacetWP integration plguin
WPGraphQL FacetWP integration plguin

WPGraphQL-FacetWP: WPGraphQL provider for FacetWP Quick Install Download and install like any WordPress plugin. Documentation The WPGraphQL documentat

WPGraphQL for Meta Box

WPGraphQL-MetaBox: WPGraphQL provider for Meta Box Quick Install Download and install like any WordPress plugin. Documentation The WPGraphQL documenta

 This is an extension to the WPGraphQL plugin for Yoast SEO
This is an extension to the WPGraphQL plugin for Yoast SEO

WPGraphQl Yoast SEO Plugin Please note version 14 of the Yoast Plugin is a major update. If you are stuck on version of Yoast before V14 then use v3 o

Add WooCommerce support and functionality to your WPGraphQL server

WPGraphQL WooCommerce (WooGraphQL) Docs β€’ AxisTaylor β€’ Join Slack Quick Install Install & activate WooCommerce Install & activate WPGraphQL Download t

Adds `tax_query` support to postObject connection queries using WP_Query

WPGraphQL Tax Query This plugin adds Tax_Query support to the WP GraphQL Plugin for postObject query args (WP_Query). Pre-req's Using this plugin requ

Querycase provides a convenient, fluent interface for creating and running database queries in WordPress.

Querycase database for WordPress Dependency-free library to create SQL Queries in WordPress. Explore the documentation β†’ ℹ️ About Querycase Querycase

:rocket: GraphQL API for WordPress

WPGraphQL WPGraphQL is a free, open-source WordPress plugin that provides an extendable GraphQL schema and API for any WordPress site. Below are some

Makes WP GraphQL's authetication "just work". It does this by customizing the CORS headers.

WP GraphQL CORS The primary purpose of this plugin is to make the WP GraphQL plugin authentication "just work". It does this by allowing you set the C

Comments
  • fix missing argument to load method of Loader

    fix missing argument to load method of Loader

    When recording is enabled and a graphql request comes in that should be persisted, the call to load (to see whether the query id already exists or not) does not have the correct amount of arguments. The call needs to pass the operation name to load.

    This PR fixes this and adds a test for it. Let me know if you would like me to take a different approach to the test, specifically the enableRecording()/resetRecording() setup. Wasn't sure how you wanted to handle database mutations for the unit tests @esamattis

    opened by michaellopez 4
  • Intrusive Admin Notices

    Intrusive Admin Notices

    The undismissable admin notices are intrusive in the CMS. Have clients who have access to the CMS and we don't want these messages to appear. We manage security to the endpoints elsewhere and want to remove the "The API is open. Anyone can send any query to it!" and "Query recording is enabled" notices without having to edit the plugin code. Can this be considered for future releases?

    image

    opened by CallumMitchellRV 2
  • Added support for batch GraphQL queries

    Added support for batch GraphQL queries

    Added check if the request is a batch query (array) or a single GraphQL query according to https://www.apollographql.com/blog/batching-client-graphql-queries-a685f5bcd41b/

    opened by vvasiloud 0
Releases(v0.1.4)
An WPGraphQL extension that adds SearchWP's query functionality to the GraphQL server

QL Search What is QL Search? An extension that integrates SearchWP into WPGraphQL. Quick Install Install & activate SearchWP v3.1.9+ Install & activat

Funkhaus 11 May 5, 2022
WPGraphQL Extension: Adds "meta_query" support to postObject connection queries using WP_Query

WPGraphQL Meta Query This plugin adds Meta_Query support to the WP GraphQL Plugin for postObject query args. Why is this an extension and not part of

WPGraphQL 42 Nov 10, 2022
Wordpress wrapper to expose Carbon Fields to WpGraphQL queries.

WpGraphQLCrb A Wordpress wrapper to expose Carbon Fields to WpGraphQL queries. Important This is just the first version. There is a lot of work to be

Matheus Paiva 16 Aug 19, 2022
Query gutenberg blocks with wp-graphql

WPGraphQL Gutenberg Query gutenberg blocks through wp-graphql Usage Docs Join our community through WpGraphQL Slack Install Requires PHP 7.0+ Requires

null 270 Jan 3, 2023
Authentication for WPGraphQL using JWT (JSON Web Tokens)

WPGraphQL JWT Authentication This plugin extends the WPGraphQL plugin to provide authentication using JWT (JSON Web Tokens) JSON Web Tokens are an ope

WPGraphQL 268 Dec 31, 2022
Send emails via mutation using WpGraphQl

WPGraphQL Send Email Plugin One of the simple things about a traditional WordPress sites is sending emails, this plugin makes it easy to do this via a

Ashley Hitchcock 18 Aug 21, 2022
Structured content blocks for WPGraphQL

WPGraphQL Content Blocks (Structured Content) This WPGraphQL plugin returns a WordPress post’s content as a shallow tree of blocks and allows for some

Quartz 72 Oct 3, 2022
WPGraphQL Polylang Extension for WordPress

WPGraphQL Polylang Extension Extend WPGraphQL schema with language data from the Polylang plugin. Features For posts and terms (custom ones too!) Adds

Valu Digital 102 Dec 29, 2022
a wordpress plugin that improves wpgraphql usage together with wpml

WPGraphQL WPML Extension Contributors: rburgst Stable tag: 1.0.6 Tested up to: 5.6.1 Requires at least: 4.9 Requires PHP: 7.0 Requires WPGraphQL: 0.8.

null 42 Dec 15, 2022
WPGraphQL for Advanced Custom Fields

WPGraphQL for Advanced Custom Fields WPGraphQL for Advanced Custom Fields automatically exposes your ACF fields to the WPGraphQL Schema. Install and A

WPGraphQL 558 Jan 8, 2023