Skip to content

Commit

Permalink
Propagate client-safety of endpoint errors (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jan 4, 2022
1 parent 7a1d2eb commit e567c4c
Show file tree
Hide file tree
Showing 48 changed files with 454 additions and 123 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.16.0

### Added

- Add configuration `EndpointConfig::errorsAreClientSafe()` to propagate client-safety of endpoint errors

### Changed

- Move `Spawnia\Sailor\ResultErrorsException` to `Spawnia\Sailor\Error\ResultErrorsException`
- Move `Spawnia\Sailor\InvalidDataException` to `Spawnia\Sailor\Error\InvalidDataException`
- Include only messages in `ResultErrorsException::$message`, expose full `Error` objects as `ResultErrorsException::$errors`

### Removed

- Remove `Spawnia\Sailor\Response::assertErrorFree()`

## v0.15.0

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'withBenSampoEnum' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\CustomTypes\TypeConverters\BenSampoEnumConverter),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'withCustomEnum' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\CustomTypes\TypeConverters\CustomEnumConverter),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'withEnumInput' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\CustomTypes\Operations\MyEnumInputQuery\WithEnumInput\EnumObject),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ protected function converters(): array
'default' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\EnumConverter),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
5 changes: 5 additions & 0 deletions examples/custom-types/expected/Types/EnumInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ protected function converters(): array
'custom' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\CustomTypes\TypeConverters\CustomEnumConverter),
];
}

public static function endpoint(): string
{
return 'custom-types';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'takeSomeInput' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\IntConverter),
];
}

public static function endpoint(): string
{
return 'input';
}
}
5 changes: 5 additions & 0 deletions examples/input/expected/Types/SomeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ protected function converters(): array
'nested' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Input\Types\SomeInput),
];
}

public static function endpoint(): string
{
return 'input';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'name' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ protected function converters(): array
'title' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ protected function converters(): array
'name' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ protected function converters(): array
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'polymorphic';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'singleObject' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Simple\Operations\MyObjectNestedQuery\SingleObject\SomeObject),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'value' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\IntConverter),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'nested' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Simple\Operations\MyObjectNestedQuery\SingleObject\Nested\SomeObject),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'singleObject' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Simple\Operations\MyObjectQuery\SingleObject\SomeObject),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'value' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\IntConverter),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'scalarWithArg' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\IDConverter),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
5 changes: 5 additions & 0 deletions examples/simple/expected/Operations/TwoArgs/TwoArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected function converters(): array
'twoArgs' => new \Spawnia\Sailor\Convert\NullConverter(new \Spawnia\Sailor\Convert\IDConverter),
];
}

public static function endpoint(): string
{
return 'simple';
}
}
11 changes: 11 additions & 0 deletions src/BelongsToEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Spawnia\Sailor;

interface BelongsToEndpoint
{
/**
* The configured endpoint this class belongs to.
*/
public static function endpoint(): string;
}
4 changes: 3 additions & 1 deletion src/Codegen/ObjectLikeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ObjectLikeBuilder
*/
private array $optionalProperties = [];

public function __construct(string $name, string $namespace)
public function __construct(string $name, string $namespace, string $endpointName)
{
$class = new ClassType($name, new PhpNamespace($namespace));

Expand All @@ -55,6 +55,8 @@ public function __construct(string $name, string $namespace)
);
$this->converters = $converters;

ClassHelper::setEndpoint($class, $endpointName);

$this->class = $class;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Codegen/OperationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ protected function makeObjectLikeBuilder(string $name): ObjectLikeBuilder
{
return new ObjectLikeBuilder(
$name,
$this->currentNamespace()
$this->currentNamespace(),
$this->endpointName
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Console/IntrospectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string $endpointName */
foreach ($endpointNames as $endpointName) {
echo "Running introspection on endpoint {$endpointName}...\n";
$generator = new Introspector(
Configuration::endpoint($endpointName)
);
$generator->introspect();
(new Introspector(
Configuration::endpoint($endpointName),
$endpointName
))->introspect();
}

echo "Successfully introspected. You might want to rerun codegen: vendor/bin/sailor\n";
Expand Down
8 changes: 8 additions & 0 deletions src/EndpointConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function parseError(stdClass $error): Error
return Error::fromStdClass($error);
}

/**
* Is it safe to display the errors from the endpoint to clients?
*/
public function errorsAreClientSafe(): bool
{
return false;
}

/**
* Return a map from type names to a TypeConfig describing how to deal with them.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Spawnia\Sailor\Error;

use Exception;
use GraphQL\Error\ClientAware;
use stdClass;

/**
* Representation of an error according to https://spec.graphql.org/October2021/#sec-Errors.
*/
class Error extends Exception
class Error extends Exception implements ClientAware
{
use OriginatesFromEndpoint;

/**
* Description of the error intended for the developer as a guide to understand and correct the error.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Spawnia\Sailor;
namespace Spawnia\Sailor\Error;

class InvalidDataException extends \Exception
{
Expand Down
Loading

0 comments on commit e567c4c

Please sign in to comment.