Search by

coac-factory / api-client-php

josep.pujol

API COAC GraphQL client

Package info

bitbucket.org/coac-factory/coac-api-client-php

pkg:composer/coac-factory/api-client-php

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

v1.2.0 2026-10-06 14:09 UTC

This package is auto-updated.

Last update: 2026-10-06 14:13:06 UTC


README

Framework-agnostic GraphQL client for PHP 8+ with JWT authentication (username/password -> Bearer token).

Features

  • Composer installable library
  • JWT authentication with persistent token caching (reused until it expires)
  • Retry on 401 (token refresh)
  • Framework-agnostic (Symfony, WordPress, CLI, etc.)

Installation

composer require coac-factory/api-client-php

Usage

use CoacFactory\GraphQLClient\Config\ClientConfig;
use CoacFactory\GraphQLClient\Auth\JwtTokenProvider;
use CoacFactory\GraphQLClient\Http\GraphQLClient;

$config = new ClientConfig(
    'https://api.example.com/graphql',
    'user',
    'password',
);
$client = new GraphQLClient($config);

$result = $client->query('
    query {
        items { id name }
    }
');

Endpoints

By default the client authenticates at {apiUrl}/token and sends the queries to {apiUrl}/api. Both paths can be changed, for instance to query the master schema:

$config = new ClientConfig(
    'https://api.example.com',
    'user',
    'password',
    queryEndpoint: 'api/master',   // default: 'api'
    authEndpoint: 'token',         // default: 'token'
);

Token caching

The JWT token is stored and reused across requests until it expires. The expiration is read from the token's exp claim (falling back to a 3600s TTL if missing), and the token is renewed 60 seconds before it expires or when the API answers with a 401.

By default the token is persisted on disk in sys_get_temp_dir()/coac-api-client (files with 0600 permissions). A different directory or storage can be provided by implementing TokenStorageInterface (e.g. to use WordPress transients, Redis or a Symfony cache):

use CoacFactory\GraphQLClient\Auth\JwtTokenProvider;
use CoacFactory\GraphQLClient\Storage\FileTokenStorage;
use CoacFactory\GraphQLClient\Storage\InMemoryTokenStorage;

// Custom directory
$tokenProvider = new JwtTokenProvider($config, storage: new FileTokenStorage('/var/cache/my-app'));

// Only for the current process (no persistence)
$tokenProvider = new JwtTokenProvider($config, storage: new InMemoryTokenStorage());

$client = new GraphQLClient($config, $tokenProvider);