PHP code example of fo3nix / php-graphql-oqm

1. Go to this page and download the library: Download fo3nix/php-graphql-oqm 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/ */

    

fo3nix / php-graphql-oqm example snippets


$rootObject = new RootQueryObject();
$rootObject
    ->selectPokemons((new RootPokemonsArgumentsObject())->setFirst(5))
        ->selectName()
        ->selectId()
        ->selectFleeRate()
        ->selectAttacks()
            ->selectFast()
                ->selectName();

$results = $client->runQuery($rootObject->getQuery());

// Step 1: Build the query (same as before)
$queryRoot = new RootQueryObject();
$pokemonQuery = $queryRoot
    ->selectPokemons((new RootPokemonsArgumentsObject())->setFirst(5))
    ->selectName()
    ->selectId()
    ->selectFleeRate();

// Step 2: Run the query and get results as array
$results = $client->runQuery($queryRoot->getQuery(), true); // true = get as array
$pokemonsData = $results->getData()['pokemons'];

// Step 3: Hydrate the raw array into Result Objects
$pokemons = array_map(function ($pokemonData) {
    return Pokemon::fromArray($pokemonData);
}, $pokemonsData);

// Step 4: Use the type-hinted objects!
foreach ($pokemons as $pokemon) {
    echo "Name: " . $pokemon->getName() . "\n";
    echo "ID: " . $pokemon->getId() . "\n";
    echo "Flee Rate: " . $pokemon->getFleeRate() . "\n";
    
    // Convert back to array if needed
    $pokemonArray = $pokemon->asArray();
}

// You can also hydrate from JSON
$pokemon = Pokemon::fromJson('{"name":"Pikachu","id":"025","fleeRate":0.1}');

$rootObject = (new RootQueryObject())->selectAttacks()->selectSpecial()->selectName();

$rootObjet = new RootQueryObject();
$rootObject->selectAttacks()->selectSpecial()->selectName();

$rootObject = new RootQueryObject();
$rootObject->selectPokemon(
    (new RootPokemonArgumentsObject())->setName('charmander')
)
    ->selectEvolutions()
        ->selectName()
        ->selectNumber()
        ->selectEvolutionRequirements()
            ->selectName()
            ->selectAmount()
    ->selectEvolutionRequirements()
        ->selectName()
        ->selectAmount();

$rootObject = new RootQueryObject();
$charmander = $rootObject->selectPokemon(
    (new RootPokemonArgumentsObject())->setName('charmander')
);
$charmander->selectEvolutions()
    ->selectName()
    ->selectNumber()
    ->selectEvolutionRequirements()
        ->selectName()
        ->selectAmount();
$charmander->selectEvolutionRequirements()
    ->selectName()
    ->selectAmount();

composer 

php vendor/bin/generate_schema_objects