I created categories in craft and I'd like to get total amount of entries related to individual category. Right now I have a query that returns my categories:
query categoriesQuery {
categories(group: "projectCategories") {
id
slug
title
}
}
but I would also like for each category to return the amount of related entries.
So I would like to add another field that counts related entries - I wanted to do somethings like this, but it won't work:
query categoriesQuery {
categories(group: "projectCategories") {
id
slug
title
_count(relatedToEntries(section: "projects"))
}
}
Is there any way to get amount of entries related to single category? The result that I'm trying to achieve would look somethings like this:
{
"data": {
"categories": [
{
"id": "1",
"slug": "Category-1",
"title": "Category 1",
"amountOfRelatedEntries": 1
},
{
"id": "2",
"slug": "Category-2",
"title": "Category 2",
"amountOfRelatedEntries": 2
},
{
"id": "3",
"slug": "Category-3",
"title": "Category 3",
"amountOfRelatedEntries": 3
},
]
}
}
Related
I need help on how to delete records that exist in the DB but not in array sent in a request;
My Array:
[
{ "id": "509",
"name": "Motions move great",
"body": "",
"subtopics": [
{
"title": "Tywan",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
},
{
"title": "Transportations Gracious",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
},
{
"title": "Transportation part",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
}
]
},
{
"name": "Motions kkk",
"body": "",
"subtopics": [
{
"title": "Transportations",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
}
]
}
]
Below is my implementation: where am going wrong?
#topics = #course.topics.map{|m| m.id()}
#delete= #topics
puts #delete
if Topic.where.not('id IN(?)', #topics).any?
#topics.each do |topic|
topic.destroy
end
end
it's not clear to me where, in your code, you pick the ids sent in the array you showed before... so I'm assuming like this:
objects_sent = [
{ "id": "509",
"name": "Motions move great",
"body": "",
"subtopics": [
{
"title": "Tywan",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
},
{
"title": "Transportations Gracious",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
},
{
"title": "Transportation part",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
}
]
},
{
"name": "Motions kkk",
"body": "",
"subtopics": [
{
"title": "Transportations",
"url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
}
]
}
]
since you have your array like this, the only information you need to query on database is the ids (also, assuming the id's in the array are the id's on database, otherwise it wouldn't make sense). You can get them like this:
sent_ids = objects_sent.map{|o| o['id'].to_i}
Also, it seems to me that, for the code you showed, you want to destroy them based on a specific course. There would be 2 ways to do that. First, using the relationship (I prefer like this one):
#course.topics.where.not(id: sent_ids).destroy_all
Or you can do the query directly on the Topic model, but passing the course_id param:
Topic.where(course_id: #course.id).where.not(id: sent_ids).destroy_all
ActiveRecord is smart enough to mount that query correctly in both ways. Give it a test and see which works better for you
I'm using strapi and graphQL ,
with this query i can get posts
query {
posts{
id,
title
}
}
i want to get posts and totalCount together ,desirable result would like this
{
"data": {
"totalCount" : "3",
"posts": [
{
"id": "1",
"title": "first post "
},
{
"id": "4",
"title": "post two"
},
{
"id": "5",
"title": "post 3"
}
]
}
}
I've read document and I can make a query for getting just total count but it is not enough for me !
so , How can i get posts and totalCounts together ?
I just found myself in your shoes right now and the solution I found was:
query POSTS {
postsConnection {
aggregate {
count
}
}
posts{
id
title
}
}
In my Strapi project, i'm trying to write a Graphql query that filters posts by an array of tags.
The post has to belong to all tags of the given array.
E.g. the post below would not meet the condition if given array is [1,2,3] but would pass the condition if array is [3,5].
Any help would be much appreciated
Example Post:
{
"id": "1",
"title": "Lorem Iopsum",
"created_at": "2021-02-19T22:53:19.204Z",
"tags": [
{
"id": "3",
"tag": "Porte De Gentily"
},
{
"id": "5",
"tag": "Bridges"
},
{
"id": "6",
"tag": "Towers"
}
]
}
I was trying something like:
query {
posts( where: {tags: {id_contains: [3,1]}}) {
title
tags{id},
created_at
}
}
You can do it something like this
query($ids:[ID]) {
posts( where: {tags: {id:$ids}}) {
title
tags{id},
created_at
}
}
And pass your ids as a query variable:
{
"ids": [1,5]
}
I was just wondering how to deal with eager loading queries to retrieve data with several associations, let me explain my models:
type User struct {
Id uint
Name string
Username string
Image string
}
type Post struct {
Id unit
Content string
Author *User // I use user_id in author column for the posts table
Comments []Comments
}
type Comments struct {
Id unit
PostId uint
Message string
CommentBy *User // I use user_id in comment_by column for the comments table
}
This is an example of retrieved data:
{
"Posts": [
{
"Id": 1,
"Content": "test post",
"Author": {
"Id": 1,
"Name": "author",
"Username": "h3ll0",
"Image": "author.png"
},
"Comments": [
{
"Id": 1,
"PostId": 1,
"Message": "good article",
"CommentBy": {
"Id": 2,
"Name": "second user",
"Username": "r3ader",
"Image": "reader.png"
}
},
{
"Id": 2,
"PostId": 1,
"Message": "bad article",
"CommentBy": {
"Id": 3,
"Name": "third user",
"Username": "thirD",
"Image": "third.png"
}
}
]
}
]
}
I want to retrieve the post data with all nested associations and map them to the Post struct. How would you do it with database/sql and pq (Postgres driver)?
I don't want to use any ORM at all, I only use database/SQL and SQLx.
Keep in mind that the performance is important because The posts and comments tables are big enough.
I am working on a project where I have to create one to many relationships which will get all the list of records referenced by id in another table and I have to display all the selected data in the multi-select field (selectArrayInput). Please help me out in this, if you help with an example that would be great.
Thanks in advance.
Example:
district
id name
1 A
2 B
3 C
block
id district_id name
1 1 ABC
2 1 XYZ
3 2 DEF
I am using https://github.com/Steams/ra-data-hasura-graphql hasura-graphql dataprovider for my application.
You're likely looking for "nested object queries" (see: https://hasura.io/docs/1.0/graphql/manual/queries/nested-object-queries.html#nested-object-queries)
An example...
query MyQuery {
district(where: {id: {_eq: 1}}) {
id
name
blocks {
id
name
}
}
}
result:
{
"data": {
"district": [
{
"id": 1,
"name": "A",
"blocks": [
{
"id": 1,
"name": "ABC"
},
{
"id": 2,
"name": "XYZ"
}
]
}
]
}
}
Or...
query MyQuery2 {
block(where: {district: {name: {_eq: "A"}}}) {
id
name
district {
id
name
}
}
}
result:
{
"data": {
"block": [
{
"id": 1,
"name": "ABC",
"district": {
"id": 1,
"name": "A"
}
},
{
"id": 2,
"name": "XYZ",
"district": {
"id": 1,
"name": "A"
}
}
]
}
}
Setting up the tables this way...
blocks:
districts:
Aside: I recommend using plural table names as they are more standard, "districts" and "blocks"