get key value from GraphQL query

474 Views Asked by At

I have a requirement to get the value from the Graphql query and validate it with a string, I tried converting it to json and used jsonpath to get the value but it is failling

Here is the request query

{
  ethereum {
    dexTrades(date: {is:"2020-11-29"} options: {limit: 10}
      baseCurrency: {is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}
        quoteCurrency:{is: "0xdac17f958d2ee523a2206206994597c13d831ec7"}
    ){


      baseCurrency {
        symbol
        address
      }
      baseAmount

      quoteCurrency {
        symbol
        address
      }
      quoteAmount

      quotePrice



    }
  }
}

I would I like to get date and below is the code I am trying

    public String getnodefromjson(String payload,String path) throws JSONException {
//        System.out.println("test"+payload.toString());
        String symbol=null;
        if(isJSONValid(payload)){

            symbol=""+JsonPath.from(payload).getString(path);

            try {
                System.out.println("symbol value 9s - " + symbol.toString());
            }catch (NullPointerException ex){
                System.out.println("Key doesnt exist - "+path);
            }
        }else {
            printResults("Not a valid json file");

        }

calling method

getnodefromjson(jsoncontent,"$..date");

Below is the error message

java.lang.IllegalArgumentException: The parameter "date" was used but not defined. Define parameters using the JsonPath.params(...) function
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:72)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:286)
    at io.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:50)
    at io.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
    at io.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:28)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:203)
    at io.restassured.path.json.JsonPath.getString(JsonPath.java:352)
0

There are 0 best solutions below