How to access non-standard root field in Apollo Client (GraphQL)? - android

I am using Apollo in my Kotlin Multiplatform Android application. I write requests like this:
apolloClient.query(query).execute().onEach{ response ->
// Here we access data via response.data
}
The problem is that the responses to some requests from the server contain non-standard root fields along with the "data" field.
An example of such a response:
{
"data": {
"user": {
"name": "John",
"surname": "Smith"
}
},
"other_data": {
"some_field": "blah blah blah"
}
}
How do I access the other_data field in the server response?

Related

How to query filter json with Retrofit

I have api that return list of workers:
{
"items": [
{
"id": "e0fceffa-cef3-45f7-97c6-6be2e3705927",
"avatarUrl": "https://cdn.fakercloud.com/avatars/marrimo_128.jpg",
"firstName": "Dee",
"lastName": "Reichert",
"userTag": "LK",
"department": "back_office",
"position": "Technician",
"birthday": "2004-08-02",
"phone": "802-623-1785"
},
{
"id": "6712da93-2b1c-4ed3-a169-c69cf74c3291",
"avatarUrl": "https://cdn.fakercloud.com/avatars/alterchuca_128.jpg",
"firstName": "Kenton",
"lastName": "Fritsch",
"userTag": "AG",
"department": "analytics",
"position": "Orchestrator",
"birthday": "1976-06-14",
"phone": "651-313-1140"
},
....
]
}
I want to filter the response so that I only get information about a worker from a specific department.
I tried to do it like this:
interface WorkersApi {
#GET("users")
suspend fun getWorkers(
#Query("department") department: String
): Workers
}
But it return the same list without any filters. How can I filter the response so that I only get information about a worker from a specific department?
*Workers is just data class that hold only one field - list of items(workers)
What you tried to do changes the request to the server. It sends the department as query parameter with the request. If the server doesn't support this parameter nothing happens. I don't know if you work together with the people controlling the backend but you could discuss with them if they could add functionality for filtered results.
If instead, you want to filter the results after getting the full list from the server simply apply a filter on the list that you got.
you could do this on your Workers object
val department = "example"
val filteredList = workersObject.items.filter {it.department == department}

How To Parse Response of Multiple Types of Single Key

How To Parse Response of Multiple Types?
Key is like (suppose student_list is a key of list types when student_list is empty then it makes as a string like student_list=""), How to manage this types of response using Retrofit? I am using MVVM Model with retrofit.
My Response:
when I get Data into the List
{
"status": 200,
"data": [
{
"prod_month_total": 2989.61,
"product": "GAS"
},
{
"prod_month_total": 39566.22,
"product": "OIL"
},
{
"prod_month_total": 83912.55,
"product": "OTHER"
}
]
}
when List is Empty Then Response:
{"status":404,"data":"No result found"}
I am getting this Error:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 23 path $.data
first create the right model calss use this site http://www.jsonschema2pojo.org
than use
`if( reponce.isSucessful){
if(responce.status==200){
//code here
}
else{
// find the error
}
}else
{
//
}`

Making Post Requests to NodeJS Server Using Postman

I am building a movies API with a node-js back-end. I have used postman to create the data to be fetched at the API. The first object works fine, I am able to GET and POST only one object like this:
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
}
When I try to create more than one object in my POST request, the data returned at the end point is garbage, like this:
[
{
_id: "59b4c78a1157f62f88defdee",
__v: 0,
isReleased: true
}
]
The data I am trying to send to the end point resembles this format, json array of objects:
[
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
},
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
},
{
"title": "The Dictator",
"genre": "drama, comedy",
"rating": "7.2",
"isReleased": "true"
}
]
I am using postman to create post and mongodb to save data. My code looks like this:
var moviesModel = require('./../models/movieModel');
/**
Get Request For Retrieving All Movies from MongoDB
*/
var getMovies = function(req, res){
moviesModel.find(function(err, movies_data){
if (err) {
console.error(`Collections Empty`);
res.status(500);
res.end('A Server Error Has Occurred');
}
res.status(200);
res.send(movies_data);
});
}
/**
Post Request For Adding Movie to MongoDB
*/
var addMovie = function(req, res){
var m = new moviesModel(req.body);
m.save(function(err){
if (err) {
res.status(500);
res.end(`Save Operation Failed`);
} else {
res.status(201); //movie created
res.send(m);
}
});
}
My schema is defined in movieModel.js file. Could someone share with me how to construct this kind of post request with postman or share a link?
I know this question has no programming relevance but it could help me with building APIs to call from my website and android app. Thanks.

Cached complex/nested objects and querying them with Greendao Android

Ok this is the scenario, I have a complex object like this (see below) and it is downloaded and parsed using Retrofit. I generated the Java class (POJO) using GreenDao and everything is ok while the app is running: retrofit response body is as I want, I insert this response.body() to the DB and if I query in other activity this object is retrived by GreenDao with all the attributes setted (is the cached object).
The problem is when I clear all the caches or close the app and open it again, the list of the nested objects is null (see the second json below).
How can I solve this? Thanks in Advance
Json sent from the server
{
"id" : 91
"timestamp": 1487786669,
"groups": [
{
"id": "G1",
"name": "Some name",
"nextPaymentDate": "2017-06-01",
"totalAmount": 1000.00,
"overdraft": false,
"apoyaT": true
},
{
"id": "G2",
"name": "Other name",
"nextPaymentDate": "2017-06-02",
"totalAmount": 1000.00,
"overdraft": true,
"apoyaT": false
}
]
}
Json/JavaObject when the cache is cleared
{
"id": 91
"timestamp": 1487786669,
"groups": null
}
My GreenDao Generator
/* List Entity ***************************/
Entity list = schema.addEntity("List_p");
list.addIntProperty("id").primaryKey();
list.addLongProperty("timestamp");
/* Group Entity **************************/
Entity group = schema.addEntity("Group");
group.addStringProperty("id").primaryKey();
group.addStringProperty("name");
group.addStringProperty("nextPaymentDate");
group.addDoubleProperty("totalAmount");
group.addBooleanProperty("overdraft");
group.addBooleanProperty("apoyaT");
Property listId = group.addLongProperty("listId").getProperty();
list.addToMany(group, listId, "groups");

Laravel display results of an array as single json object

Is there a way I can get my response to be a single object with an array of users using Eloquent?
For instance:
{
"results" : [
{ "email" : "test1#test.ca" },
{ "email" : "test2#test.ca" }
]
}
Current it outputs like this:
[
{
"email": "test1#test.ca",
},
{
"email": "test2#test.ca",
}
]
This is how I'm displaying users from my code:
$users = User::whereIn('number', $numbers)->select('email')->get();
return $users;
Which would be fine but I'm using Volley for Android using JSONObjectRequest but its failing when it tries to parse the JSON because it can't parse the array.
You can try it like this:
$users = User::whereIn('number', $numbers)->select('email')->get();
return Response::json(array('results' => $users));

Categories

Resources