Skip to content
Alexandr Viniychuk edited this page Jul 29, 2016 · 1 revision

Query of list of scalar values

<?php
namespace Sandbox;

use Youshido\GraphQL\Execution\Processor;
use Youshido\GraphQL\Schema\Schema;
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
use Youshido\GraphQL\Type\ListType\ListType;
use Youshido\GraphQL\Type\Object\ObjectType;
use Youshido\GraphQL\Type\Scalar\StringType;

require_once 'vendor/autoload.php';

$processor = new Processor(new Schema([
    'query'    => new ObjectType([
        'name'   => 'RootQueryType',
        'fields' => [
            'labels' => [
                'type'    => new ListType(new StringType()),
                'resolve' => function () {
                    return ['one', 'two'];
                }
            ]
        ]
    ])
]));


$payload = '{ labels }';
$processor->processPayload($payload);
echo json_encode($processor->getResponseData()) . "\n";

Using InputObject as both Input and Output type

<?php
namespace Sandbox;

use Youshido\GraphQL\Execution\Processor;
use Youshido\GraphQL\Schema\Schema;
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
use Youshido\GraphQL\Type\ListType\ListType;
use Youshido\GraphQL\Type\Object\ObjectType;
use Youshido\GraphQL\Type\Scalar\StringType;

require_once 'vendor/autoload.php';

class UserProperties extends AbstractInputObjectType
{

    public function build($config)
    {
        $config->addFields([
            'name'  => [
                'type' => new StringType(),
            ],
            'email' => [
                'type' => new StringType(),
            ]
        ]);
    }
}

$processor = new Processor(new Schema([
    'query'    => new ObjectType([
        'name'   => 'RootQueryType',
        'fields' => [
            'me' => [
                'type' => new UserProperties(),
                'resolve' => function() {
                    return [
                        'name' => 'Alex',
                        'email' => 'alex@gmail.com'
                    ];
                }
            ]
        ]
    ]),
    'mutation' => new ObjectType([
        'name'   => 'RootMutation',
        'fields' => [
            'createUser' => [
                'type' => new UserProperties(),
                'args' => [
                    'user' => new UserProperties()
                ],
                'resolve' => function($value, $args, $info) {
                    return $args['user'];
                }
            ]
        ]
    ])
]));


//$payload = '{ me {name, email} }';
$payload = 'mutation { createUser(user: { name: "Robot", email: "robot@gmail.com"} ) {name, email} }';
$processor->processPayload($payload);
echo json_encode($processor->getResponseData()) . "\n";