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

Add automatically-verified working examples #668

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,33 @@ jobs:
fail_ci_if_error: false # optional (default = false) - Need CODECOV_TOKEN
# Do not upload in forks, and only on php8.3, latest deps
if: ${{ github.repository == 'thecodingmachine/graphqlite' && matrix.php-version == '8.3' && matrix.install-args == '' }}

examples:
name: Check Examples
runs-on: ubuntu-latest
strategy:
matrix:
example: ['no-framework']
fail-fast: false
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.2"
tools: composer:v2
- name: "Install dependencies with composer"
working-directory: "examples/${{ matrix.example }}"
run: "composer --version && composer install --no-interaction --no-progress --prefer-dist"
- name: "Run example ${{ matrix.example }}"
working-directory: "examples/${{ matrix.example }}"
run: |
php -S localhost:8080 &
sleep 3
curl --silent -X POST -H "Content-Type: application/json" \
-d '{"query":"{ hello(name: \"World\") }"}' \
http://localhost:8080/graphql -o output.json
grep -q '"data":{"hello":"Hello World"}' output.json || \
(cat output.json && false)
kill %1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
/.php_cs/cache
/.idea

examples/*/vendor/
examples/*/composer.lock

node_modules

lib/core/metadata.js
Expand Down
11 changes: 11 additions & 0 deletions examples/no-framework/README.md
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/
```
28 changes: 28 additions & 0 deletions examples/no-framework/composer.json
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"
}
}
41 changes: 41 additions & 0 deletions examples/no-framework/index.php
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";

14 changes: 14 additions & 0 deletions examples/no-framework/src/Controllers/MyController.php
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;
}
}

Loading