First GraphQL parser POC
This commit is contained in:
parent
a050b7ebb8
commit
77d0f2b37a
3 changed files with 170 additions and 10 deletions
|
@ -1,6 +1,14 @@
|
|||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
global $utopia;
|
||||
use GraphQL\GraphQL;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
|
||||
global $utopia, $request, $response;
|
||||
|
||||
/**
|
||||
* TODO:
|
||||
|
@ -10,13 +18,108 @@ global $utopia;
|
|||
* 4. Handle errors if any
|
||||
* 5. Returen JSON response
|
||||
* 6. Write tests!
|
||||
*
|
||||
* Demo
|
||||
* curl -H "Content-Type: application/json" http://localhost/v1/graphql -d '{"query": "query { echo(message: \"Hello World\") }" }'
|
||||
*
|
||||
* Explorers:
|
||||
* - https://shopify.dev/tools/graphiql-admin-api
|
||||
* - https://developer.github.com/v4/explorer/
|
||||
* - http://localhost:4000
|
||||
*
|
||||
* Docs
|
||||
* - Overview
|
||||
* - Query
|
||||
* - Mutation
|
||||
* - Objects
|
||||
*/
|
||||
|
||||
$utopia->post('/v1/graphql')
|
||||
->desc('GraphQL Endpoint')
|
||||
->label('scope', 'public')
|
||||
->action(
|
||||
function () {
|
||||
throw new Exception('GraphQL support is coming soon!', 502);
|
||||
function () use ($request, $response) {
|
||||
$userType = new ObjectType([
|
||||
'name' => 'User',
|
||||
'fields' => [
|
||||
'name' => [
|
||||
'type' => Type::string(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$queryType = new ObjectType([
|
||||
'name' => 'Query',
|
||||
'fields' => [
|
||||
'echo' => [
|
||||
'type' => Type::string(),
|
||||
'args' => [
|
||||
'message' => ['type' => Type::string()],
|
||||
],
|
||||
'resolve' => function ($rootValue, $args) {
|
||||
return $rootValue['prefix'] . $args['message'];
|
||||
}
|
||||
],
|
||||
'users' => [
|
||||
'type' => Type::listOf($userType),
|
||||
//'type' => $userType,
|
||||
'args' => [
|
||||
'message' => ['type' => Type::string()],
|
||||
],
|
||||
'resolve' => function ($rootValue, $args) {
|
||||
return ['name' => 'Eldad Fux'];
|
||||
return [
|
||||
['name' => 'Eldad Fux'],
|
||||
['name' => 'Sharon Kapon'],
|
||||
];
|
||||
}
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$mutationType = new ObjectType([
|
||||
'name' => 'Mutation',
|
||||
'fields' => [
|
||||
'sum' => [
|
||||
'type' => Type::int(),
|
||||
'args' => [
|
||||
'x' => ['type' => Type::int()],
|
||||
'y' => ['type' => Type::int()],
|
||||
],
|
||||
'resolve' => function ($calc, $args) {
|
||||
return $args['x'] + $args['y'];
|
||||
},
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$schema = new Schema([
|
||||
'query' => $queryType,
|
||||
'mutation' => $mutationType,
|
||||
]);
|
||||
|
||||
$query = $request->getPayload('query', '');
|
||||
$variables = $request->getPayload('variables', null);
|
||||
|
||||
try {
|
||||
$rootValue = [];
|
||||
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables);
|
||||
$output = $result->toArray();
|
||||
} catch (\Exception $error) {
|
||||
$output = [
|
||||
'errors' => [
|
||||
[
|
||||
'message' => $error->getMessage().'xxx',
|
||||
'code' => $error->getCode(),
|
||||
'file' => $error->getFile(),
|
||||
'line' => $error->getLine(),
|
||||
'trace' => $error->getTrace(),
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
$response->json($output);
|
||||
echo "\n"; //TODO REMOVE THIS
|
||||
}
|
||||
);
|
||||
);
|
|
@ -43,6 +43,7 @@
|
|||
"utopia-php/domains": "0.2.*",
|
||||
|
||||
"resque/php-resque": "1.3.6",
|
||||
"webonyx/graphql-php": "14.0.0",
|
||||
"geoip2/geoip2": "2.9.0",
|
||||
"piwik/device-detector": "3.5.1",
|
||||
"dragonmantank/cron-expression": "2.2.0",
|
||||
|
|
68
composer.lock
generated
68
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "9deec50e5a99197511b5efbcaa9593bd",
|
||||
"content-hash": "b2678886d7c1aa5f4edfdd1ad1558f14",
|
||||
"packages": [
|
||||
{
|
||||
"name": "appwrite/php-clamav",
|
||||
|
@ -1546,16 +1546,16 @@
|
|||
},
|
||||
{
|
||||
"name": "utopia-php/domains",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/domains.git",
|
||||
"reference": "1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73"
|
||||
"reference": "98e85296867a59c9d712d6ed768a5c5b2b297b43"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/domains/zipball/1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73",
|
||||
"reference": "1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73",
|
||||
"url": "https://api.github.com/repos/utopia-php/domains/zipball/98e85296867a59c9d712d6ed768a5c5b2b297b43",
|
||||
"reference": "98e85296867a59c9d712d6ed768a5c5b2b297b43",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1592,7 +1592,7 @@
|
|||
"upf",
|
||||
"utopia"
|
||||
],
|
||||
"time": "2020-02-23T07:40:02+00:00"
|
||||
"time": "2020-06-20T11:47:04+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/framework",
|
||||
|
@ -1730,6 +1730,62 @@
|
|||
"utopia"
|
||||
],
|
||||
"time": "2020-06-20T11:46:06+00:00"
|
||||
},
|
||||
{
|
||||
"name": "webonyx/graphql-php",
|
||||
"version": "v14.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/webonyx/graphql-php.git",
|
||||
"reference": "45d84562433613c6ee41e2421c95409d2b2664d6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/webonyx/graphql-php/zipball/45d84562433613c6ee41e2421c95409d2b2664d6",
|
||||
"reference": "45d84562433613c6ee41e2421c95409d2b2664d6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"ext-mbstring": "*",
|
||||
"php": "^7.1||^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/amp": "^2.3",
|
||||
"doctrine/coding-standard": "^6.0",
|
||||
"nyholm/psr7": "^1.2",
|
||||
"phpbench/phpbench": "^0.14",
|
||||
"phpstan/extension-installer": "^1.0",
|
||||
"phpstan/phpstan": "0.12.18",
|
||||
"phpstan/phpstan-phpunit": "0.12.6",
|
||||
"phpstan/phpstan-strict-rules": "0.12.2",
|
||||
"phpunit/phpunit": "^7.2|^8.5",
|
||||
"psr/http-message": "^1.0",
|
||||
"react/promise": "2.*",
|
||||
"simpod/php-coveralls-mirror": "^3.0",
|
||||
"squizlabs/php_codesniffer": "3.5.4"
|
||||
},
|
||||
"suggest": {
|
||||
"psr/http-message": "To use standard GraphQL server",
|
||||
"react/promise": "To leverage async resolving on React PHP platform"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GraphQL\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"description": "A PHP port of GraphQL reference implementation",
|
||||
"homepage": "https://github.com/webonyx/graphql-php",
|
||||
"keywords": [
|
||||
"api",
|
||||
"graphql"
|
||||
],
|
||||
"time": "2020-06-21T12:53:32+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [
|
||||
|
|
Loading…
Reference in a new issue