Skip to content

Commit 521bce9

Browse files
committed
Add psr-15 example to examples folder
1 parent e3d5d1a commit 521bce9

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-1
lines changed

.github/workflows/continuous_integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
runs-on: ubuntu-latest
9696
strategy:
9797
matrix:
98-
example: ['no-framework']
98+
example: ['no-framework', 'psr-15']
9999
fail-fast: false
100100
steps:
101101
- name: "Checkout"

examples/psr-15/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
PSR-15 Integration Example
2+
==========================
3+
4+
```
5+
composer install
6+
php -S 127.0.0.1:8080
7+
```
8+
9+
```
10+
curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080/graphql
11+
```

examples/psr-15/composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"App\\": "src/"
5+
}
6+
},
7+
"require": {
8+
"thecodingmachine/graphqlite": "@dev",
9+
"laminas/laminas-diactoros": "^2",
10+
"laminas/laminas-stratigility": "^3",
11+
"laminas/laminas-httphandlerrunner": "^2",
12+
"mouf/picotainer": "^1.1",
13+
"symfony/cache": "^4.2"
14+
},
15+
"repositories": [
16+
{
17+
"type": "path",
18+
"url": "tmp-graphqlite",
19+
"options": {
20+
"symlink": true
21+
}
22+
}
23+
],
24+
"scripts": {
25+
"symlink-package": [
26+
"rm -rf tmp-graphqlite && ln -s -f ../../ tmp-graphqlite"
27+
],
28+
"pre-install-cmd": "@symlink-package",
29+
"pre-update-cmd": "@symlink-package"
30+
}
31+
}

examples/psr-15/config/container.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use GraphQL\Type\Schema;
4+
use Mouf\Picotainer\Picotainer;
5+
use Psr\Container\ContainerInterface;
6+
use Psr\SimpleCache\CacheInterface;
7+
use Symfony\Component\Cache\Simple\FilesystemCache;
8+
use TheCodingMachine\GraphQLite\Http\Psr15GraphQLMiddlewareBuilder;
9+
use TheCodingMachine\GraphQLite\SchemaFactory;
10+
use Laminas\Stratigility\MiddlewarePipe;
11+
12+
// Picotainer is a minimalist PSR-11 container.
13+
return new Picotainer([
14+
MiddlewarePipe::class => function(ContainerInterface $container) {
15+
$pipe = new MiddlewarePipe();
16+
$pipe->pipe($container->get(WebonyxGraphqlMiddleware::class));
17+
return $pipe;
18+
},
19+
// The WebonyxGraphqlMiddleware is a PSR-15 compatible
20+
// middleware that exposes Webonyx schemas.
21+
WebonyxGraphqlMiddleware::class => function(ContainerInterface $container) {
22+
$builder = new Psr15GraphQLMiddlewareBuilder($container->get(Schema::class));
23+
return $builder->createMiddleware();
24+
},
25+
CacheInterface::class => function() {
26+
return new FilesystemCache();
27+
},
28+
Schema::class => function(ContainerInterface $container) {
29+
// The magic happens here. We create a schema using GraphQLite SchemaFactory.
30+
$factory = new SchemaFactory($container->get(CacheInterface::class), $container);
31+
$factory->addControllerNamespace('App\\Controllers');
32+
$factory->addTypeNamespace('App');
33+
return $factory->createSchema();
34+
},
35+
// We declare the controller in the container.
36+
MyController::class => function() {
37+
return new MyController();
38+
},
39+
]);
40+

examples/psr-15/index.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Laminas\Diactoros\Response;
4+
use Laminas\Diactoros\ServerRequest;
5+
use Laminas\Diactoros\ServerRequestFactory;
6+
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter;
7+
use Laminas\Stratigility\Middleware\ErrorResponseGenerator;
8+
use Laminas\Stratigility\MiddlewarePipe;
9+
use Laminas\Diactoros\Server;
10+
use Laminas\HttpHandlerRunner\RequestHandlerRunner;
11+
12+
require_once __DIR__ . '/vendor/autoload.php';
13+
14+
$container = require 'config/container.php';
15+
16+
$serverRequestFactory = [ServerRequestFactory::class, 'fromGlobals'];
17+
18+
$errorResponseGenerator = function (Throwable $e) {
19+
$generator = new ErrorResponseGenerator();
20+
return $generator($e, new ServerRequest(), new Response());
21+
};
22+
23+
$runner = new RequestHandlerRunner(
24+
$container->get(MiddlewarePipe::class),
25+
new SapiStreamEmitter(),
26+
$serverRequestFactory,
27+
$errorResponseGenerator
28+
);
29+
$runner->run();
30+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace App\Controllers;
3+
4+
use TheCodingMachine\GraphQLite\Annotations\Query;
5+
6+
class MyController
7+
{
8+
#[Query]
9+
public function hello(string $name): string
10+
{
11+
return 'Hello '.$name;
12+
}
13+
}

0 commit comments

Comments
 (0)