What I am trying to do is essentially fetch data from another endpoint, say a WordPress blog, and then loop through the results and create Articles/Posts in my RedwoodJS app. I don’t want to do this in the web client so I created a service to make the API call, then loop through my results and insert into my database.
// File: api/services/myservice.js
export const fetchArticles = async () => {
// API call to fetch articles
const articles = await apiFetchArticles()
articles.forEach((article) => {
// insert into database each article
db.article.create({
data: article,
})
})
return articles
}
What I don’t understand is how can I call this from the frontend using Redwood? This is how I am used to doing things in the “react way”, where a button onClick handler executes.
// my component to initiate these service requests
export const Page = () => {
const [articles, setArticles] = useState([])
const generateArticles = async () => {
// make API call to my service
const { articles } = await fetchArticles()
setArticles(articles)
}
return (
<>
<button onClick={generateArticles}>Generate Articles</button>
{articles.map((item) => {
return <li key={item.id}>{JSON.stringify(item)}</li>
})}
</>
)
}
I think the “RedwoodJS way” is to use Cells and create a GraphQL SDL, but that seems like a lot of extra work just to make an API call? I already have an Article model defined with Prisma and I just want my api method to return a list of records it created in the database. I’m new to GraphQL so learning that and Redwood is a little cumbersome.
How can I easily call my service from the web? Do I have to use GraphQL or is there an easier option?