-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "no framework" example to examples folder
Having an example as actual code rather than as out-of-context snippets inside a text file means that we can run the code, and ensure that it actually works Submitting mostly for discussion to see if people like this idea TODO: - have a github action which runs the code and ensures that the "hello world" query works (thus ensuring that the example code is correct) - install graphqlite from parent folder instead of downloading the published 6.X (thus ensuring that the example is always in-sync with the library it is an example of) - update the documentation to reference the files in this repository, rather than embedding hard-coded out-of-context snippets into the .mdx files (thus ensuring that the documentation stays in-sync with the known-good example code)
- Loading branch information
Showing
6 changed files
with
127 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
/.php_cs/cache | ||
/.idea | ||
|
||
examples/*/vendor/ | ||
examples/*/composer.lock | ||
|
||
node_modules | ||
|
||
lib/core/metadata.js | ||
|
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 @@ | ||
No-Framework 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/ | ||
``` |
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,28 @@ | ||
{ | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"thecodingmachine/graphqlite": "@dev", | ||
"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,41 @@ | ||
<?php | ||
use GraphQL\GraphQL; | ||
use GraphQL\Type\Schema; | ||
use TheCodingMachine\GraphQLite\SchemaFactory; | ||
use TheCodingMachine\GraphQLite\Context\Context; | ||
|
||
use Symfony\Component\Cache\Simple\FilesystemCache; | ||
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. | ||
$cache = new FilesystemCache(); | ||
|
||
// $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\\'); | ||
|
||
$schema = $factory->createSchema(); | ||
|
||
$rawInput = file_get_contents('php://input'); | ||
$input = json_decode($rawInput, true); | ||
$query = $input['query']; | ||
$variableValues = isset($input['variables']) ? $input['variables'] : null; | ||
|
||
$result = GraphQL::executeQuery($schema, $query, null, new Context(), $variableValues); | ||
$output = $result->toArray(); | ||
|
||
header('Content-Type: application/json'); | ||
echo json_encode($output) . "\n"; | ||
|
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,14 @@ | ||
<?php | ||
namespace App\Controllers; | ||
|
||
use TheCodingMachine\GraphQLite\Annotations\Query; | ||
|
||
class MyController | ||
{ | ||
#[Query] | ||
public function hello(string $name): string | ||
{ | ||
return 'Hello '.$name; | ||
} | ||
} | ||
|