Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 92c29ba

Browse files
committedMar 20, 2024·
Make the "smallest working example" actually work, see #553
This example had implicit-and-undocumented dependencies on composer.json, and explicit-but-unexplained dependencies on "a container" and "a cache", leaving the installation and configuring of these as an exercise to the reader. I have changed it so that the example code can be copy-pasted verbatim, and the user will end up with an example which runs, which I find much easier to learn from :) I'm still not actually sure if I'm doing this correctly, because #553 mentioned loading classes via the PSR-4 autoloader, and I found that it only worked when I manually listed out my controller classes inside the PSR-11 container, but it works...
1 parent 256da31 commit 92c29ba

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed
 

‎website/docs/other-frameworks.mdx

+68-3
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,63 @@ $result = GraphQL::executeQuery($schema, $query, null, new Context(), $variableV
102102

103103
The smallest working example using no framework is:
104104

105-
```php
105+
```json title="composer.json"
106+
{
107+
"autoload": {
108+
"psr-4": {
109+
"App\\": "src/"
110+
}
111+
},
112+
"require": {
113+
"thecodingmachine/graphqlite": "^6",
114+
"mouf/picotainer": "^1.1",
115+
"symfony/cache": "^4.2"
116+
}
117+
}
118+
```
119+
120+
```php title="src/Controllers/MyController.php"
121+
<?php
122+
namespace App\Controllers;
123+
124+
use TheCodingMachine\GraphQLite\Annotations\Query;
125+
126+
class MyController
127+
{
128+
#[Query]
129+
public function hello(string $name): string
130+
{
131+
return 'Hello '.$name;
132+
}
133+
}
134+
```
135+
136+
```php title="index.php"
106137
<?php
107138
use GraphQL\GraphQL;
108139
use GraphQL\Type\Schema;
109140
use TheCodingMachine\GraphQLite\SchemaFactory;
110141
use TheCodingMachine\GraphQLite\Context\Context;
111142

112-
// $cache is a PSR-16 compatible cache.
113-
// $container is a PSR-11 compatible container.
143+
use Symfony\Component\Cache\Simple\ApcuCache;
144+
use Mouf\Picotainer\Picotainer;
145+
use GraphQL\Utils\SchemaPrinter;
146+
use App\Controllers\MyController;
147+
148+
require_once __DIR__ . '/vendor/autoload.php';
149+
150+
// $cache is any PSR-16 compatible cache; ApcuCache has good performance,
151+
// FilesystemCache is a low-dependency alternative if APCu is not available.
152+
$cache = new ApcuCache();
153+
154+
// $container is any PSR-11 compatible container which has
155+
// been populated with your controller classes.
156+
$container = new Picotainer([
157+
MyController::class => function() {
158+
return new MyController();
159+
},
160+
]);
161+
114162
$factory = new SchemaFactory($cache, $container);
115163
$factory->addControllerNamespace('App\\Controllers\\')
116164
->addTypeNamespace('App\\');
@@ -129,6 +177,23 @@ header('Content-Type: application/json');
129177
echo json_encode($output);
130178
```
131179

180+
Then to install dependencies and run the server:
181+
```console
182+
$ composer install
183+
$ php -S localhost:8080
184+
```
185+
186+
And check that it works:
187+
```console
188+
$ curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080
189+
```
190+
191+
If all is working correctly this should output:
192+
```json
193+
{"data":{"hello":"Hello World"}}
194+
```
195+
196+
132197
## PSR-15 Middleware
133198

134199
When using a framework, you will need a way to route your HTTP requests to the `webonyx/graphql-php` library.

0 commit comments

Comments
 (0)
Please sign in to comment.