R::setup('sqlite:'.__DIR__.'/db.sqlite');
// Here we set where our subscriptions are running
GraphQL\subscriptions_at('ws://127.0.0.1:3000');
$roomByName = function ($name) {
return R::findOne('room', 'name = ?', [$name]);
'messages' => function ($room) {
return R::findAll('message', 'room_id = ?', [$room['id']]);
return R::findAll('room');
'messages' => function () use ($roomByName) {
$roomName = $args['roomName'];
$room = $roomByName($roomName);
$messages = R::find('message', 'room_id = ?', [$room['id']]);
'start' => function ($root, $args) {
$roomName = $args['roomName'];
$room = R::dispense('room');
$room['name'] = $roomName;
'chat' => function ($root, $args) use ($roomByName) {
$roomName = $args['roomName'];
$room = $roomByName($roomName);
$message = R::dispense('message');
$message['roomId'] = $room['id'];
$message['body'] = $body;
$message['timestamp'] = new \DateTime();
// Then we can publish new messages that arrives from the chat mutation
GraphQL\publish('inbox', $message); // <- Exactly what "inbox" will receive
// Our added Subscription type
'inbox' => function ($message) { // <- Received from "publish"
'Mutation' => $mutationType,
'Subscription' => $subscriptionType, // Add to the resolver functions array