Skip to content

Commit

Permalink
Allow opitonal upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Feb 4, 2018
1 parent aa38205 commit e24f44a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Tests/Functional/App/config/upload/mapping/schema.types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Mutation:
fields:
singleUpload:
type: String!
resolve: '@=args["file"].getBasename()'
resolve: '@=null===args["file"] ? "Sorry, No file was uploaded." : args["file"].getBasename()'
args:
file: Upload!
file: Upload
multipleUpload:
type: '[String!]'
resolve: '@=[args["files"][0].getBasename(), args["files"][1].getBasename()]'
Expand Down
15 changes: 15 additions & 0 deletions Tests/Functional/Upload/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public function testSingleUpload()
);
}

public function testOptionalUpload()
{
$this->assertUpload(
['data' => ['singleUpload' => 'Sorry, No file was uploaded.']],
[
'operations' => [
'query' => 'mutation($file: Upload) { singleUpload(file: $file) }',
'variables' => ['file' => null],
],
'map' => [],
],
[]
);
}

public function testMultipleUpload()
{
$this->assertUpload(
Expand Down
4 changes: 2 additions & 2 deletions Upload/Type/GraphQLUploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function __construct($name = null)
*/
public function parseValue($value)
{
if (!$value instanceof File) {
if (null !== $value && !$value instanceof File) {
throw new InvariantViolation(sprintf(
'Upload should be instance of "%s" but %s given.',
'Upload should be null or instance of "%s" but %s given.',
File::class,
is_object($value) ? get_class($value) : gettype($value)
));
Expand Down

0 comments on commit e24f44a

Please sign in to comment.