PHP code example of ciloe / graphql-client-php

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

    

ciloe / graphql-client-php example snippets




l = new \GraphQLClientPhp\Model\ApiModel('https://api.github.com', '/graphql', 'MyToken');



// Some code

$bridge = new \GraphQLClientPhp\Bridge\BasicBridge($model);
$client = new \GraphQLClientPhp\Client\BasicClient(
    $bridge, 
    new GraphQLClientPhp\Parser\QueryBasicParser()
);



nt = \GraphQLClientPhp\Client\BasicClient::factory(
    'https://api.github.com',
    'graphql',
    'MyToken'
);



// Some code

$results = $client->query('query test {user {name}}');



// Some code

$bridge = new \GraphQLClientPhp\Bridge\BasicBridge($model);
$client = new \GraphQLClientPhp\Client\BasicClient(
    $bridge, 
    new GraphQLClientPhp\Parser\QueryBasicParser()
);

$results = $client
    ->addVariable('variable', true)
    ->query('query test ($variable: Boolean!) {user @



// Some code

$pool = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$fileCache = $PATH_TO_CACHE_DIR . 'cache.php';
$queries = $PATH_TO_GRAPHQL_QUERIES . 'queries';
$fragments = $PATH_TO_GRAPHQL_FRAGMENTS . 'fragments';
$adapter = new \Symfony\Component\Cache\Adapter\PhpArrayAdapter($fileCache, $pool);

$service = new \GraphQLClientPhp\Cache\BasicCache(
    $adapter,
    new \GraphQLClientPhp\Parser\QueryBasicParser(),
    ['queries' => $queries, 'fragments' => $fragments]
);
$service->warmUp();



// Some code

$DS = DIRECTORY_SEPARATOR;
$fileCache = __DIR__ . $DS . 'Resources' . $DS . 'cache' . $DS . 'cache.php';
$queries = __DIR__ . $DS . 'Resources' . $DS . 'graph' . $DS . 'queries';
$fragments = __DIR__ . $DS . 'Resources' . $DS . 'graph' . $DS . 'fragments';
$queryParser = new \GraphQLClientPhp\Parser\QueryBasicQueryParser();

$cache = \GraphQLClientPhp\Cache\BasicCache::factory(
    $fileCache,
    $queries,
    $fragments,
    $queryParser
);



// Some code

$query = $adapter->getItem('myQueryFile');
$fragments = $adapter->getItem(\GraphQLClientPhp\Cache\CacheInterface::CACHED_FRAGMENT_KEY);
$client = new \GraphQLClientPhp\Client\BasicClient(
    $bridge, 
    new GraphQLClientPhp\Parser\QueryBasicParser(),
    $fragments
);

$result = $client->query($query);



// Some code

// In your PHP execution, you can stored more than one query.
$client
->setName('myFirstQuery')
->setVariables(['number' => 5])
->addQuery(
    'query ($number:Int!) {
      viewer {
        name
         repositories(last: $number) {
           nodes {
             name
           }
         }
       }
    }'
);

$client
->setName('mySecondQuery')
->addQuery(
    'query {
      viewer {
        name
       }
    }'
);

$results = $client->sendQueries(true); // Use parameter $async to use or not promises.