空手道API-Hello World示例,但POST(GraphQL)创建/检索用户 [英] Karate API - Hello World example but POST (GraphQL) Create/Retrieve a User
本文介绍了空手道API-Hello World示例,但POST(GraphQL)创建/检索用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的场景中有两个GraphQL帖子,与Hello World示例类似:我想创建一个用户(api-createUser),然后检索该用户(api-getUser)以在其上运行断言。
我不确定是不是GraphQL帖子查询把我绊倒了,但可能有人以前遇到过这个问题。返回错误并返回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
JSON
#(replaceMe)
技巧仅适用于推荐答案,而不适用于text
字符串类型。因此请使用replace
:https://github.com/intuit/karate#replace
只需更改占位符并在第二个request
前添加一行request
:
Given text query =
"""
{
getUser(id: "<userID>") {
bio
birthDate
createdAt
id
}
}
"""
And replace query.userID = userID
仅供参考,使用GraphQL变量可能是另一种选择(和更好的做法):https://stackoverflow.com/a/62162541/143475
这篇关于空手道API-Hello World示例,但POST(GraphQL)创建/检索用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文