Skip to content

Commit

Permalink
Add Lighthouse v6 support (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: Wim Reckman <[email protected]>
  • Loading branch information
wimski and WimWidgets authored Mar 28, 2023
1 parent 7c0d9d5 commit 5e42204
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": "^8.1",
"czim/laravel-paperclip": "^5",
"nuwave/lighthouse": "^5"
"nuwave/lighthouse": "^6"
},
"require-dev": {
"laravel/pint": "^1.2",
Expand Down
25 changes: 6 additions & 19 deletions src/Directives/AttachmentDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Closure;
use Czim\Paperclip\Contracts\AttachmentInterface;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
Expand All @@ -24,26 +24,13 @@ public static function definition(): string
SDL;
}

public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
public function handleField(FieldValue $fieldValue): void
{
// Retrieve the existing resolver function
/** @var Closure $previousResolver */
$previousResolver = $fieldValue->getResolver();

// Wrap around the resolver
$wrappedResolver = function ($root, array $args, GraphQLContext $context, ResolveInfo $info) use ($previousResolver): ?string {
// Call the resolver, passing along the resolver arguments
/** @var AttachmentInterface $result */
$result = $previousResolver($root, $args, $context, $info);
$fieldValue->wrapResolver(fn (callable $resolver): Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
/** @var AttachmentInterface $result $result */
$result = $resolver($root, $args, $context, $resolveInfo);

return $result->url($args['variant'] ?? null);
};

// Place the wrapped resolver back upon the FieldValue
// It is not resolved right now - we just prepare it
$fieldValue->setResolver($wrappedResolver);

// Keep the middleware chain going
return $next($fieldValue);
});
}
}
3 changes: 2 additions & 1 deletion src/Directives/VariantDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Exception;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\Parser;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function manipulateArgDefinition(
DocumentAST &$documentAST,
InputValueDefinitionNode &$argDefinition,
FieldDefinitionNode &$parentField,
ObjectTypeDefinitionNode &$parentType
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType,
): void {
/** @var string $modelName */
$modelName = ASTHelper::modelName($parentType);
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DanielDeWit\LighthousePaperclip\Providers\LighthousePaperclipServiceProvider;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Nuwave\Lighthouse\LighthouseServiceProvider;
use Nuwave\Lighthouse\Testing\TestingServiceProvider;
use Orchestra\Testbench\TestCase;

abstract class UnitTest extends TestCase
Expand All @@ -18,6 +19,7 @@ protected function getPackageProviders($app): array
return [
LighthouseServiceProvider::class,
LighthousePaperclipServiceProvider::class,
TestingServiceProvider::class,
];
}

Expand Down

0 comments on commit 5e42204

Please sign in to comment.