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 Bundle Laravel Package

ciloe/graphql-client-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle via Composer:

    composer require ciloe/graphql-client-bundle
    

    Enable the bundle in config/bundles.php:

    return [
        // ...
        Ciloe\GraphQLClientBundle\CiloeGraphQLClientBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration Configure the client in config/packages/ciloe_graphql_client.yaml:

    ciloe_graphql_client:
        endpoint: 'https://your-graphql-endpoint.com/graphql'
        headers:
            Authorization: 'Bearer YOUR_TOKEN'
    
  3. First Query Inject the client service and execute a query:

    use Ciloe\GraphQLClientBundle\Service\GraphQLClient;
    
    class MyController extends AbstractController
    {
        public function __construct(private GraphQLClient $client)
        {
        }
    
        public function queryExample()
        {
            $query = '
                query {
                    user(id: 1) {
                        name
                        email
                    }
                }
            ';
            $result = $this->client->query($query);
            return $this->json($result);
        }
    }
    

Where to Look First


Implementation Patterns

Query Execution Workflows

  1. Direct Query Execution Use the query() method for one-off requests:

    $result = $client->query($queryString);
    
  2. Named Queries (Recommended for Reusability) Define queries in YAML/JSON files (e.g., config/graphql/queries/user.yaml):

    user:
        query: 'query { user(id: $id) { name } }'
        variables: { id: 1 }
    

    Load and execute via the service:

    $result = $client->queryFromFile('user', ['id' => 1]);
    
  3. Variable Binding Pass variables dynamically:

    $query = 'query GetUser($id: ID!) { user(id: $id) { name } }';
    $result = $client->query($query, ['id' => 2]);
    
  4. Mutation Support Use the mutate() method for mutations:

    $mutation = '
        mutation CreateUser($input: UserInput!) {
            createUser(input: $input) { id }
        }
    ';
    $result = $client->mutate($mutation, ['input' => ['name' => 'John']]);
    

Integration with Symfony Ecosystem

  1. Dependency Injection Inject GraphQLClient into controllers, services, or commands:

    public function __construct(private GraphQLClient $client) {}
    
  2. Event Listeners/Subscribers Extend functionality by listening to graphql.client.query events:

    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Ciloe\GraphQLClientBundle\Event\QueryEvent;
    
    class GraphQLLoggerSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [QueryEvent::NAME => 'onQuery'];
        }
    
        public function onQuery(QueryEvent $event)
        {
            // Log or modify the query/variables
        }
    }
    
  3. Caching Strategies Leverage the built-in cache service:

    # config/packages/ciloe_graphql_client.yaml
    ciloe_graphql_client:
        cache:
            enabled: true
            provider: 'cache.app' # Symfony cache service ID
    

    Cache queries automatically by normalizing variables:

    $client->query($query, ['id' => 1]); // Cached with key: "query_hash:id:1"
    
  4. Error Handling Wrap queries in try-catch blocks:

    try {
        $result = $client->query($query);
    } catch (GraphQLClientException $e) {
        // Handle errors (e.g., network issues, GraphQL errors)
        $this->addFlash('error', $e->getMessage());
    }
    

Gotchas and Tips

Common Pitfalls

  1. Deprecated Package

    • Last updated in 2018—expect missing features (e.g., no support for GraphQL subscriptions, limited error handling).
    • Mitigation: Extend the bundle or use a modern alternative like webonyx/graphql-php.
  2. Cache Key Collisions

    • The cache normalizes variables, but complex objects (e.g., DateTime) may cause issues.
    • Fix: Implement a custom cache key generator in the service:
      $client->setCacheKeyGenerator(function ($query, $variables) {
          return md5($query . json_encode($variables, JSON_UNESCAPED_UNICODE));
      });
      
  3. Header Injection Risks

    • Hardcoded headers in config may expose tokens.
    • Tip: Use Symfony’s %env% or parameter bags:
      headers:
          Authorization: '%env(API_TOKEN)%'
      
  4. No Built-in Retry Logic

    • Network failures will throw exceptions immediately.
    • Workaround: Wrap calls in a retry service:
      use Symfony\Component\Retry\Retry;
      
      $retry = new Retry(3, 100); // 3 retries, 100ms delay
      $result = $retry->retry(function () use ($client, $query) {
          return $client->query($query);
      });
      

Debugging Tips

  1. Enable Verbose Logging Add to config/packages/monolog.yaml:

    handlers:
        graphql:
            type: stream
            path: '%kernel.logs_dir%/graphql.log'
            level: debug
            channels: ['graphql']
    

    Then enable the channel in the bundle config:

    ciloe_graphql_client:
        logging:
            enabled: true
            channel: 'graphql'
    
  2. Inspect Raw Responses Use the rawQuery() method to bypass processing:

    $response = $client->rawQuery($query);
    // Dump the full HTTP response
    file_put_contents('debug.graphql', print_r($response, true));
    
  3. Variable Validation The bundle doesn’t validate variables against the schema. Use a tool like GraphQL Playground or Altair to test queries first.

Extension Points

  1. Custom HTTP Client Override the default GuzzleHttp\Client by binding a new service:

    # config/services.yaml
    services:
        Ciloe\GraphQLClientBundle\Service\GraphQLClient:
            arguments:
                $httpClient: '@your_custom.http_client'
    
  2. Query Preprocessing Add middleware to modify queries/variables before execution:

    $client->addMiddleware(function ($query, $variables) {
        // Add auth headers dynamically
        $variables['authToken'] = $this->getAuthToken();
        return [$query, $variables];
    });
    
  3. Response Transformation Extend the response parser to normalize data:

    $client->setResponseParser(function ($response) {
        $data = json_decode($response->getBody(), true);
        return $this->transformResponse($data);
    });
    
  4. Schema Introspection The bundle lacks introspection tools. Use a separate library like graphql-php for schema-aware development.

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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
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