leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 181 forks source link

How can I define a GraphQL DTO attribute holding raw JSON? #498

Open midmarch opened 3 months ago

midmarch commented 3 months ago

Is it possible to define an attribute with raw JSON?

I have got the following DTO, with the property `geoShape is raw GeoJSON JSON.


import io.leangen.graphql.annotations.GraphQLInputField;
import io.leangen.graphql.annotations.GraphQLQuery;

public class GeoLocation {

  /**
     * Geo
     */
  @GraphQLQuery
  @GraphQLInputField
  private String geoShape;
}

Returning the following result querying for that attribute:

{
  "geoShape": "{ \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\",  \"coordinates\": [125.6, 10.1]   }, \"properties\": {      \"name\": \"Dinagat Islands\"  }  }"
}  

What I like to havem is the following:

{
  "geoShape": {
    "type": "Feature",
     "geometry": {
      "type": "Point",
      "coordinates": [125.6, 10.1]
    },
    "properties": {
      "name": "Dinagat Islands"
    } 
  }
}  

I tried to add @JsonRawValue


import com.fasterxml.jackson.annotation.JsonRawValue;
import io.leangen.graphql.annotations.GraphQLInputField;
import io.leangen.graphql.annotations.GraphQLQuery;

public class GeoLocation {

  /**
     * Geo
     */
  @GraphQLQuery
  @GraphQLInputField
  @JsonRawValue
  private String geoShape;
}

But that made no difference. Also tried to use a Jackson JsonNode, instread of a String, which resulted in Runtime errors.

Do you have any advice?