O
O
Oleg Voitenko2021-05-16 18:55:17
Express.js
Oleg Voitenko, 2021-05-16 18:55:17

How to select all posts from categories Express + GraphQL?

Hello!

I can't figure out how to do the endpoint correctly.

There is a table in which there are records with fields id, title, category_id
There is a table with categories id, title, parent_id.

nesting of categories can be large
. For example, a table of categories
____________________________
| id | title | parent_id |
| 1 | title | 0 |
| 2 | title | 1 |
| 3 | title | 1 |
| 4 | title | 2 |
| 5 | title | 2 |
|_____________________|

The task is to select all posts from the category with id = 1 including nested categories id = 2.3 and id = 4.5, we need to make this function work and if we request posts in category 2, and it will find all posts in subcategories of this category. I use a request to the server in the GraphQL format

query Posts($alias: String) {
  postsitems(alias: $alias) {
      id
      name
  }
}


Server side using GraphQL + ExpressJS + sequelize (pgsql)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question