c# - 如何为 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();
解决方案
我已经弄清楚了查询字符串的语法,它将如下
const string query = @"{
""query"": "" mutation CreateMutation($input: InputType!) { addNewItem(myInput: $input) { col1 col2 col3 col4 col5 }} "",
""variables"": { input :{ col1: 100, col2: 'starwars' }}
}";
推荐阅读
- c# - C# 服务器客户端在连接 UWP 时失败 (2)
- python - 升级过时的软件包后 pip 需要密码
- c++ - c++ r值引用应用于函数指针
- linux - 当 IP 地址改变时 TCP 套接字会发生什么?
- excel - 我需要帮助修复这个 VBA 宏
- r - Alternative to (m)get in data.table functions
- firebase - Difference between Firebase service account key files from Firebase and from GCloud?
- node.js - 如何从另一个(本地)目录导入库?
- sql-server - SQL 列出每辆车的最低价格
- automated-tests - Robot Framework:如何通过 value 属性单击复选框