Any help is appreciated!
I am currently retrieving data from a WP Rest API. When I run the WordPress site locally on my machine using http://localhost:8000 and access the graphql playground at http://localhost:3000/api/graphql, everything works fine as expected. However, when I update the WP rest API address to http://example.com/cms, I encounter an error. The only change is the URL, so I suspect it might be related to CORS.
Upon inspecting the browser window, there are no CORS errors, ruling out that possibility. What's puzzling is that making the API call through Postman yields the expected response, entering the endpoint in a browser gives the desired results, but using the endpoint to resolve the query request leads to an error. I started examining the headers, as they seem to be the only difference between a Postman request and a regular browser request. For the local WP installation at localhost:8000, I can see the requests being made from Postman, the browser, and Axios (used in the query resolver). Conversely, for the live WP installation online, the logs show requests from Postman and the browser to the API endpoint, but not from the graphql resolver. How can I resolve this issue where the resolver fails to make the request?
This is the resolver for the query:
const resolvers = {
Query: {
pages: (_parent, _args, _context) => {
return axios.get(`${wpURL}/wp-json/wp/v2/pages`)
.then(res => res.data)
.catch(error => {
console.log("Response Status:", error.response.status);
console.log("Response Headers:", error.response.headers);
console.log("Response Data:", error.response.data);
});
}
}
}
GraphQL Server:
import {ApolloServer} from 'apollo-server-micro'
import Cors from 'micro-cors'
import {schema} from './schema'
const cors = Cors()
const server = new ApolloServer({schema})
const handler = server.createHandler({path: '/api/graphql'})
export const config = {
api: {
bodyParser: false,
}
}
export default cors(handler)
Terminal:
> next dev
ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /api/graphql
wait - compiling...
event - build page: /api/graphql
event - compiled successfully
Screenshot of the issue: https://i.sstatic.net/EDTkZ.png
Any insights on what could be causing this issue?