【问题标题】:How to send a mutation on testing in Laravel using (Lighthouse package)如何使用(Lighthouse 包)在 Laravel 中发送测试突变
【发布时间】:2019-07-25 00:05:05
【问题描述】:

在这个post上作者做了一个这样的例子助手 并且使用的包是Lighthouse

public function graphql(string $query)
{
    return $this->post('/graphql', [
      'query' => $query
    ]);
}

所以可以这样使用:

$response = $this->graphql("{articles(first: 10) { edges { node { title } } } }");

但是我可以在突变上实现它吗?如果例如我有一个突变:

type Mutation {
   sampleMutation(
      id: ID!
   )
}

我不确定如何在突变上执行此操作。

【问题讨论】:

    标签: laravel graphql laravel-lighthouse


    【解决方案1】:

    可以测试突变。

    在最新版本的 Lighthouse 中,您现在可以将一个特征添加到您的测试类中,称为 MakesGraphQLRequests
    以下示例来自他们的docs,是关于如何为突变创建测试的示例。

    public function testCreatePost(): void
    {
        /** @var \Illuminate\Foundation\Testing\TestResponse $response */
        $response = $this->postGraphQL([
            'query' => '
                mutation CreatePost($title: String!) {
                    createPost(title: $title) {
                        id
                    }
                }
            ',
            'variables' => [
                'title' => 'Automatic testing proven to reduce stress levels in developers'
            ],
        ]);
    }
    

    您获得的响应对象将包含来自您的突变的 json 结果。所以在这里你可以像通常使用 Laravel 一样进行任何 json 断言。
    在他们的文档中,some examples 说明了 json 验证的外观。

    免责声明:我是所引用文章的作者。

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2020-08-18
      • 2020-11-06
      • 2020-05-01
      • 2020-03-30
      • 2020-11-20
      • 2021-09-09
      • 1970-01-01
      • 2018-03-17
      相关资源
      最近更新 更多