From 64b74d41007a188ff0694f2c97cad8e2261a29d5 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 15 Dec 2022 13:36:47 +1300 Subject: [PATCH] Add alias route to allow for multiple SDK method names --- app/controllers/api/graphql.php | 48 ++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/graphql.php b/app/controllers/api/graphql.php index 869d860af..f18a391d2 100644 --- a/app/controllers/api/graphql.php +++ b/app/controllers/api/graphql.php @@ -57,13 +57,59 @@ App::get('/v1/graphql') ->json($output); }); +App::post('/v1/graphql/mutation') + ->desc('GraphQL Endpoint') + ->groups(['graphql']) + ->label('scope', 'graphql') + ->label('docs', false) + ->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) + ->label('sdk.namespace', 'graphql') + ->label('sdk.method', 'mutation') + ->label('sdk.methodType', 'graphql') + ->label('sdk.description', '/docs/references/graphql/post.md') + ->label('sdk.parameters', [ + 'query' => ['default' => [], 'validator' => new JSON(), 'description' => 'The query or queries to execute.', 'optional' => false], + ]) + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_ANY) + ->label('abuse-limit', 60) + ->label('abuse-time', 60) + ->inject('request') + ->inject('response') + ->inject('schema') + ->inject('promiseAdapter') + ->action(function (Request $request, Response $response, GQLSchema $schema, Adapter $promiseAdapter) { + $query = $request->getParams(); + + if ($request->getHeader('x-sdk-graphql') == 'true') { + $query = $query['query']; + } + + $type = $request->getHeader('content-type'); + + if (\str_starts_with($type, 'application/graphql')) { + $query = parseGraphql($request); + } + + if (\str_starts_with($type, 'multipart/form-data')) { + $query = parseMultipart($query, $request); + } + + $output = execute($schema, $promiseAdapter, $query); + + $response + ->setStatusCode(Response::STATUS_CODE_OK) + ->json($output); + }); + App::post('/v1/graphql') ->desc('GraphQL Endpoint') ->groups(['graphql']) ->label('scope', 'graphql') ->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'graphql') - ->label('sdk.method', 'post') + ->label('sdk.method', 'query') ->label('sdk.methodType', 'graphql') ->label('sdk.description', '/docs/references/graphql/post.md') ->label('sdk.parameters', [