I have a requirement where i have to accept the list of objects.
The method in mutation class looks like this
#GraphQLMutation //1
public void ack(#GraphQLInputField List<PingEntity> pingEntityList) { //2
log.info("Handling ack calls.");
pingEntityRepository.savePingEntityList(pingEntityList);
}
PingEntity looks like this
#Data
//#Document(collection="pingdatastore")
#AllArgsConstructor
#NoArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class PingEntity {
private String id;
#JsonProperty("action")
private String action;
#JsonProperty("message")
private String message;
#JsonProperty("timestamp")
private Long timestamp;
#JsonProperty("transactionId")
private String transactionId;
#JsonProperty("type")
private Integer type;
private String imei;
}
my query looks like this
mutation ack {
ack(pingEntityList: [{action: "KEEP-ALIVE2", message: "Keep alive message at regular intervals", timestamp: 1462747047}]) {
id
}
}
I got the Error like this:
"data": null,
"errors": [
{
"validationErrorType": "SubSelectionNotAllowed",
"message": "Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type Boolean",
"locations": [
{
"line": 2,
"column": 3
}
],
"errorType": "ValidationError"
}
]
}
I tried giving different annotations. i am not able to solve this issue.. need help in this issue
Thanks in advance :)
The problem seems to be that your ack method returns void which gets mapped to boolean, for the lack of a better option. As boolean is a simple scalar, you can not select anything from it, and you're trying to select an id in the query.
If you'd change your ack method to return the saved List<PingEntity>, you'd get the behavior you wanted.
But... more importantly, what library are you using, as the annotations I see (#GraphQLInputField) in your code are not from graphql-java (as graphql-java itself provides no annotations), nor from any of the libraries I recognize.
It seems to be coming from a really old and never publically released version of graphql-spqr. If this is indeed the case, you absolutely need to update to the latest as the version you seem to be using was alpha quality at best.
Related
I am working on a model that involves the data type boolean, so the issue with Jackson is that if I put in isValid it will auto convert to valid in the schema. However this model schema is in need to store into MongoDB, and I want isValid to be the field, not valid.
So I searched around and I read that #Field("isValid") should solve the issue, however it is not and it is causing #JsonProperty("isValid") not working as well.
What happened here is when I put only #JsonProperty("isValid"), it can be serialized and deserialized as isValid perfectly, however when I try to insert into MongoDB, the field change into valid instead of isValid.
Here are my code for the POJO:
#Data
#ToString
public class Response {
private String duration;
private String time;
#JsonProperty("isValid")
#Field("isValid")
private boolean isValid;
}
This is what I want inside the MongoDB:
{
"duration": "313610236",
"isValid": false,
"time": "1658304521794"
}
However now I have:
{
"duration": "313610236",
"valid": false,
"time": "1658304521794"
}
How can I achieve both function where I get to deserialize and serialize as isValid but also saved as isValid in terms of field into the MongoDB?
Thank you!
This might work for you:
#JsonIgnoreProperties({"valid"}) // Prevents serialization
#Data
#ToString
public class Response {
private String duration;
private String time;
#JsonProperty("isValid")
private boolean isValid;
}
Found a link that might be helpful for you:
Jackson renames primitive boolean field by removing 'is'
I want to send a request to my controller via Postman and I actually don't know the structure of a list containing Enum class
I'll give an example of a Enum to make it easy to explain:
public enum ReservationState {
WAITING("Waiting"),
BUSY("Busy"),
READY("Ready");
private String status;
public ReservationState(String status){
this.status = status;
}
}
So how does a List<ReservationState> reservations look in a json string?
Thanks in advance!!
Try this {"reservations": ["WAITING", "BUSY", "READY" ]}
{
"reservations": [
"WAITING",
"BUSY",
"READY"
]
}
unless you provided special serializer, enums are serialized as their enum literals
I have a need to parse some json that comes to an endpoint of mine. I cannot change the incoming json, it is sent by a 3rd party. The json in the request body is technically valid but it has no parent element so I can't seem to parse it. Ideally I'd be able to wrap the whole thing in an object, so basically, how can I actually add the "wrapper" object:
{
"wrapper": {
{
"value1": 1,
"value2": 2,
"value3": 3
}
}
}
if the original was:
{
"value1": 1,
"value2": 2,
"value3": 3
}
With that I could make a model and use xml annotation as I have elsewhere in the project, something like this:
#XmlRootElement(name = "wrapper")
#XmlAccessorType(XmlAccessType.NONE)
public class WrapperObject {
#XmlElement(name = "value1")
private int value1;
#XmlElement(name = "value2")
private int value2;
#XmlElement(name = "value3")
private int value3;
}
And then I could have the rest method be
#POST
#Path("/cloud")
#Override
public Response handleResponse(WrapperObject wrapper) throws Exception {
}
As your question is an answer, my answer will be a question. What do you ask? p.s. your idea and code look fine. When a project is spread between Front end and Back end teams they should always have a CONTRACT about the data transfer objects so parsing is possible for the both sides.
Update:
Since I feel like I'm running out of options, it might be helpful, to find a way to parse a com.google.appengine.api.datastore.Text object. How do I represent this kind of class in json?
Original question:
I am using Objectify to handle datastore operations in Google App Engine. I have a User class, and an Event class. A User has a List of Events, thus I've added the #Embed annotation to my Event class (otherwise I would get "not a supported property type" error).
#Entity
#Embed
public class Event
{
private String eventTitle;
private Date eventDate;
private int eventDuration;
private int ticketsLeft;
private String eventDescription;
private int ticketPrice;
private String location;
private int ticketsPurchased;
private boolean isPaid;
private int priority;
#Id private Long identifier;
// ... getters/setters
}
User class:
#Entity
public class User
{
#Id private Long identifier;
private String username;
private String password;
private List<Event> orderedEvents;
// ... getters/setters
}
I'm parsing the events from a JSON file uring gson. The eventDescription property of an event can get pretty long, and in these cases I get the error message in the title. I've also tried changing the String to Text, but I have no idea how to parse a Text object, and I keep getting "Expected BEGIN_OBJECT but was STRING" error. Is there any way around this limitation?
I am not sure which version of Objectify you are using currently. But as per the documentation available "String fields which store more than 500 characters (the GAE limit) are automatically converted to Text internally. "
Additionally, this has been addressed as per this discussion thread in version 4.0.
I have found a way to get around this problem, by doing what the error message suggests, and using a com.google.appengine.api.datastore.Text object instead of a String. When using Text, the json should look like the following:
{
"priority": 1,
"title": "xxxx",
"date": "2013-12-17T19:30:00Z",
"duration": 120,
"tickets": 12,
"price": 1400,
"description": {
"value": "long text goes here"
},
"location": "location"
}
I have an existing database that I am now connecting to using hibernate. I cannot change the data in it at the moment and have everything working apart from a single column.
I have a status column that has the values:
new
mailed
in
out
And the column is mapped as follows:
#Column(name = "STATUS", nullable = false, length = 50)
#Enumerated(EnumType.STRING)
private TeamMemberStatus status;
I would REALLY like (for application reasons) to have this column mapped as a Java Enum (TeamMemberStatus), but due to the fact that 'new' is a keyword in Java I cannot have that as an enum member.
If I have the enum contstants NEW, MAILED, IN and OUT hibernate fails as inside EnumType it does a Enum.valueOf().
Is there any way for me to map this to my Enum without having to write a complex UserType?
-- added content
My Enum like this:
public enum TeamMemberStatus {
NEW, MAILED, IN, OUT
}
is a valid Java enum, but not matching the case of the database. If I change it to match the database like:
public enum TeamMemberStatus {
new, mailed, in, out
}
It won't compile as 'new' is a Java reserved word.
If you can use a SQL UPPER statement at database, It will work without using any UserType
UPDATE
Well, It can not be The nicest solution but it solves what you want
#Entity
public class WrapperEntity {
private TeamMemberStatus memberStatus;
#Transient
private TeamMemberStatus getMemberStatus() {
return this.memberStatus;
}
public void setMemberStatus(TeamMemberStatus memberStatus) {
this.memberStatus = memberStatus;
}
#Column(name="STATUS", nullable=false, length=50)
public String getMemberStatusAsString() {
return memberStatus.name().toLowerCase();
}
public void setMemberStatusAsString(String memberStatus) {
this.setsetMemberStatus(TeamMemberStatus.valueOf(memberStatus.toUpperCase()));
}
}
If your Database values are "new", "mailed", "in" and "out" then your Enum need exactly the same names. - I believe that the problem is, that your Enums are in capital letters but your data base values not.