mark quinteros
mark quinteros

Reputation: 129

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

I have two graphQL posts in my scenario, and like the Hello World example: I would like to create a user (API - createUser) and then retrieve that user (API - getUser) to run assertions on.

I am not sure if its the graphQL Post query tripping me up but maybe someone has run into this before. There error is coming back with a graphQL parsing error, and I am assuming its not passing the userID correctly from the first POST call response.

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

Upvotes: 1

Views: 531

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58098

The #(replaceMe) trick works only for JSON and not for the text type which is a string. So use replace instead: https://github.com/intuit/karate#replace

Just change the "placeholder" and add a line before the second request:

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

Just FYI, using GraphQL variables may be another option (and "better" practice): https://stackoverflow.com/a/62162541/143475

Upvotes: 1

Related Questions