-
Notifications
You must be signed in to change notification settings - Fork 20
Add ReactPhp client for non-blocking GraphQL calls #137
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
Open
spawnia
wants to merge
5
commits into
master
Choose a base branch
from
add-reactphp-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
963b5c3
Add ReactPhp client for non-blocking GraphQL calls
spawnia c843125
Address review: PHP compat and README install snippet
spawnia 2c84f75
Ignore PHPStan error from lowest react/async await() return type
spawnia 4aefcd6
Use imports in ReactPhpTest
spawnia 93615ca
Apply php-cs-fixer changes
spawnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Spawnia\Sailor\Client; | ||
|
|
||
| use React\Http\Browser; | ||
| use Spawnia\Sailor\Client; | ||
| use Spawnia\Sailor\Response; | ||
|
|
||
| use function React\Async\await; | ||
| use function Safe\json_encode; | ||
|
|
||
| class ReactPhp implements Client | ||
| { | ||
| protected string $uri; | ||
|
|
||
| protected Browser $browser; | ||
|
|
||
| public function __construct(string $uri, ?Browser $browser = null) | ||
| { | ||
| $this->uri = $uri; | ||
| $this->browser = $browser ?? new Browser(); | ||
| } | ||
|
|
||
| public function request(string $query, ?\stdClass $variables = null): Response | ||
| { | ||
| $body = ['query' => $query]; | ||
| if (! is_null($variables)) { | ||
| $body['variables'] = $variables; | ||
| } | ||
|
|
||
| $json = json_encode($body); | ||
| $response = await($this->browser->post($this->uri, ['Content-Type' => 'application/json'], $json)); | ||
|
|
||
| return Response::fromResponseInterface($response); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Spawnia\Sailor\Tests\Unit\Client; | ||
|
|
||
| use Mockery; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\StreamInterface; | ||
| use React\Http\Browser; | ||
| use Spawnia\Sailor\Client\ReactPhp; | ||
| use Spawnia\Sailor\Error\UnexpectedResponse; | ||
| use Spawnia\Sailor\Tests\TestCase; | ||
|
|
||
| use function React\Promise\resolve; | ||
|
|
||
| /** @requires function React\Async\await */ | ||
| final class ReactPhpTest extends TestCase | ||
| { | ||
| public function testRequest(): void | ||
| { | ||
| $uri = 'https://simple.bar/graphql'; | ||
| $expectedBody = /* @lang JSON */ '{"query":"{simple}"}'; | ||
|
|
||
| $browser = \Mockery::mock(Browser::class); | ||
| $browser->shouldReceive('post') | ||
| ->once() | ||
| ->withArgs(function (string $url, array $headers, string $body) use ($uri, $expectedBody): bool { | ||
| return $url === $uri | ||
| && $headers === ['Content-Type' => 'application/json'] | ||
| && $body === $expectedBody; | ||
| }) | ||
| ->andReturn(resolve($this->mockResponse(200, /* @lang JSON */ '{"data": {"simple": "bar"}}'))); | ||
|
|
||
| $client = new ReactPhp($uri, $browser); | ||
| $response = $client->request(/* @lang GraphQL */ '{simple}'); | ||
|
|
||
| self::assertEquals( | ||
| (object) ['simple' => 'bar'], | ||
| $response->data, | ||
| ); | ||
| } | ||
|
|
||
| public function testRequestWithVariables(): void | ||
| { | ||
| $uri = 'https://simple.bar/graphql'; | ||
| $variables = (object) ['foo' => 'bar']; | ||
| $expectedBody = /* @lang JSON */ '{"query":"{simple}","variables":{"foo":"bar"}}'; | ||
|
|
||
| $browser = \Mockery::mock(Browser::class); | ||
| $browser->shouldReceive('post') | ||
| ->once() | ||
| ->withArgs(function (string $url, array $headers, string $body) use ($uri, $expectedBody): bool { | ||
| return $url === $uri | ||
| && $headers === ['Content-Type' => 'application/json'] | ||
| && $body === $expectedBody; | ||
| }) | ||
| ->andReturn(resolve($this->mockResponse(200, /* @lang JSON */ '{"data": {"simple": "bar"}}'))); | ||
|
|
||
| $client = new ReactPhp($uri, $browser); | ||
| $response = $client->request(/* @lang GraphQL */ '{simple}', $variables); | ||
|
|
||
| self::assertEquals( | ||
| (object) ['simple' => 'bar'], | ||
| $response->data, | ||
| ); | ||
| } | ||
|
|
||
| public function testNon200StatusThrows(): void | ||
| { | ||
| $uri = 'https://simple.bar/graphql'; | ||
|
|
||
| $browser = \Mockery::mock(Browser::class); | ||
| $browser->shouldReceive('post') | ||
| ->once() | ||
| ->andReturn(resolve($this->mockResponse(500, 'Internal Server Error'))); | ||
|
|
||
| $client = new ReactPhp($uri, $browser); | ||
|
|
||
| $this->expectException(UnexpectedResponse::class); | ||
| $client->request(/* @lang GraphQL */ '{simple}'); | ||
| } | ||
|
|
||
| /** @return ResponseInterface&Mockery\MockInterface */ | ||
| private function mockResponse(int $statusCode, string $body): ResponseInterface | ||
| { | ||
| $stream = \Mockery::mock(StreamInterface::class); | ||
| $stream->shouldReceive('getContents')->andReturn($body); | ||
| $stream->shouldReceive('__toString')->andReturn($body); | ||
|
|
||
| $response = \Mockery::mock(ResponseInterface::class); | ||
| $response->shouldReceive('getStatusCode')->andReturn($statusCode); | ||
| $response->shouldReceive('getBody')->andReturn($stream); | ||
| $response->shouldReceive('getHeaders')->andReturn([]); | ||
|
|
||
| return $response; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.