ciloe/graphql-client-php
Lightweight PHP GraphQL client with a basic bridge/client setup or quick factory creation. Supports simple queries, named queries with variables, and optional caching via Symfony Cache adapters. Configure host/URI/token and send requests easily.
Installation
composer require ciloe/graphql-client-php
Add to composer.json if using a custom repository or fork.
First Query
use Ciloe\GraphQLClient\Client;
use Ciloe\GraphQLClient\Query;
$client = new Client('https://your-graphql-endpoint.com/graphql');
$query = new Query('
query GetUser($id: ID!) {
user(id: $id) {
id
name
}
}
');
$query->setVariables(['id' => '1']);
$result = $client->query($query);
Key Files
src/Client.php: Core client logic.src/Query.php: Query builder and execution.src/Exception/GraphQLException.php: Error handling.// Fetch a single user
$user = $client->query($query)->getData()['user'];
Query Execution
$client = new Client('https://api.example.com/graphql');
$query = new Query('{ user(id: 1) { name } }');
$response = $client->query($query);
Mutations
$mutation = new Query('
mutation CreateUser($name: String!) {
createUser(name: $name) { id }
}
');
$mutation->setVariables(['name' => 'John Doe']);
$client->query($mutation);
Pagination
$query = new Query('
query PaginatedUsers($first: Int!) {
users(first: $first) { edges { node { id } } }
}
');
$query->setVariables(['first' => 10]);
Client constructor:
$client = new Client('https://api.example.com/graphql', [
'Authorization' => 'Bearer token123',
]);
try-catch with GraphQLException:
try {
$client->query($query);
} catch (GraphQLException $e) {
Log::error($e->getMessage());
}
$cacheKey = 'graphql_user_1';
$user = Cache::remember($cacheKey, 3600, function () use ($client, $query) {
return $client->query($query)->getData()['user'];
});
retry package).GraphQLException may not expose raw GraphQL errors; inspect $response->getErrors().['debug' => true] to Client constructor for verbose logs.$response = $client->query($query);
dd($response->getRawResponse());
Ciloe\GraphQLClient\Transport\TransportInterface for HTTP clients (e.g., Guzzle middleware).Query methods to add auto-variables or logging.Ciloe\GraphQLClient\Response to handle custom response formats.Client constructor; no per-query header support.timeout option).How can I help you explore Laravel packages today?