Skip to content

Commit

Permalink
bug #1048 Fix Client URL Prepending For GraphQL Endpoint on Enterpris…
Browse files Browse the repository at this point in the history
…e (asher-goldberg, acrobat)

This PR was squashed before being merged into the 3.4.x-dev branch.

Discussion
----------

fixes #1047 

This checks if `v4` is passed into the Client and an Enterprise URL is set then the prepend will only be `/api`.  (The class default is `v3` if no version is provided so can't just pass an empty string).

TL;DR - this now allows this package to run GraphQL calls on Enterprise `$client = new Client(null, 'v4', 'https://enterpriseurl');`

Commits
-------

0d989bb Fix prepend on Enteprrise for V4
a44fa2b Allow codestyle fix
  • Loading branch information
asher-goldberg committed Jan 23, 2022
1 parent a46dc82 commit b56a3fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,14 @@ private function setEnterpriseUrl($enterpriseUrl): void
$builder->removePlugin(PathPrepend::class);

$builder->addPlugin(new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($enterpriseUrl)));
$builder->addPlugin(new PathPrepend(sprintf('/api/%s', $this->getApiVersion())));

// For GHE, v4 API endpoint is at `api/graphql` so we don't want to add the version number
// For earlier versions add the version number after /api
if ($this->getApiVersion() === 'v4') {
$builder->addPlugin(new PathPrepend('/api'));
} else {
$builder->addPlugin(new PathPrepend(sprintf('/api/%s', $this->getApiVersion())));
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/Github/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,25 @@ public function testEnterpriseUrl()
$client = new Client($httpClientBuilder, null, 'https://foobar.com');
$client->enterprise()->stats()->show('all');
}

/**
* Make sure that the prepend is correct when using the v4 endpoint on Enterprise.
*/
public function testEnterprisePrependGraphQLV4()
{
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
->setMethods(['sendRequest'])
->getMock();

$httpClientMock->expects($this->once())
->method('sendRequest')
->with($this->callback(function (RequestInterface $request) {
return (string) $request->getUri() === 'https://foobar.com/api/graphql';
}))
->willReturn(new Response(200, [], '[]'));

$httpClientBuilder = new Builder($httpClientMock);
$client = new Client($httpClientBuilder, 'v4', 'https://foobar.com');
$client->graphql()->execute('query');
}
}

0 comments on commit b56a3fe

Please sign in to comment.