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 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
|
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient;
|
||||||
|
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,24 +15,10 @@ class GitHubProvider
|
|||||||
{
|
{
|
||||||
private const GRAPHQL_URL = 'https://api.github.com/graphql';
|
private const GRAPHQL_URL = 'https://api.github.com/graphql';
|
||||||
|
|
||||||
private const QUERY = <<<'GRAPHQL'
|
public function __construct(
|
||||||
query($username: String!, $from: DateTime!, $to: DateTime!) {
|
private readonly HttpClientInterface $client,
|
||||||
user(login: $username) {
|
private readonly GraphQLApiClientRegistryInterface $registry,
|
||||||
contributionsCollection(from: $from, to: $to) {
|
) {}
|
||||||
contributionCalendar {
|
|
||||||
weeks {
|
|
||||||
contributionDays {
|
|
||||||
date
|
|
||||||
contributionCount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GRAPHQL;
|
|
||||||
|
|
||||||
public function __construct(private readonly HttpClientInterface $client) {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<string, int> date (Y-m-d) => contribution count
|
* @return array<string, int> 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');
|
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||||
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\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, [
|
$response = $this->client->request('POST', self::GRAPHQL_URL, [
|
||||||
'headers' => [
|
'headers' => [
|
||||||
'Authorization' => "Bearer $token",
|
'Authorization' => "Bearer $token",
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
],
|
],
|
||||||
'json' => [
|
'json' => ['query' => $query],
|
||||||
'query' => self::QUERY,
|
|
||||||
'variables' => ['username' => $username, 'from' => $from, 'to' => $to],
|
|
||||||
],
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$data = $response->toArray();
|
$data = $response->toArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user