From d7d3559ea4ac36d9ffb771d13ef6719bd7cf81a7 Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 4 Apr 2024 00:19:58 +0100 Subject: [PATCH] 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) --- .github/workflows/continuous_integration.yml | 30 ++++++++++++++ .gitignore | 3 ++ examples/no-framework/README.md | 11 +++++ examples/no-framework/composer.json | 28 +++++++++++++ examples/no-framework/index.php | 41 +++++++++++++++++++ .../src/Controllers/MyController.php | 14 +++++++ 6 files changed, 127 insertions(+) create mode 100644 examples/no-framework/README.md create mode 100644 examples/no-framework/composer.json create mode 100644 examples/no-framework/index.php create mode 100644 examples/no-framework/src/Controllers/MyController.php diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 9a617c5f7d..2e9c2ac198 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -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: Test 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" + run: "composer --version && composer install --no-interaction --no-progress --prefer-dist" + working-directory: "examples/${{ matrix.example }}" + - name: "Run example ${{ 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 + working-directory: "examples/${{ matrix.example }}" diff --git a/.gitignore b/.gitignore index 5c515c507c..c7bebb045e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ /.php_cs/cache /.idea +examples/*/vendor/ +examples/*/composer.lock + node_modules lib/core/metadata.js diff --git a/examples/no-framework/README.md b/examples/no-framework/README.md new file mode 100644 index 0000000000..470576554f --- /dev/null +++ b/examples/no-framework/README.md @@ -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/ +``` diff --git a/examples/no-framework/composer.json b/examples/no-framework/composer.json new file mode 100644 index 0000000000..91c4da4444 --- /dev/null +++ b/examples/no-framework/composer.json @@ -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" + } +} diff --git a/examples/no-framework/index.php b/examples/no-framework/index.php new file mode 100644 index 0000000000..a18b173dab --- /dev/null +++ b/examples/no-framework/index.php @@ -0,0 +1,41 @@ + 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"; + diff --git a/examples/no-framework/src/Controllers/MyController.php b/examples/no-framework/src/Controllers/MyController.php new file mode 100644 index 0000000000..3557ff4ce6 --- /dev/null +++ b/examples/no-framework/src/Controllers/MyController.php @@ -0,0 +1,14 @@ +