-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add psr-15 example to examples folder
This example is broken and I don't know why (maybe somebody who knows what they are doing can explain?). But at least now we can _see_ that it is broken, which is better than just silently misleading people...
- Loading branch information
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
PSR-15 Integration Example | ||
========================== | ||
|
||
``` | ||
composer install | ||
php -S 127.0.0.1:8080 | ||
``` | ||
|
||
``` | ||
curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080/graphql | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"thecodingmachine/graphqlite": "@dev", | ||
"laminas/laminas-diactoros": "^2", | ||
"laminas/laminas-stratigility": "^3", | ||
"laminas/laminas-httphandlerrunner": "^2", | ||
"mouf/picotainer": "^1.1", | ||
"symfony/cache": "^4.2" | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "path", | ||
"url": "tmp-graphqlite", | ||
"options": { | ||
"symlink": true | ||
} | ||
} | ||
], | ||
"scripts": { | ||
"symlink-package": [ | ||
"rm -rf tmp-graphqlite && ln -s -f ../../ tmp-graphqlite" | ||
], | ||
"pre-install-cmd": "@symlink-package", | ||
"pre-update-cmd": "@symlink-package" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use GraphQL\Type\Schema; | ||
use Mouf\Picotainer\Picotainer; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\SimpleCache\CacheInterface; | ||
use Symfony\Component\Cache\Simple\FilesystemCache; | ||
use TheCodingMachine\GraphQLite\Http\Psr15GraphQLMiddlewareBuilder; | ||
use TheCodingMachine\GraphQLite\SchemaFactory; | ||
use Laminas\Stratigility\MiddlewarePipe; | ||
|
||
// Picotainer is a minimalist PSR-11 container. | ||
return new Picotainer([ | ||
MiddlewarePipe::class => function(ContainerInterface $container) { | ||
$pipe = new MiddlewarePipe(); | ||
$pipe->pipe($container->get(WebonyxGraphqlMiddleware::class)); | ||
return $pipe; | ||
}, | ||
// The WebonyxGraphqlMiddleware is a PSR-15 compatible | ||
// middleware that exposes Webonyx schemas. | ||
WebonyxGraphqlMiddleware::class => function(ContainerInterface $container) { | ||
$builder = new Psr15GraphQLMiddlewareBuilder($container->get(Schema::class)); | ||
return $builder->createMiddleware(); | ||
}, | ||
CacheInterface::class => function() { | ||
return new FilesystemCache(); | ||
}, | ||
Schema::class => function(ContainerInterface $container) { | ||
// The magic happens here. We create a schema using GraphQLite SchemaFactory. | ||
$factory = new SchemaFactory($container->get(CacheInterface::class), $container); | ||
$factory->addControllerNamespace('App\\Controllers'); | ||
$factory->addTypeNamespace('App'); | ||
return $factory->createSchema(); | ||
}, | ||
// We declare the controller in the container. | ||
MyController::class => function() { | ||
return new MyController(); | ||
}, | ||
]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Laminas\Diactoros\Response; | ||
use Laminas\Diactoros\ServerRequest; | ||
use Laminas\Diactoros\ServerRequestFactory; | ||
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter; | ||
use Laminas\Stratigility\Middleware\ErrorResponseGenerator; | ||
use Laminas\Stratigility\MiddlewarePipe; | ||
use Laminas\Diactoros\Server; | ||
use Laminas\HttpHandlerRunner\RequestHandlerRunner; | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
$container = require 'config/container.php'; | ||
|
||
$serverRequestFactory = [ServerRequestFactory::class, 'fromGlobals']; | ||
|
||
$errorResponseGenerator = function (Throwable $e) { | ||
$generator = new ErrorResponseGenerator(); | ||
return $generator($e, new ServerRequest(), new Response()); | ||
}; | ||
|
||
$runner = new RequestHandlerRunner( | ||
$container->get(MiddlewarePipe::class), | ||
new SapiStreamEmitter(), | ||
$serverRequestFactory, | ||
$errorResponseGenerator | ||
); | ||
$runner->run(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace App\Controllers; | ||
|
||
use TheCodingMachine\GraphQLite\Annotations\Query; | ||
|
||
class MyController | ||
{ | ||
#[Query] | ||
public function hello(string $name): string | ||
{ | ||
return 'Hello '.$name; | ||
} | ||
} |