Skip to content

Commit b56a3fe

Browse files
bug #1048 Fix Client URL Prepending For GraphQL Endpoint on Enterprise (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
1 parent a46dc82 commit b56a3fe

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/Github/Client.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,14 @@ private function setEnterpriseUrl($enterpriseUrl): void
346346
$builder->removePlugin(PathPrepend::class);
347347

348348
$builder->addPlugin(new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($enterpriseUrl)));
349-
$builder->addPlugin(new PathPrepend(sprintf('/api/%s', $this->getApiVersion())));
349+
350+
// For GHE, v4 API endpoint is at `api/graphql` so we don't want to add the version number
351+
// For earlier versions add the version number after /api
352+
if ($this->getApiVersion() === 'v4') {
353+
$builder->addPlugin(new PathPrepend('/api'));
354+
} else {
355+
$builder->addPlugin(new PathPrepend(sprintf('/api/%s', $this->getApiVersion())));
356+
}
350357
}
351358

352359
/**

test/Github/Tests/ClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,25 @@ public function testEnterpriseUrl()
223223
$client = new Client($httpClientBuilder, null, 'https://foobar.com');
224224
$client->enterprise()->stats()->show('all');
225225
}
226+
227+
/**
228+
* Make sure that the prepend is correct when using the v4 endpoint on Enterprise.
229+
*/
230+
public function testEnterprisePrependGraphQLV4()
231+
{
232+
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
233+
->setMethods(['sendRequest'])
234+
->getMock();
235+
236+
$httpClientMock->expects($this->once())
237+
->method('sendRequest')
238+
->with($this->callback(function (RequestInterface $request) {
239+
return (string) $request->getUri() === 'https://foobar.com/api/graphql';
240+
}))
241+
->willReturn(new Response(200, [], '[]'));
242+
243+
$httpClientBuilder = new Builder($httpClientMock);
244+
$client = new Client($httpClientBuilder, 'v4', 'https://foobar.com');
245+
$client->graphql()->execute('query');
246+
}
226247
}

0 commit comments

Comments
 (0)