From f92eadd65d6560cac19ea95cd6d8d08295403ab9 Mon Sep 17 00:00:00 2001 From: Martijn Swinkels Date: Mon, 30 Jun 2025 14:07:07 +0200 Subject: [PATCH 1/3] Allow adding of GraphQL mutations --- src/GraphQL/DefaultSchema.php | 7 ++++++- src/GraphQL/Middleware/CacheResponse.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/GraphQL/DefaultSchema.php b/src/GraphQL/DefaultSchema.php index 6cde23ac0e..1aaed9f1a1 100644 --- a/src/GraphQL/DefaultSchema.php +++ b/src/GraphQL/DefaultSchema.php @@ -45,7 +45,7 @@ public function getConfig() { return [ 'query' => $this->getQueries(), - 'mutation' => [], + 'mutation' => $this->getMutations(), 'middleware' => $this->getMiddleware(), 'method' => ['GET', 'POST'], ]; @@ -82,4 +82,9 @@ private function getMiddleware() GraphQL::getExtraMiddleware() ); } + + private function getMutations() + { + return config('statamic.graphql.mutations', []); + } } diff --git a/src/GraphQL/Middleware/CacheResponse.php b/src/GraphQL/Middleware/CacheResponse.php index 45b6360f5a..1a82012a3c 100644 --- a/src/GraphQL/Middleware/CacheResponse.php +++ b/src/GraphQL/Middleware/CacheResponse.php @@ -13,6 +13,10 @@ public function handle($request, Closure $next) return $next($request); } + if ($this->isMutation($request)) { + return $next($request); + } + $cache = app(ResponseCache::class); if ($response = $cache->get($request)) { @@ -25,4 +29,10 @@ public function handle($request, Closure $next) return $response; } + + protected function isMutation($request): bool + { + $query = ltrim(strtolower($request->get('query', ''))); + return str_starts_with($query, 'mutation'); + } } From 6ea1b5610f78a94b946d62db8c7efef287554b4d Mon Sep 17 00:00:00 2001 From: Martijn Swinkels Date: Mon, 30 Jun 2025 14:13:48 +0200 Subject: [PATCH 2/3] Add mutations to graphql config file --- config/graphql.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/config/graphql.php b/config/graphql.php index 132e71f355..02f8258ce3 100644 --- a/config/graphql.php +++ b/config/graphql.php @@ -42,6 +42,20 @@ // ], + /* + |-------------------------------------------------------------------------- + | Mutations + |-------------------------------------------------------------------------- + | + | Here you may list mutations to be added to the Statamic schema. + | + | https://statamic.dev/graphql#custom-mutations + */ + + 'mutations' => [ + // + ], + /* |-------------------------------------------------------------------------- | Middleware From 8524b64a728a4bd68cb86cfbdc1d960e8fee179b Mon Sep 17 00:00:00 2001 From: Martijn Swinkels Date: Mon, 30 Jun 2025 15:31:24 +0200 Subject: [PATCH 3/3] Code style fix --- src/GraphQL/Middleware/CacheResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GraphQL/Middleware/CacheResponse.php b/src/GraphQL/Middleware/CacheResponse.php index 1a82012a3c..73b5b14636 100644 --- a/src/GraphQL/Middleware/CacheResponse.php +++ b/src/GraphQL/Middleware/CacheResponse.php @@ -33,6 +33,7 @@ public function handle($request, Closure $next) protected function isMutation($request): bool { $query = ltrim(strtolower($request->get('query', ''))); + return str_starts_with($query, 'mutation'); } }