空手道 API - Hello World 示例,但 POST (GraphQL) Create/Retrieve 一个用户

Karate API - Hello World example but POST (GraphQL) Create/Retrieve a User

我的场景中有两个 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

#(replaceMe) 技巧仅适用于 JSON 而不适用于 text 类型,它是一个字符串。因此,请改用 replacehttps://github.com/intuit/karate#replace

只需更改“占位符”并在第二个之前添加一行 request:

Given text query = 
"""
{
  getUser(id: "<userID>") {
    bio
    birthDate
    createdAt
    id
  }
}
"""
And replace query.userID = userID

仅供参考,使用 GraphQL 变量可能是另一种选择(也是“更好”的做法):