I have a web page on which I want to present a set of client's articles. A single article is comprised of - Title, Author, Short description, Article's body. On the main page I would like to display all available articles in the shorter form, including only Title and Short description.
When I create on Strapi's backend collection of Articles (with aforementioned fields) getting all articles (via GET request) involves fetching all article's data (Title, Author, Short description and Article's body), whereas at the main page the only information required are Title and Short description.
Is there a way to design it on Strapi's backend so that only a partial information be sent in a response?
As @zero298 suggested, graphql is the best way to achieve what you want.
The other way to do that is by customizing your api's repsonse.
When you create a content type, it generates an API with the following list of endpoints.
Each of these endpoint triggers a controller action. Here is the list of controller actionsthat exist by default when a content type is created.
If you check the controller file of your generated API ./api/{content-type}/controller/{Content-Type}.js
, you will see an empty file. It is because all the default logic is managed by Strapi. But you can override these actions with your own code.
Refer to the following documentation Example. This example explains exactly the use case that you want.
But,i prefer using the graphQL enpoint provided by Strapi. This is much simpler to use especially for your use case.
Steps:
Login as the super admin user , and select plugins and then install the graphql plugin.
You can also install the plugin through terminal using npm or yarn ( eg. npm run strapi install graphql
)
Then, start your app and open your browser at http://localhost:1337/graphql (opens new window). You should see the interface (GraphQL Playground) that will help you to write GraphQL query to explore your data.
To perform authorized requests, you must first get a JWT. Refer to the link on how to perform the authorized requests.
To perform query request you can resfer to Query api .For each model, the plugin auto-generates queries and mutations which just fit to your needs
This is how you can start using the graphQL. Btw, from you application, you would be needing a graphql client to perform the requests. You can refer to this article for the popular list of graphql clients. For simple use cases, I prefer graphql-request.
For response with partial use case data your graphql-request
query could be something like:
import { GraphQLClient, gql } from 'graphql-request'
async function main() {
const endpoint = 'your graphql endpoint'
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
},
})
const query = gql`
{
Article {
Title
Short description
}
}
`
const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))
}
main().catch((error) => console.error(error));
One of the drawbacks using the graphql
instead of the REST
endpoints is that, you need to specify all the fields that you require in the response. So, if you need all the fields for an endpoint you need to explicitly specify all the field names in the query.