我的场景中有两个 graphQL 帖子,就像 Hello World 示例一样:我想创建一个用户(API - createUser),然后检索该用户(API - getUser)以运行断言。
我不确定它是否是 graphQL Post 查询让我绊倒的,但也许有人以前遇到过这个。出现错误并伴有 graphQL 解析错误,我假设它没有从第一个 POST 调用响应中正确传递用户 ID。
Scenario: Create a User
Given path 'query'
And header Accept = 'application/json'
Given text query =
"""
mutation {
createUser(username: "CreateUser16"
, name: "Auto CreateUser", isPrivate: true) {
id
username
lastLoginAt
createdAt
}
}
"""
And request { query: '#(query)' }
When method POST
Then status 200
And print response
And match response.data.createUser.id !=null
* def userID = response.data.createUser.id
And print userID
Given path 'query'
And header Accept = 'application/json'
Given text query =
"""
{
getUser(id: '#(userID') {
bio
birthDate
createdAt
id
}
}
"""
And request { query: '#(query)' }
When method POST
Then status 200
And print response
The #(replaceMe)
技巧仅适用于 JSON,不适用于text
type 这是一个字符串。所以使用replace
反而:https://github.com/intuit/karate#replace
只需更改“占位符”并在第二个之前添加一行request
:
Given text query =
"""
{
getUser(id: "<userID>") {
bio
birthDate
createdAt
id
}
}
"""
And replace query.userID = userID
仅供参考,使用 GraphQL 变量可能是另一种选择(也是“更好”的做法):https://stackoverflow.com/a/62162541/143475
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)