Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Graphql Client Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require ciloe/graphql-client-php
    

    Add to composer.json if using a custom repository or fork.

  2. 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);
    
  3. Key Files

    • src/Client.php: Core client logic.
    • src/Query.php: Query builder and execution.
    • src/Exception/GraphQLException.php: Error handling.

First Use Case: Fetching Data

// Fetch a single user
$user = $client->query($query)->getData()['user'];

Implementation Patterns

Workflows

  1. Query Execution

    $client = new Client('https://api.example.com/graphql');
    $query = new Query('{ user(id: 1) { name } }');
    $response = $client->query($query);
    
  2. Mutations

    $mutation = new Query('
        mutation CreateUser($name: String!) {
            createUser(name: $name) { id }
        }
    ');
    $mutation->setVariables(['name' => 'John Doe']);
    $client->query($mutation);
    
  3. Pagination

    $query = new Query('
        query PaginatedUsers($first: Int!) {
            users(first: $first) { edges { node { id } } }
        }
    ');
    $query->setVariables(['first' => 10]);
    

Integration Tips

  • Authentication: Pass headers via Client constructor:
    $client = new Client('https://api.example.com/graphql', [
        'Authorization' => 'Bearer token123',
    ]);
    
  • Error Handling: Use try-catch with GraphQLException:
    try {
        $client->query($query);
    } catch (GraphQLException $e) {
        Log::error($e->getMessage());
    }
    
  • Caching: Cache responses with Laravel’s cache system:
    $cacheKey = 'graphql_user_1';
    $user = Cache::remember($cacheKey, 3600, function () use ($client, $query) {
        return $client->query($query)->getData()['user'];
    });
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package: Last release in 2018; verify compatibility with your PHP/Laravel version.
  2. No Built-in Retry Logic: Implement retry for transient failures (e.g., using retry package).
  3. Limited Error Details: GraphQLException may not expose raw GraphQL errors; inspect $response->getErrors().
  4. No Type Safety: Variables are passed as raw arrays; validate inputs manually.

Debugging

  • Enable Debug Mode: Pass ['debug' => true] to Client constructor for verbose logs.
  • Inspect Raw Response:
    $response = $client->query($query);
    dd($response->getRawResponse());
    

Extension Points

  1. Custom Transport: Extend Ciloe\GraphQLClient\Transport\TransportInterface for HTTP clients (e.g., Guzzle middleware).
  2. Query Builder: Override Query methods to add auto-variables or logging.
  3. Response Parsing: Extend Ciloe\GraphQLClient\Response to handle custom response formats.

Configuration Quirks

  • Default Headers: Set globally via Client constructor; no per-query header support.
  • Timeouts: Configure via transport layer (e.g., Guzzle’s timeout option).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views