【问题标题】:Laravel GraphQL Lighthouse unit testing mutation returns error "Syntax Error: Unexpected <EOF>"Laravel GraphQL Lighthouse 单元测试突变返回错误“语法错误:意外 <EOF>”
【发布时间】:2020-11-20 10:17:09
【问题描述】:

我正在做一个 Laravel 项目。我正在使用 GraphQL Lighthouse 开发 API。我正在为我的应用程序编写单元测试,其中涉及文件上传功能。但它正在抛出错误。

这是我的变异代码:

class CreateMenuItem
{
    public function __invoke($_, array $args)
    {
        //create menu item and returns

        return $menuItem;
    }
}

我有一个突变模式如下:

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
}

这是我的验证指令:

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' ],
        ];
    }
}

我可以从 GraphQL 游乐场创建菜单项。但是当我为它编写测试时,它失败了。这是我的测试:

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
            ]
        );
    }
}

它甚至没有达到突变。我收到以下回复。

[{
    "errors": [
        {
            "message": "Syntax Error: Unexpected <EOF>",
            "extensions": {
                "category": "graphql"
            },
            "locations": [
                {
                    "line": 1,
                    "column": 1
                }
            ]
        }
    ]
}].

我的代码有什么问题,我该如何解决?

【问题讨论】:

  • 你找到解决办法了吗?
  • 很遗憾,没有。

标签: laravel graphql laravel-lighthouse


【解决方案1】:

试试这个

$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
    ]
);

【讨论】:

    【解决方案2】:

    您的请求失败的原因是您的 JSON 在 operations 中无效。

    在 json 中的 query 键中,您有一堆 ",但是您必须转义它们。这是因为它是 JSON。

    一个好主意是添加 phpdoc lang 标签,这样 IDE 可以帮助您显示您的 JSON 无效。

    所以您的查询已修复如下所示

    $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
        ]
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多