PHP code example of mathsgod / graphql-php-loader

1. Go to this page and download the library: Download mathsgod/graphql-php-loader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mathsgod / graphql-php-loader example snippets



use GraphQL\Loader;
use GraphQL\Type\Schema;

$loader=new Loader();

$config = SchemaConfig::create()
    ->setQuery($loader->queryType())
    ->setMutation($loader->mutationType());

$schema = new Schema($config);


return [
    "type"=>"String",
    "resolve"=>function($root,$args,$context){
        return "world!";
    }
];

return [
    "type"=>"User",
    "resolve"=>function($root,$args,$context){
        return $context->me; //return object
    }
];

return [
    "fields"=>[
        "first_name"=>"String",
        "last_name"=>"String"
    ]
];

return [
    "fields"=>[
        "first_name"=>"String",
        "last_name"=>"String"
    ]
];
/* 
no need create phone in fields,
by create file phone.php in User folder, it auto generate fields in User type
*/

return [
    "type"=>"[String]",
    "resolve"=>function($user,$args,$context){
        return $user->getPhones(); //return multi phone
    }
];

graphql
+-- Query
|  +-- hello.php

graphql
+-- Query
|  +-- me.php
+-- User.php

graphql
+-- Query
|  +-- me.php
+-- User
|  +-- phone.php
+-- User.php

graphql
+-- Query
|  +-- User
|      +-- list.php
|  +-- Invoice
|      +-- list.php
+-- User.php
+-- Invoice.php