Consolidate frontend REST calls with GraphQL
Deploy with Docker Compose / deploy (push) Failing after 1s
Deploy with Docker Compose / deploy (push) Failing after 1s
Replace 5 separate REST calls on home page load with a single GraphQL query. Add homeData store that fetches posts, favorites, activities, spotify, and auth in one request. Convert all admin mutations and auth flows to use GraphQL. Add album images to Spotify GraphQL schema. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
import axios from "axios";
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { gql } from "@/graphql";
|
||||
import { useHomeDataStore } from "@/stores/homeData";
|
||||
|
||||
const post_template = {
|
||||
title: "Can't fetch from the db yo",
|
||||
@@ -17,32 +18,34 @@ export const usePostsStore = defineStore("posts", () => {
|
||||
|
||||
const postsCount = computed(() => posts.value.length);
|
||||
|
||||
async function fetchPosts() {
|
||||
try {
|
||||
const res = await axios.get("/api/posts");
|
||||
if (!Array.isArray(res.data)) {
|
||||
throw new Error("Invalid response from posts API");
|
||||
const homeData = useHomeDataStore();
|
||||
watch(
|
||||
() => homeData.posts,
|
||||
(newPosts) => {
|
||||
if (newPosts.length > 0) {
|
||||
posts.value = newPosts;
|
||||
}
|
||||
posts.value = res.data;
|
||||
} catch (err) {
|
||||
console.error("Cannot connect to Post API", err);
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
async function fetchPosts() {
|
||||
await homeData.fetchAll();
|
||||
}
|
||||
|
||||
async function deletePost(post) {
|
||||
try {
|
||||
const res = await axios.delete(
|
||||
`/api/posts/${encodeURIComponent(post.id)}`,
|
||||
await gql(
|
||||
`mutation DeletePost($id: ID!) { deletePost(id: $id) { id } }`,
|
||||
{ id: post.id },
|
||||
);
|
||||
console.log("Deleted:", res.data);
|
||||
fetchPosts();
|
||||
console.log("Deleted:", post.id);
|
||||
await homeData.fetchAll();
|
||||
} catch (err) {
|
||||
console.error("Delete failed:", err);
|
||||
}
|
||||
}
|
||||
|
||||
fetchPosts();
|
||||
|
||||
return {
|
||||
posts,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user