Reputation: 14791
I am working on a Laravel project. I am developing an API using GraphQL Lighthouse. I am writing the unit tests for my application where it involves file upload feature. But it is throwing error.
This is my mutation code:
class CreateMenuItem
{
public function __invoke($_, array $args)
{
//create menu item and returns
return $menuItem;
}
}
I have a schema for the mutation as follows:
input CreateMenuItemInput {
image: Upload
name: String
price: Float
menu_item_option_name: [String]
menu_item_option_price: [Float]
menu_item_extra_name: [String]
menu_item_extra_price: [Float]
}
extend type Mutation @middleware(checks: ["auth:api", "auth.restaurant-admin"]) {
createMenuItem(input: CreateMenuItemInput): MenuItem! @createMenuItemValidation
}
This is my validation directive:
use Nuwave\Lighthouse\Schema\Directives\ValidationDirective;
class CreateMenuItemValidationDirective extends ValidationDirective
{
public function name()
{
return 'createMenuItemValidation';
}
public function rules(): array
{
return [
'input.name' => [ 'required' ],
'input.price' => [ 'required', 'numeric' ],
'input.menu_item_option_name' => [ 'required', 'array' ],
'input.menu_item_option_price' => [ 'required', 'array' ],
'input.menu_item_extra_name' => [ 'required', 'array' ],
'input.menu_item_extra_price' => [ 'required', 'array' ],
];
}
}
I can create the menu item from the GraphQL playground. But when I write the test for it, it is failing. This is my test:
class CreateMenuItemTest extends RestaurantAdminTestCase
{
/** @test */
public function creating_menu_item_create_menu_if_not_exists()
{
Passport::actingAs($this->restaurantAdmin);
$imageFile = UploadedFile::fake()->create('image.jpg', 500);
$this->multipartGraphQL(
[
'operations' =>
'
{
"query": "mutation Upload($image: Upload) { createMenuItem(input: { image: $image, name: "testing", price: 12, menu_item_option_name:[ "testing 1", "testing 2" ], menu_item_option_price:[ 11, 12 ], menu_item_extra_name:[ "testing 1", "testing 2" ], menu_item_extra_price:[ 12, 13 ] }) { id } }",
"variables": {
"image": null
}
}
',
'map' =>
'
{
"0": ["variables.image"]
}
'
],
[
'0' => $imageFile
]
);
}
}
It is not even hitting the mutation. I get the following response.
[{
"errors": [
{
"message": "Syntax Error: Unexpected <EOF>",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 1
}
]
}
]
}].
What is wrong with my code and how can I fix it?
Upvotes: 1
Views: 2323
Reputation: 587
Try this
$imageFile = UploadedFile::fake()->create('image.jpg', 500);
$response = $this->multipartGraphQL(
[
'operations' => json_encode(
[
'query' => /** @lang graphql*/
'"mutation Upload($image: Upload) { createMenuItem(input: { image: $image, name: \\"testing\\", price: 12, menu_item_option_name:[ \\"testing 1\\", \\"testing 2\\" ], menu_item_option_price:[ 11, 12 ], menu_item_extra_name:[ \\"testing 1\\", \\"testing 2\\" ], menu_item_extra_price:[ 12, 13 ] }) { id } }",
"variables": {
"image": null
}'
]),
'map' =>'{"0": ["variables.image"]}'
],
[
'0' => $imageFile
]
);
Upvotes: 1
Reputation: 1866
The reason your request is failing is because your JSON is invalid in operations
.
Inside your query
key in the json, you have a bunch of "
, however you have to escape them. This is because it's JSON.
A great idea is to add phpdoc lang tags, this way IDE's can help you to show that your JSON is invalid.
So your query fixed will look the following way
$imageFile = UploadedFile::fake()->create('image.jpg', 500);
$response = $this->multipartGraphQL(
[
'operations' => /** @lang JSON */
'
{
"query": "mutation Upload($image: Upload) { createMenuItem(input: { image: $image, name: \\"testing\\", price: 12, menu_item_option_name:[ \\"testing 1\\", \\"testing 2\\" ], menu_item_option_price:[ 11, 12 ], menu_item_extra_name:[ \\"testing 1\\", \\"testing 2\\" ], menu_item_extra_price:[ 12, 13 ] }) { id } }",
"variables": {
"image": null
}
}
',
'map' => /** @lang JSON */
'
{
"0": ["variables.image"]
}
'
],
[
'0' => $imageFile
]
);
Upvotes: 0