Created
October 31, 2025 13:06
-
-
Save pochitax/c793287d2987055f966ede65759ebd5c to your computer and use it in GitHub Desktop.
función graphQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // tu función GraphQL | |
| function getPeliculasGraph(token) { | |
| const query = ` | |
| query { | |
| posts { | |
| nodes { | |
| title | |
| pelicula | |
| } | |
| } | |
| }`; | |
| return fetch('http://localhost:8888/wp-headless/wordpress/graphql', { | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': `Bearer ${token}`, | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({ query }) | |
| }) | |
| .then(response => response.json()); | |
| } | |
| // aquí llamas a la función // puedes ver el token en tu jwt.php | |
| const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0Ojg4ODgvd3AtaGVhZGxlc3Mvd29yZHByZXNzIiwiaWF0IjoxNzYxODc0NzQ4LCJuYmYiOjE3NjE4NzQ3NDgsImV4cCI6MTc2MjQ3OTU0OCwiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMiJ9fX0.ptA3JU5NpeuMJB_wsdtN2WNtLIw5lR9GsOSEUvjIe78"; // reemplázalo por tu token válido | |
| getPeliculasGraph(token) | |
| .then(wpJson => { | |
| console.log(wpJson); // ver en consola | |
| const lista = document.getElementById("peliculas-lista"); | |
| wpJson.data.posts.nodes.forEach(post => { | |
| const li = document.createElement("li"); | |
| li.textContent = post.pelicula; | |
| lista.appendChild(li); | |
| }); | |
| }) | |
| .catch(err => console.error("Error:", err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment