How to get GraphQL Variables on postman - graphql

Lets suppose in request body, I am using following GraphQL Variables:
{
"getUsers": {
"offset": 0,
"limit": 5,
"cursor": ""
}
}
Now on test body, I want to get value of limit, how can I do that?

Related

Graphql playground cursor queries

I am using the graphql playground client, and I am only getting back the first graphql response. I am expecting multiple responses given the data set that I am querying, and I am receiving the first response.
query($search: ServiceEntitySearch!, $cursor: CursorInput) {
searchServiceEntities(search: $search, cursor: $cursor) {
nodes {...
Query variables
"search": {},
"cursor": {
"startCursor": null,
"endCursor": "MTA0MTExMw==",
"hasNextPage": true,
"hasPreviousPage": false,
"forward": true,
"complexSort": null,
"pageSize": 100
}
}```

How to handle JSON array response while using APIConsumer?

Using the APIConsumer contract we can feed data from API to the smart contract.
Eg: If the server response is:
{
"RAW":{"ETH":{"USD":{"VOLUME24HOUR": 10000,}}}
}
Then, data can be obtained as:
request.add("get", URL);
request.add("path", "RAW.ETH.USD.VOLUME24HOUR");
Similarly, If the server response contains some JSON array,
Eg:
{
"date":"530934083405834",
"results": [
{
"id": 9865,
"rank":1
},
{
"id": 9869,
"rank": 2
},
{
"id": 9866,
"rank": 3
}
]}
Then in this case is there a way to get the id of the rank 1 i.e results[0]["id"]?
To get results[0]["id"] your path in the request needs to be
request.add("path", "results.0.id");

How to prepare the JSON payload from the response in JMeter

I am getting response from one API and need to prepare the payload from that response.
For example the response as like
{
"data": {
"total_count": 5,
"userIds": [1,2,3,4,5]
}
Need to make the payload from the response to other API like
{
"users": [
{
"user_id": 1,
"invite_amount": 100,
},
{
"user_id": 2,
"invite_amount": 100
},
{
"user_id": 3,
"invite_amount": 100
},
{
"user_id": 4,
"invite_amount": 100
},
{
"user_id": 5,
"invite_amount": 100
}
]
}
Above payload need to send to the another API
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def userIds = new groovy.json.JsonSlurper().parse(prev.getResponseData()).data.userIds
def payload = [:]
def users = []
userIds.each { userId ->
def user = [:]
user.put('user_id', userId)
user.put('invite_amount', 100)
users.add(user)
}
payload.put('users', users)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
That's it, you will be able to refer the generated payload value as ${payload} where required
More information:
JsonSlurper and JsonBuilder
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
Have a look at this link
Basically you need to use JMeter JSON Extractor

How to return HTTP status code when returning a ResourceCollection in Laravel

I'm building an API and I'm trying to return a ResourceCollection for a Classroom in Laravel.
Previously I used an array of classrooms and returned a response with the array and the status code, like this:
$classrooms=Classroom::all();
return response()->json($classrooms,200);
Now this is my code:
$classrooms = new ClassroomCollection(Classroom::paginate(10));
return $classrooms;
to get this response:
"data": [classrooms array],
"links": {
"first": "http://127.0.0.1:8000/api/classrooms ?page=1",
"last": "http://127.0.0.1:8000/api/classrooms ?page=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": null,
"last_page": 12,
"path": "http://127.0.0.1:8000/api/classrooms ",
"per_page": 10,
"to": null,
"total": 0
}
and I can't find a way to send a status code along with the ClassroomCollection, because if I do
return response()->json($classrooms,200);
I'm only returned the "data" object, without the links and meta of the paginator.
Any help?
you can override the withResponse function in your collection like this:
public function withResponse($request, $response)
{
if($response->getData()) {
$response->setStatusCode(200);
} else{
$response->setStatusCode(404);
}
parent::withResponse($request, $response);
}
If you really want to you can do the following:
return response()->json($classrooms->jsonSerialize(), 200);
->jsonSerialize() does not actually serialize as a JSON string but returns an array that can be serialized to JSON string. Laravel serializes to a JSON response if you return an array or JsonSerializableable object from a controller/route and that is what the paginator implements.
However, if 200 is the status code you want, that is implied and the default status code and there is no need to supply it.
So the above is equal to:
return $classrooms;

laravel/codeception : test if json response contains only certain keys

I have a json array coming from my api as response:
{
"data": [
{
"id": 1,
"name": "abc"
}
}
I am using laravel for api and laravel-codeception for testing.
public function getAll(ApiTester $I)
{
$I->sendGET($this->endpoint);
}
I have to test if the response contains only id and name key (not any other key) example this response should fail the test.
{
"data": [
{
"id": 1,
"name": "abc",
"email":"abc#xyz"
}
}
I have found $I->seeResponseContainsJson(), but it checks if JSON is present or not. It does not check if JSON response contains only specified keys.
Thanks.

Resources