Skip to content

Fix Client URL Prepending For GraphQL Endpoint on Enterprise #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}