<?php
function objectType($name, ...$fields) {
if (empty($fields)) {
throw new \RuntimeException();
}
if (!is_string(key($fields[0]))) {
$interfaces = array_shift($fields);
} else {
$interfaces = [];
}
if (empty($fields)) {
throw new \RuntimeException();
}
$def = [
'name' => $name,
'interfaces' => $interfaces,
'fields' => [],
];
foreach ($fields as &$field) {
$def['fields'][$field['name']] = $field;
}
return $def;
}
function field($name, $type, ...$args) {
$def = [
'name' => $name,
'type' => $type,
'args' => [],
'resolve' => null,
];
if (!empty($args) && is_callable(end($args))) {
$def['resolve'] = array_pop($args);
}
foreach ($args as &$arg) {
$def['args'][$arg['name']] = $arg;
}
return $def;
}
function argument($name, $type, $value = null) {
$def = [
'name' => $name,
'type' => $type,
'defaultValue' => $value,
];
return $def;
}
$IntType = ['name' => 'Int'];
$StringType = ['name' => 'String'];
$BlogType = objectType('Blog',
field('id', $IntType),
field('name', $StringType));
$UserType = objectType('Blog',
field('id', $IntType),
field('email', $StringType));
$QueryType = objectType('Query',
field('blog', $BlogType, argument('id', $IntType)),
field('user', $UserType, argument('id', $IntType)));
var_dump($QueryType);
preferences:
30.32 ms | 402 KiB | 5 Q