Skip to content

Instantly share code, notes, and snippets.

@chillu
Created February 18, 2019 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chillu/0afd4e22e3c8b58f1d848288fa07892a to your computer and use it in GitHub Desktop.
Save chillu/0afd4e22e3c8b58f1d848288fa07892a to your computer and use it in GitHub Desktop.
graphql-interface-types-poc
<?php
// See http://webonyx.github.io/graphql-php/getting-started/ on how to run this
require_once('vendor/autoload.php');
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Schema;
class PageType extends ObjectType {
public function __construct()
{
$config = [
'name' => 'Page',
'fields' => [
'ID' => Type::id(),
'Content' => Type::string()
],
'interfaces' => [
MyTypes::pageInterface()
]
];
parent::__construct($config);
}
}
class MyPageType extends ObjectType {
public function __construct()
{
$config = [
'name' => 'MyPage',
'fields' => [
'ID' => Type::id(),
'Content' => Type::string(),
'MyField' => Type::string()
],
'interfaces' => [
MyTypes::pageInterface()
]
];
parent::__construct($config);
}
}
class PageInterfaceType extends InterfaceType
{
public function __construct()
{
$config = [
'name' => 'PageInterface',
'fields' => [
'ID' => Type::id(),
'Content' => Type::string()
],
'resolveType' => function($val) {
return MyTypes::myPage();
}
];
return parent::__construct($config);
}
}
class PageUnionType extends UnionType
{
public function __construct()
{
$config = [
'name' => 'PageUnion',
'types' => [
MyTypes::page(),
MyTypes::myPage()
],
'resolveType' => function($val) {
return MyTypes::myPage();
}
];
return parent::__construct($config);
}
}
class MyTypes
{
private static $page;
private static $myPage;
private static $pageInterface;
private static $pageUnion;
public static function page()
{
return self::$page ?: (self::$page = new PageType());
}
public static function myPage()
{
return self::$myPage ?: (self::$myPage = new MyPageType());
}
public static function pageInterface()
{
return self::$pageInterface ?: (self::$pageInterface = new PageInterfaceType());
}
public static function pageUnion()
{
return self::$pageUnion ?: (self::$pageUnion = new PageUnionType());
}
}
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'readPages' => [
'type' => MyTypes::pageUnion(),
'resolve' => function() {
return [
'ID' => 99,
'Content' => 'test',
'MyField' => 'my test field'
];
}
],
'echo' => [
'type' => Type::string(),
'args' => [
'message' => Type::nonNull(Type::string()),
],
'resolve' => function ($root, $args) {
return $root['prefix'] . $args['message'];
}
],
],
]);
$schema = new Schema([
'query' => $queryType
]);
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
try {
$rootValue = ['prefix' => 'You said: '];
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
$output = $result->toArray();
} catch (\Exception $e) {
$output = [
'errors' => [
[
'message' => $e->getMessage()
]
]
];
}
header('Content-Type: application/json');
echo json_encode($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment