I use FOSrestbundle in my API to get my objects as a JSON.
So my controller look like that:
public function getArticleAction($id)
{
$article = $this->getDoctrine()
->getRepository('ApiBundle:Article')
->find($id);
return $this->handleView($this->view($article));
}
And my issue is that an article contains comments, added by users so my json looks like that:
{
"title": "My Article",
"comments": [
{
"content": "my first comment",
"added_by": {
"username": "John"
}
},
{
"content": "my second comment",
"added_by": {
"username": "Smith"
}
}
]
}
and when I render it, there is too many queries, for each comment there is a query to get user informations.
Do I have to get my article object with a querybuilder and lot of join (because it is just an example but I have a lot more relations) to get all the informations in one query or is there an other trick to avoid it ?
You should use EAGER loading for added_by relation while querying your data using Doctrine. This way you will query all data you need for serialization at once.
Related
I'm using a schema interceptor to configure my schema. It's a multi-tenant application, so I build the schema according to the tenant's configuration. I'm mapping that configuration to SDL language (schema-first approach) and then I add it to the schema builder (schemaBuilder.AddDocumentFromString(...)).
As said on the documentation (here), "Schema-first does currently not support filtering!". But that is the only approach I can use right now, so I'm trying to find a workaround.
What I've tried:
Manually create the input filter types and add the filtering to the server (something like this):
...
schemaBuilder.AddDocumentFromString(#"
type Query {
persons(where: PersonFilterInput): [Person]
}
input PersonFilterInput {
and: [PersonFilterInput!]
or: [PersonFilterInput!]
name: StringOperationFilterInput
}
input StringOperationFilterInput {
and: [StringOperationFilterInput!]
or: [StringOperationFilterInput!]
eq: String
neq: String
contains: String
ncontains: String
in: [String]
nin: [String]
startsWith: String
nstartsWith: String
endsWith: String
nendsWith: String
}
}
type Person {
name: String
}");
...
//add resolvers
...
And on the server configuration:
services
.AddGraphQLServer()
.TryAddSchemaInterceptor<TenantSchemaInterceptor>()
.AddFiltering();
However, this is not enough because the filters aren't being applied.
Query:
{
persons (where: { name: {eq: "Joao" }}){
name
}
}
Results:
{
"data": {
"persons": [
{
"name": "Joao"
},
{
"name": "Liliana"
}
]
}
}
Is there anything I can do to workaround this problem?
Thank you people
Filter support for schema-first is coming with version 12. You then do not even have to specify everything since we will provide schema building directives.
type Query {
persons: [Person] #filtering
}
type Person {
name: String
}
you also will be able to control which filter operations can be provided. We have the first preview coming up this week.
I have a firestore database as below
My firestore database
I would like to query the CASH AND CREDT amount for document 20191003.
I used the below REST api
POST https://firestore.googleapis.com/v1/projects/badansales/databases/(default)/documents/sales/20191003:runQuery?key={YOUR_API_KEY}
{
"structuredQuery": {
"select": {
"fields": [
{
"fieldPath": "CASH"
},
{
"fieldPath": "CREDT"
}
]
}
}
}
But the response is as below
{
"0": {
"readTime": "2019-10-04T02:03:57.366908Z"
}
}
Can someone help on this? Thanks
I'd advise reading the documentation here: https://firebase.google.com/docs/firestore/use-rest-api#making_rest_calls which recommends checking out the API Explorer. This can help you create a query.
For your example, maybe try:
GET https://firestore.googleapis.com/v1/projects/badansales/databases/(default)/documents/sales/20191003?key={YOUR_API_KEY}
Notice two things:
It's a GET and not a POST (there's no alteration of the data here).
I'm stuck with querying Firestore REST API. I can figure out how to filter by a regular key but I can't figure out how to query by nested object key.
Basically, in the browser client, I can do
firestore.collection("channels")
.where("members.[memberID]", "==", true)
.get()
However, when I try to do the same in the REST API, it said Invalid Argument. I was trying to do
{
"structuredQuery": {
"where": {
"fieldFilter": {
"field": {
"fieldPath": "members.[memberID]"
},
"op": "EQUAL",
"value": {
booleanValue: "true"
}
}
},
"from": [
{
"collectionId": "channels"
}
]
}
}
but it gives me "Invalid Argument" error on fieldPath. Does anybody know how to query Firestore REST API based on nested object?
Thank you!
I asked for Firebase support and they said special characters in fieldPath such as "-" in my case can be escaped using back tick or back slash. It is mentioned in the documentation. In my case, I'll need to do
members.`[memberID]`
I hope this is helpful for you guys.
Following this tutorial as a guide (OData/EntityFramework/Asp.Net).
I'm able to execute a simple GET command on the root.
{
"#odata.context": "http://localhost:49624/$metadata",
"value": [
{
"name": "Appointments",
"kind": "EntitySet",
"url": "Appointments"
},
......
{
"name": "Clients",
"kind": "EntitySet",
"url": "Clients"
}
]
}
But anything more complex than that gives me an error message. (I'm using a null routePrefix.)
http://localhost:49624/Services
Gives me:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:49624/Services'.",
"MessageDetail": "No type was found that matches the controller named 'Services'."
}
Here's my super simple GET
[EnableQuery]
public IQueryable<Service> Get()
{
return db.Services;
}
If it matters I'm using Postman to test these commands. Although I imagine that is a non-factor.
I have a database & a DbSet for every table. I have no idea why I can't access any of this.
WebApiConfig:
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Appointment>("Appointments");
builder.EntitySet<Service>("Services");
builder.EntitySet<Employee>("Employees");
builder.EntitySet<Client>("Clients");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
I'm sorry if this is a basic question but I'm really new to all this and have been at this wall too long already haha.
Jan Hommes above pointed out above that the controller class needs to be pluralized (In my case ServiceController -> ServicesController)
I'm attempting to build off of Mike Jansen's JIRA REST Client, and I'm trying to pull in JIRA version information. I'm new to JSON, so I'm not sure if it's just a formatting issue or what.
When debugging, I have the following token:
{[
{
"self": "https://<company>.atlassian.net/rest/api/2/version/10101",
"id": "10101",
"name": "2012.3",
"archived": false,
"released": false,
"releaseDate": "2012-10-08"
},
{
"self": "https://<company>.atlassian.net/rest/api/2/version/10200",
"id": "10200",
"name": "2012.4",
"archived": false,
"released": false
}
]}
and the following line of code
token.Children().Values<T>()
is throwing the following error
Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken
while trying to convert the two version tokens into the corresponding JiraVersion class:
using System;
namespace JiraRestClient
{
public class JiraVersion : JiraObjectBase, IJiraVersion
{
public JiraVersion(IJiraRestResponse jiraResponse) : base(jiraResponse) { }
public string Id { get { return Get<string>("Id", "id"); } }
public string Name { get { return Get<string>("Name", "name"); } }
}
}
Can somebody help me out?
Those of you familiar with JSON may have noticed quickly that it was, in fact, a problem with the formatting (the extra curly brackets enclosing the array). Like I said, I'm new to JSON, but the end result of my research is that the JsonWrapper.TryGetPath(...) method attempts to traverse the JObject tree and does not produce correctly formatted JSON when what's being retrieved is an array.
My solution was to simplify things by removing JSON.Net from the solution and depending only on RestSharp (just because it makes it so easy to make requests) and the System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(response.Content) approach.