Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the "smallest working example" actually work, see #553 #663

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions website/docs/other-frameworks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,63 @@ $result = GraphQL::executeQuery($schema, $query, null, new Context(), $variableV

The smallest working example using no framework is:

```php
```json title="composer.json"
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"require": {
"thecodingmachine/graphqlite": "^6",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use an asterisk, * for the version here. We're now at 7.0 - no need to specify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, when I upgrade to 7.0, this "working example" stops working again :(

6.2

$ curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080
{"data":{"hello":"Hello World"}}

7.0

$ curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080
{"errors":[{"message":"Cannot query field \"hello\" on type \"Query\".","locations":[{"line":1,"column":3}]}]}%

"mouf/picotainer": "^1.1",
"symfony/cache": "^4.2"
}
}
```

```php title="src/Controllers/MyController.php"
<?php
namespace App\Controllers;

use TheCodingMachine\GraphQLite\Annotations\Query;

class MyController
{
#[Query]
public function hello(string $name): string
{
return 'Hello '.$name;
}
}
```

```php title="index.php"
<?php
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use TheCodingMachine\GraphQLite\SchemaFactory;
use TheCodingMachine\GraphQLite\Context\Context;

// $cache is a PSR-16 compatible cache.
// $container is a PSR-11 compatible container.
use Symfony\Component\Cache\Simple\ApcuCache;
use Mouf\Picotainer\Picotainer;
use GraphQL\Utils\SchemaPrinter;
use App\Controllers\MyController;

require_once __DIR__ . '/vendor/autoload.php';

// $cache is any PSR-16 compatible cache; ApcuCache has good performance,
// FilesystemCache is a low-dependency alternative if APCu is not available.
$cache = new ApcuCache();

// $container is any PSR-11 compatible container which has
// been populated with your controller classes.
$container = new Picotainer([
MyController::class => function() {
return new MyController();
},
]);

$factory = new SchemaFactory($cache, $container);
$factory->addControllerNamespace('App\\Controllers\\')
->addTypeNamespace('App\\');
Expand All @@ -129,6 +177,23 @@ header('Content-Type: application/json');
echo json_encode($output);
```

Then to install dependencies and run the server:
```console
$ composer install
$ php -S localhost:8080
```

And check that it works:
```console
$ curl -X POST -d '{"query":"{ hello(name: \"World\") }"}' -H "Content-Type: application/json" http://localhost:8080
```

If all is working correctly this should output:
```json
{"data":{"hello":"Hello World"}}
```


## PSR-15 Middleware

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