diff --git a/Context/SymfonyGraphQLContext.php b/Context/SymfonyGraphQLContext.php new file mode 100644 index 0000000..81435d5 --- /dev/null +++ b/Context/SymfonyGraphQLContext.php @@ -0,0 +1,28 @@ +request = $request; + } + + /** + * @return Request + */ + public function getRequest(): Request + { + return $this->request; + } +} diff --git a/Context/SymfonyRequestContextInterface.php b/Context/SymfonyRequestContextInterface.php new file mode 100644 index 0000000..d550733 --- /dev/null +++ b/Context/SymfonyRequestContextInterface.php @@ -0,0 +1,13 @@ +standardServer = $standardServer; + $this->serverConfig = $serverConfig; $this->httpMessageFactory = $httpMessageFactory ?: new DiactorosFactory(); $this->debug = $debug ?? false; } @@ -84,12 +88,17 @@ public function handleRequest(Request $request): Response $uploadMiddleware = new UploadMiddleware(); $psr7Request = $uploadMiddleware->processRequest($psr7Request); - return $this->handlePsr7Request($psr7Request); + return $this->handlePsr7Request($psr7Request, $request); } - private function handlePsr7Request(ServerRequestInterface $request): JsonResponse + private function handlePsr7Request(ServerRequestInterface $request, Request $symfonyRequest): JsonResponse { - $result = $this->standardServer->executePsrRequest($request); + // Let's put the request in the context. + $serverConfig = clone $this->serverConfig; + $serverConfig->setContext(new SymfonyGraphQLContext($symfonyRequest)); + + $standardService = new StandardServer($serverConfig); + $result = $standardService->executePsrRequest($request); if ($result instanceof ExecutionResult) { return new JsonResponse($result->toArray($this->debug), $this->decideHttpStatusCode($result)); diff --git a/Mappers/RequestParameter.php b/Mappers/RequestParameter.php new file mode 100644 index 0000000..a7ed7e6 --- /dev/null +++ b/Mappers/RequestParameter.php @@ -0,0 +1,28 @@ + $args + * @param mixed $context + * + * @return mixed + */ + public function resolve(?object $source, array $args, $context, ResolveInfo $info) + { + if (!$context instanceof SymfonyRequestContextInterface) { + throw new GraphQLException('Cannot type-hint on a Symfony Request object in your query/mutation/field. The request context must implement SymfonyRequestContextInterface.'); + } + return $context->getRequest(); + } +} diff --git a/Mappers/RequestParameterMapper.php b/Mappers/RequestParameterMapper.php new file mode 100644 index 0000000..e5548d5 --- /dev/null +++ b/Mappers/RequestParameterMapper.php @@ -0,0 +1,25 @@ +getType()->getName() === Request::class) { + return new RequestParameter(); + } + return null; + } +} \ No newline at end of file diff --git a/Resources/config/container/graphqlite.xml b/Resources/config/container/graphqlite.xml index f49f80c..1898a7f 100644 --- a/Resources/config/container/graphqlite.xml +++ b/Resources/config/container/graphqlite.xml @@ -47,10 +47,6 @@ - - - - @@ -62,6 +58,10 @@ + + + + \ No newline at end of file diff --git a/Tests/Fixtures/Controller/TestGraphqlController.php b/Tests/Fixtures/Controller/TestGraphqlController.php index 3c8d304..e435bdc 100644 --- a/Tests/Fixtures/Controller/TestGraphqlController.php +++ b/Tests/Fixtures/Controller/TestGraphqlController.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use Porpaginas\Arrays\ArrayResult; +use Symfony\Component\HttpFoundation\Request; use TheCodingMachine\GraphQLite\Annotations\FailWith; use TheCodingMachine\GraphQLite\Annotations\Logged; use TheCodingMachine\GraphQLite\Annotations\Right; @@ -102,4 +103,13 @@ public function withUserRight(): string { return 'foo'; } + + /** + * @Query() + * @return string + */ + public function getUri(Request $request): string + { + return $request->getPathInfo(); + } } diff --git a/Tests/FunctionalTest.php b/Tests/FunctionalTest.php index 1586b57..10bd9ed 100644 --- a/Tests/FunctionalTest.php +++ b/Tests/FunctionalTest.php @@ -17,7 +17,7 @@ class FunctionalTest extends TestCase { - public function testServiceWiring() + public function testServiceWiring(): void { $kernel = new GraphqliteTestingKernel('test', true); $kernel->boot(); @@ -68,7 +68,7 @@ public function testServiceWiring() ], $result); } - public function testServiceAutowiring() + public function testServiceAutowiring(): void { $kernel = new GraphqliteTestingKernel('test', true); $kernel->boot(); @@ -98,7 +98,7 @@ public function testServiceAutowiring() ], $result); } - public function testErrors() + public function testErrors(): void { $kernel = new GraphqliteTestingKernel('test', true); $kernel->boot(); @@ -134,7 +134,7 @@ public function testErrors() $this->assertSame(404, $response->getStatusCode(), $response->getContent()); } - public function testLoggedMiddleware() + public function testLoggedMiddleware(): void { $kernel = new GraphqliteTestingKernel('test', true); $kernel->boot(); @@ -155,7 +155,7 @@ public function testLoggedMiddleware() ], $result); } - public function testLoggedMiddleware2() + public function testLoggedMiddleware2(): void { $kernel = new GraphqliteTestingKernel('test', true); $kernel->boot(); @@ -185,6 +185,27 @@ public function testLoggedMiddleware2() } + public function testInjectQuery(): void + { + $kernel = new GraphqliteTestingKernel('test', true); + $kernel->boot(); + + $request = Request::create('/graphql', 'GET', ['query' => ' + { + uri + }']); + + $response = $kernel->handle($request); + + $result = json_decode($response->getContent(), true); + + $this->assertSame([ + 'data' => [ + 'uri' => '/graphql' + ] + ], $result); + } + private function logIn(ContainerInterface $container) { // put a token into the storage so the final calls can function