首页 > 解决方案 > 如何为 xunit 集成测试编写 GraphQL 变异查询

问题描述

我想对我的 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();

标签: c#graphqlxunit

解决方案


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

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

推荐阅读