Skip to content

Commit

Permalink
Add psr-15 example to examples folder
Browse files Browse the repository at this point in the history
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
shish committed Apr 21, 2024
1 parent 1a8dd39 commit a705281
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
example: ['no-framework']
example: ['no-framework', 'psr-15']
fail-fast: false
steps:
- name: "Checkout"
Expand Down
11 changes: 11 additions & 0 deletions examples/psr-15/README.md
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
```
31 changes: 31 additions & 0 deletions examples/psr-15/composer.json
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"
}
}
40 changes: 40 additions & 0 deletions examples/psr-15/config/container.php
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();
},
]);

30 changes: 30 additions & 0 deletions examples/psr-15/index.php
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();

13 changes: 13 additions & 0 deletions examples/psr-15/src/Controllers/MyController.php
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;
}
}

0 comments on commit a705281

Please sign in to comment.