GraphQL\Examples\Blog\Data\DataSource::init PHP Method

init() public static method

public static init ( )
    public static function init()
    {
        self::$users = ['1' => new User(['id' => '1', 'email' => '[email protected]', 'firstName' => 'John', 'lastName' => 'Doe']), '2' => new User(['id' => '2', 'email' => '[email protected]', 'firstName' => 'Jane', 'lastName' => 'Doe']), '3' => new User(['id' => '3', 'email' => '[email protected]', 'firstName' => 'John', 'lastName' => 'Doe'])];
        self::$stories = ['1' => new Story(['id' => '1', 'authorId' => '1', 'body' => '<h1>GraphQL is awesome!</h1>']), '2' => new Story(['id' => '2', 'authorId' => '1', 'body' => '<a>Test this</a>']), '3' => new Story(['id' => '3', 'authorId' => '3', 'body' => "This\n<br>story\n<br>spans\n<br>newlines"])];
        self::$storyLikes = ['1' => ['1', '2', '3'], '2' => [], '3' => ['1']];
        self::$comments = ['100' => new Comment(['id' => '100', 'authorId' => '3', 'storyId' => '1', 'body' => 'Likes']), '110' => new Comment(['id' => '110', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply <b>#1</b>', 'parentId' => '100']), '111' => new Comment(['id' => '111', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-1', 'parentId' => '110']), '112' => new Comment(['id' => '112', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-2', 'parentId' => '110']), '113' => new Comment(['id' => '113', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-3', 'parentId' => '110']), '114' => new Comment(['id' => '114', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-4', 'parentId' => '110']), '115' => new Comment(['id' => '115', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-5', 'parentId' => '110']), '116' => new Comment(['id' => '116', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-6', 'parentId' => '110']), '117' => new Comment(['id' => '117', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-7', 'parentId' => '110']), '120' => new Comment(['id' => '120', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #2', 'parentId' => '100']), '130' => new Comment(['id' => '130', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #3', 'parentId' => '100']), '200' => new Comment(['id' => '200', 'authorId' => '2', 'storyId' => '1', 'body' => 'Me2']), '300' => new Comment(['id' => '300', 'authorId' => '3', 'storyId' => '1', 'body' => 'U2']), '400' => new Comment(['id' => '400', 'authorId' => '2', 'storyId' => '2', 'body' => 'Me too']), '500' => new Comment(['id' => '500', 'authorId' => '2', 'storyId' => '2', 'body' => 'Nice!'])];
        self::$storyComments = ['1' => ['100', '200', '300'], '2' => ['400', '500']];
        self::$commentReplies = ['100' => ['110', '120', '130'], '110' => ['111', '112', '113', '114', '115', '116', '117']];
        self::$storyMentions = ['1' => [self::$users['2']], '2' => [self::$stories['1'], self::$users['3']]];
    }

Usage Example

Ejemplo n.º 1
0
use GraphQL\Error\FormattedError;
// Disable default PHP error reporting - we have better one for debug mode (see bellow)
ini_set('display_errors', 0);
if (!empty($_GET['debug'])) {
    // Enable additional validation of type configs
    // (disabled by default because it is costly)
    Config::enableValidation();
    // Catch custom errors (to report them in query results if debugging is enabled)
    $phpErrors = [];
    set_error_handler(function ($severity, $message, $file, $line) use(&$phpErrors) {
        $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
    });
}
try {
    // Initialize our fake data source
    DataSource::init();
    // Prepare context that will be available in all field resolvers (as 3rd argument):
    $appContext = new AppContext();
    $appContext->viewer = DataSource::findUser('1');
    // simulated "currently logged-in user"
    $appContext->rootUrl = 'http://localhost:8080';
    $appContext->request = $_REQUEST;
    // Parse incoming query and variables
    if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
        $raw = file_get_contents('php://input') ?: '';
        $data = json_decode($raw, true);
    } else {
        $data = $_REQUEST;
    }
    $data += ['query' => null, 'variables' => null];
    if (null === $data['query']) {