biohzrdmx/liteql-php

Barebones GraphQL client library

1.0.0 2025-04-30 03:23 UTC

This package is auto-updated.

Last update: 2025-04-30 03:28:12 UTC


README

Barebones GraphQL client library.

With LiteQL you will be able to run GraphQL queries easily, without requiring to create complicate schemas or generating code. You just run a query and get the result or receive an exception.

Also, you get to choose the PSR-17/18 implementation used, so you get a lightweight and flexible alternative.

Basic usage

First require biohzrdmx/liteql-php with Composer.

Next you need to create a LiteQL\Client instance, to do so you must pass two parameters to the constructor:

  • $http_client - An instance of an implementation of ClientInterface (PSR-18)
  • $request_factory - An instance of an implementation of RequestFactoryInterface (PSR-17)

You are free to choose which implementation to use, just pass both instances to the create method of LiteQL\Client for easy chaining.

Then use the other methods such as setUri, setHeader to configure the client, leveraging the fluent interface.

Now it's time to build the Query itself, to do so you can create it from a string (with the fromString method) or from a .graphql file (using the fromFile method).

Please do note that at this point the fluent interface returns a Query instance, so you may add variables with the setVariable method if you want.

Finally, call the execute method and you should get a Result object.

The full flow looks like the following example:

use AwesomeSauce\HttpCLient;
use AwesomeSauce\RequestFactory;

use LiteQL\Client; 

$http_client = new HttpClient();
$request_factory = new RequestFactory();
$result = Client::create($http_client, $request_factory)
    ->setUri('https://dummy-pokemon-graphql.local')
    ->fromString('query GetPokemon($name: String) {pokemon(name: $name) {id number name } }')
    ->setVariable('name', 'Pikachu')
    ->execute();

To load the query from a file:

$result = Client::create($http_client, $request_factory)
    ->setUri('https://dummy-pokemon-graphql.local')
    ->fromFile('/path/to/queries/directory/getPokemon.graphql')
    ->setVariable('name', 'Pikachu')
    ->execute();

Either way you will get a Result object or a ClientException will be thrown (if for example you need authentication, or you hit a request limit, etc.).

Result object

The Result object has the following methods:

  • getResponse - Get the raw ResponseInterface implementation
  • getStatusCode - Get the status code (anything <200 or >299 will generate an exception)
  • getData - Get the parsed data from the response (if the Content-Type header is application/json, otherwise an exception will be thrown)

Now you can do whatever you want with the returned data, cache it, parse it, display it, etc.

That's it, you need no introspection or schemas or anything else to query a GraphQL server, just prepare your queries and pass some variables.

Licensing

This software is released under the MIT license.

Credits

Lead coder: biohzrdmx <github.com/biohzrdmx>