-
Notifications
You must be signed in to change notification settings - Fork 222
Make the encoder for the cursors of the edges of a connection customizable #678
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
mcg-web
merged 3 commits into
overblog:master
from
ste93cry:feature/custom-connection-cursor-encoder
May 23, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
UPGRADE FROM 0.13 to 1.0 | ||
======================= | ||
|
||
# Table of Contents | ||
|
||
- [Customize the cursor encoder of the edges of a connection](#customize-the-cursor-encoder-of-the-edges-of-a-connection) | ||
|
||
### Customize the cursor encoder of the edges of a connection | ||
|
||
The connection builder now accepts an optional custom cursor encoder as first argument of the constructor. | ||
|
||
```diff | ||
$connectionBuilder = new ConnectionBuilder( | ||
+ new class implements CursorEncoderInterface { | ||
+ public function encode($value): string | ||
+ { | ||
+ ... | ||
+ } | ||
+ | ||
+ public function decode(string $cursor) | ||
+ { | ||
+ ... | ||
+ } | ||
+ } | ||
static function (iterable $edges, PageInfoInterface $pageInfo) { | ||
... | ||
}, | ||
static function (string $cursor, $value, int $index) { | ||
... | ||
} | ||
); | ||
``` |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Overblog\GraphQLBundle\Relay\Connection\Cursor; | ||
|
||
use Overblog\GraphQLBundle\Util\Base64Encoder; | ||
|
||
/** | ||
* @phpstan-implements CursorEncoderInterface<string> | ||
*/ | ||
final class Base64CursorEncoder implements CursorEncoderInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($value): string | ||
{ | ||
return Base64Encoder::encode($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decode(string $cursor) | ||
{ | ||
return Base64Encoder::decode($cursor); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Relay/Connection/Cursor/Base64UrlSafeCursorEncoder.php
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Overblog\GraphQLBundle\Relay\Connection\Cursor; | ||
|
||
use Overblog\GraphQLBundle\Util\Base64Encoder; | ||
|
||
/** | ||
* @phpstan-implements CursorEncoderInterface<string> | ||
*/ | ||
final class Base64UrlSafeCursorEncoder implements CursorEncoderInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($value): string | ||
{ | ||
return Base64Encoder::encodeUrlSafe($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decode(string $cursor) | ||
{ | ||
return Base64Encoder::decodeUrlSafe($cursor); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Overblog\GraphQLBundle\Relay\Connection\Cursor; | ||
|
||
/** | ||
* @phpstan-template T | ||
*/ | ||
interface CursorEncoderInterface | ||
{ | ||
/** | ||
* @param mixed $value | ||
* | ||
* @phpstan-param T $value | ||
*/ | ||
public function encode($value): string; | ||
|
||
/** | ||
* @return mixed | ||
* | ||
* @phpstan-return T | ||
*/ | ||
public function decode(string $cursor); | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Overblog\GraphQLBundle\Relay\Connection\Cursor; | ||
|
||
/** | ||
* @phpstan-implements CursorEncoderInterface<string> | ||
*/ | ||
final class PlainCursorEncoder implements CursorEncoderInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decode(string $cursor) | ||
{ | ||
return $cursor; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Overblog\GraphQLBundle\Util; | ||
|
||
final class Base64Encoder | ||
{ | ||
public static function encode(string $value): string | ||
{ | ||
return \base64_encode($value); | ||
} | ||
|
||
public static function decode(string $value, bool $strict = true): string | ||
{ | ||
$result = \base64_decode($value, $strict); | ||
|
||
if (false === $result) { | ||
throw new \InvalidArgumentException(\sprintf('The "%s" value failed to be decoded from base64 format.', $value)); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public static function encodeUrlSafe(string $value, bool $padding = false): string | ||
{ | ||
$result = \base64_encode($value); | ||
$result = \str_replace(['+', '/'], ['-', '_'], $result); | ||
|
||
if (!$padding) { | ||
$result = \str_replace('=', '', $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public static function decodeUrlSafe(string $value, bool $strict = true): string | ||
{ | ||
$value = \str_replace(['-', '_'], ['+', '/'], $value); | ||
|
||
if (0 === \substr_compare($value, '=', -1) && 0 !== \strlen($value) % 4) { | ||
$value = \str_pad($value, (\strlen($value) + 3) & ~3, '='); | ||
} | ||
|
||
$result = \base64_decode($value, $strict); | ||
|
||
if (false === $result) { | ||
throw new \InvalidArgumentException(\sprintf('The "%s" value failed to be decoded from base64 format.', $value)); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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
Oops, something went wrong.
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.