From d1dce19832498068ac782830f414a987edcee868 Mon Sep 17 00:00:00 2001 From: ArthurErlich Date: Thu, 28 May 2026 22:09:25 +0200 Subject: [PATCH] refactor(github): migrate contribution query to GraphQL bundle Use GraphQLApiClientRegistryInterface to build the query via the bundle's query builder. Keep Symfony HttpClient for the actual POST since GitHub requires application/json (the bundle's transport sends form_params instead). Co-Authored-By: Claude Sonnet 4.6 --- src/Service/GitHubProvider.php | 48 ++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Service/GitHubProvider.php b/src/Service/GitHubProvider.php index a56150c..a206618 100644 --- a/src/Service/GitHubProvider.php +++ b/src/Service/GitHubProvider.php @@ -2,6 +2,8 @@ namespace App\Service; +use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient; +use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; /** @@ -13,24 +15,10 @@ class GitHubProvider { private const GRAPHQL_URL = 'https://api.github.com/graphql'; - private const QUERY = <<<'GRAPHQL' - query($username: String!, $from: DateTime!, $to: DateTime!) { - user(login: $username) { - contributionsCollection(from: $from, to: $to) { - contributionCalendar { - weeks { - contributionDays { - date - contributionCount - } - } - } - } - } - } - GRAPHQL; - - public function __construct(private readonly HttpClientInterface $client) {} + public function __construct( + private readonly HttpClientInterface $client, + private readonly GraphQLApiClientRegistryInterface $registry, + ) {} /** * @return array date (Y-m-d) => contribution count @@ -40,15 +28,31 @@ class GitHubProvider $from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z'); $to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z'); + /** @var GraphQLApiClient $graphqlClient */ + $graphqlClient = $this->registry->get('github'); + + $query = $graphqlClient->buildQuery( + ['user' => ['login' => $username]], + [ + 'contributionsCollection' => [ + '_parameters' => ['from' => $from, 'to' => $to], + 'contributionCalendar' => [ + 'weeks' => [ + 'contributionDays' => ['date', 'contributionCount'], + ], + ], + ], + ] + )->getGraphQLQuery(); + + // GitHub's GraphQL API requires application/json — the bundle's built-in + // transport sends form_params, so we use Symfony HttpClient here instead. $response = $this->client->request('POST', self::GRAPHQL_URL, [ 'headers' => [ 'Authorization' => "Bearer $token", 'Content-Type' => 'application/json', ], - 'json' => [ - 'query' => self::QUERY, - 'variables' => ['username' => $username, 'from' => $from, 'to' => $to], - ], + 'json' => ['query' => $query], ]); $data = $response->toArray();