Skip to content

Commit

Permalink
Treat UploadType like any regular type which has to be registered
Browse files Browse the repository at this point in the history
  • Loading branch information
mfn committed Jul 10, 2019
1 parent 1f02f09 commit 1ec5cfa
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
[Next release](https://github.com/rebing/graphql-laravel/compare/v1.24.0...master)
------------
## Breaking changes
- The `UploadType` now has to be added manually to the `types` in your schema if you want to use it
- The `::getInstance()` method is gone
- The order and arguments/types for resolvers has changed:
- before: `resolve($root, $array, SelectFields $selectFields, ResolveInfo $info)`
- after: `resolve($root, $array, $context, ResolveInfo $info, Closure $getSelectFields)`
Expand Down
11 changes: 6 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,11 @@ public function validationErrorMessages(array $args = []): array

#### File uploads

For uploading new files just use `UploadType`. This support of uploading files is
based on https://github.com/jaydenseric/graphql-multipart-request-spec
so you have to upload them as multipart form:
This library provides a middleware compliant with the spec at https://github.com/jaydenseric/graphql-multipart-request-spec .

You have to add the `\Rebing\GraphQL\Support\UploadType` first to your `config/graphql` schema types definition.

It is relevant that you send the request as `multipart/form-data`:

> **WARNING:** when you are uploading files, Laravel will use FormRequest - it means
> that middlewares which are changing request, will not have any effect.
Expand All @@ -534,7 +536,6 @@ use Closure;
use GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\UploadType;
use Rebing\GraphQL\Support\Mutation;

class UserProfilePhotoMutation extends Mutation
Expand All @@ -553,7 +554,7 @@ class UserProfilePhotoMutation extends Mutation
return [
'profilePicture' => [
'name' => 'profilePicture',
'type' => UploadType::getInstance(),
'type' => GraphQL::type('Upload'),
'rules' => ['required', 'image', 'max:1500'],
],
];
Expand Down
13 changes: 5 additions & 8 deletions src/Support/UploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rebing\GraphQL\Support;

use GraphQL\Error\Error;
use GraphQL\Type\Definition\Type;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\ScalarType;
use Rebing\GraphQL\Support\Contracts\TypeConvertible;

class UploadType extends ScalarType
class UploadType extends ScalarType implements TypeConvertible
{
/**
* @var string
Expand Down Expand Up @@ -52,13 +54,8 @@ public function parseLiteral($valueNode, ?array $variables = null)
throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. Instead got: '.$valueNode->kind, [$valueNode]);
}

public static function getInstance()
public function toType(): Type
{
static $inst = null;
if ($inst === null) {
$inst = new self();
}

return $inst;
return new static();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/UploadTests/UploadMultipleFilesMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GraphQL\Type\Definition\Type;
use Illuminate\Http\Testing\File;
use Rebing\GraphQL\Support\Mutation;
use Rebing\GraphQL\Support\UploadType;
use Rebing\GraphQL\Support\Facades\GraphQL;

class UploadMultipleFilesMutation extends Mutation
{
Expand All @@ -24,7 +24,7 @@ public function args(): array
{
return [
'files' => [
'type' => Type::listOf(UploadType::getInstance()),
'type' => Type::listOf(GraphQL::type('Upload')),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/UploadTests/UploadSingleFileMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Mutation;
use Rebing\GraphQL\Support\UploadType;
use Rebing\GraphQL\Support\Facades\GraphQL;

class UploadSingleFileMutation extends Mutation
{
Expand All @@ -23,7 +23,7 @@ public function args(): array
{
return [
'file' => [
'type' => UploadType::getInstance(),
'type' => GraphQL::type('Upload'),
],
];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/UploadTests/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Http\UploadedFile;
use Rebing\GraphQL\Tests\TestCase;
use Rebing\GraphQL\Support\UploadType;

class UploadTest extends TestCase
{
Expand Down Expand Up @@ -184,5 +185,8 @@ protected function getEnvironmentSetUp($app)
UploadSingleFileMutation::class,
],
]);
$app['config']->set('graphql.types', [
UploadType::class,
]);
}
}

0 comments on commit 1ec5cfa

Please sign in to comment.