Илья Шилков
Илья Шилков

Reputation: 35

How can I get data from the api graphQL?

I make a request on api graphQL and have the responce of body:

{
"dataRequests": [
    {
        "status": "success",
        "title": "token",
        "values": {
            "limit": 1,
            "offset": 0,
            "count": 1,
            "total": 1,
            "elements": [
                {
                    "type": "DOMAIN",
                    "permission": "default",
                    "properties": [
                        {
                            "name": "property:id",
                            "value": 390
                        },
                        {
                            "name": "setting:crawler:token",
                            "value": "token(here's real token)"
                        }
                    ],
                    "filters": []
                }
            ]
        }
    }
]
       }

I want to get value of the field "value" with token. But i have a problem to desirialize it.

My code can get the list of maps of 'dataRequests' field(using RestAssured):

GraphQLSteps graphQLSteps = new GraphQLSteps();

    Response response = graphQLSteps.postProjectToken(id);

    List<Map<String, String>> dataRequest = response.jsonPath().getList("dataRequests");

but, if i try to get the list of maps of 'value' field:

List<Map<String, String>> dataRequest = response.jsonPath().getList("value");

i get the "null" value. I think it's because i have to wrap it through the whole tree: values - elements - properties and only then value. But it's very complicated and i tried to get it in this way, but obtain only the same result "null".

I noticed that if i print an entrySet of an existing map from list:

List<Map<String, String>> dataRequest = response.jsonPath().getList("dataRequests");

    for (Map<String, String> map : dataRequest) {
        System.out.println(map.entrySet());
    }

I get the result:

[status=success, title=token, values={limit=1, offset=0, count=1, total=1, elements=[{type=DOMAIN, permission=default, properties=[{name=property:id, value=390}, {name=setting:crawler:token, value=someToken}], filters=[]}]}]

And can see value with token there.

Can you prompt me, how can i get the "value" with token either from this List<Map<String, String>> or by other way with deserialitation api?

Upvotes: 0

Views: 601

Answers (1)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

Your json is kind of complicated with many nested level. I would recommend to use lib jsonpath, instead of using jsonpath provided by rest-assured.

Example:

List<String> values = JsonPath.read(response.toString(), "$..value");
System.out.println(values);
//[390,"token(here's real token)"]

add jsonpath to pom.xml

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.6.0</version>
</dependency>

Upvotes: 1

Related Questions