繁体   English   中英

如何为 xunit 集成测试编写 GraphQL 突变查询

[英]How to write a GraphQL mutation query for xunit integration test

我想对我的 GraphQL 突变执行集成测试。 我可以在我的 GraphiQL 中运行查询,但我真的不知道如何将其转换为带有 xunit 集成测试输入的查询字符串。

在此处输入图片说明

以上是我的GraphiQL,它可以很好地返回我的数据。 我遵循StarWars git hub 项目进行集成测试。 通过传递 graphql 查询字符串来查询 GraphQL 工作正常,但由于缺乏知识和文档,我无法将上述 GraphiQL 突变转换为查询字符串。

以下是到目前为止我的集成测试代码,其中缺少查询变量部分并且不知道如何将它们引入

        //Arrange
        const string query = @"{ 
            ""query"": "" mutation CreateMutation($input: InputType!) {
                          addNewItem(myInput: $input) {
                            col1
                            col2
                            col3
                            col4
                            col5
                    }
                } ""
        }";
        var content = new StringContent(query, Encoding.UTF8, "application/json");

        // Act
        var response = await client.PostAsync("/graphql", content);

        //Assert
        response.EnsureSuccessStatusCode();

我已经弄清楚了查询字符串的语法,它将如下所示

const string query = @"{ 
            ""query"": "" mutation CreateMutation($input: InputType!) { addNewItem(myInput: $input) { col1 col2 col3 col4 col5  }} "",
            ""variables"": { input :{ col1: 100, col2: 'starwars' }}
        }";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM