WordPress GraphQl Endpoint not working in Production - but works on Netlify CLI Dev

I currently have a configuration (NextJs with GraphQL) where on my blog page, I fetch for posts from a WordPress website through GraphQL. Below, the code runs the function getPosts() which is inside an api/graphql-api.js file.

My issue is that this works perfectly in local environment as well as netlify-cli when I run it in dev. The problem I face is that it doesn’t fetch the blogs in Production and I cannot see any error logs to debug. I tried putting the graphql-api.js inside netlify->functions folder as well so that it can run through there but it still isn’t working (maybe because I’m importing the file from netlify/functions instead of .netlify/functions? What could be the problem? Below is the code:

export async function getServerSideProps(ctx){
  try {
    let blogPosts = await getPosts();
    return {
      props: {
        blogPosts
      }
    }
  } catch (error) {
    console.log(error);
    return {
      props: {
        blogPosts: []
      }
    }
  }
}

The graphql-api.js file is:

async function fetchAPI(query, { variables } = {}) {
  
    const headers = { 'Content-Type': 'application/json' }
    
    const res = await fetch('http://blog.example.com/graphql', {
      method: 'POST',
      headers,
      body: JSON.stringify({
        query,
        variables,
      }),
    })

    const json = await res.json()
    if (json.errors) {
      console.error(json.errors)
      throw new Error('Failed to fetch API')
    }
    return json.data
  }

export async function getPosts(){

    const data = await fetchAPI(
        `query AllPosts {
          posts(first: 9) {
            edges {
              cursor
              node {
                title
                id
                author {
                  node {
                    firstName
                    lastName
                    name
                  }
                }
                categories {
                  nodes {
                    name
                    slug
                    taxonomyName
                  }
                }
                databaseId
                excerpt(format: RENDERED)
                slug
                content(format: RENDERED)
                uri
                featuredImage {
                  node {
                    mediaItemUrl
                    sourceUrl
                  }
                }
                date
              }
            }
            pageInfo {
              hasNextPage
              hasPreviousPage
              endCursor
              startCursor
            }
          }
        }
      `,
        {
          variables: {},
        }
      );
    return data?.posts?.edges;
}

Additional Updates:

I modified the code to properly use the serverless functions. It is now working in netlify-cli, and even when I make the query request to the function on Postman, it returns data.
However, my production site still doesn’t work…

In POSTMAN, I am using {base_url}/.netlify/functions/graphql-api and querying it with the AllPosts query from post above and it returns just fine…

When I go to my blog page and have the function logs on to realtime, nothing shows up, as if it’s not even being processed. But when I run it through POSTMAN or just enter the endpoint on the address bar, I get console logs and data.

Could there be something on Netlify’s end hindering this process for Production only?

This is my new code (that works just fine in netlify-cli mind you with the serverless function):

export async function getServerSideProps(ctx){
  try {
    const headers = { 'Content-Type': 'application/json' }

    const res = await fetch('/.netlify/functions/graphql-api', {
      method: 'post',
      headers,
      body: JSON.stringify({
        query: `query AllPosts {
          posts(first: 9) {
            edges {
              cursor
              node {
                title
                id
                author {
                  node {
                    firstName
                    lastName
                    name
                  }
                }
                categories {
                  nodes {
                    name
                    slug
                    taxonomyName
                  }
                }
                databaseId
                excerpt(format: RENDERED)
                slug
                content(format: RENDERED)
                uri
                featuredImage {
                  node {
                    mediaItemUrl
                    sourceUrl
                  }
                }
                date
              }
            }
            pageInfo {
              hasNextPage
              hasPreviousPage
              endCursor
              startCursor
            }
          }
        }`,
        variables: {},
      }),
    });

    if (!res.ok) {
      throw new Error(`API response status: ${res.status}`);
    }

    const data = await res.json();
    console.log("Response data from serverless function:", data);

    let blogPosts = data?.posts?.edges;
    return {
      props: {
        blogPosts
      }
    }
  } catch (error) {
    console.log("Error while fetching data from serverless function:", error);
    return {
      props: {
        blogPosts: []
      }
    }
  }
}

and the /.netlify/functions/graphql-api.js

const fetch = require('isomorphic-fetch');


exports.handler = async function(event, context) {
  if (!event.body) {
    console.error("No body in the request");
    return {
      statusCode: 400,
      body: "Missing request body"
    };
  }

  const headers = { 'Content-Type': 'application/json' }

  const body = JSON.parse(event.body);
  const { query, variables } = body;

  const res = await fetch('https://blog.example.com/graphql', {
    method: 'POST',
    headers,
    body: JSON.stringify({
      query,
      variables,
    }),
  })

  const json = await res.json()
  if (json.errors) {
    console.error("API errors:", json.errors)
    throw new Error('Failed to fetch API')
  }
  console.log("Outgoing response body:", json.data);


  return {
    statusCode: 200,
    body: JSON.stringify(json.data),
    headers: {
      "Content-Type": "application/json"
    },
  }
}

It seems that the issue might be with Netlify’s server configuration. One thing you can try is to check if your environment variables are properly set in production. Sometimes, environment variables may not be set correctly which makes it impossible for Netlify to retrieve the necessary data.

You can also try to use console logs to debug your code on the production site to see where exactly the error occurs. Since you mentioned that your code works locally and on Netlify-cli, this might give you some direction as to what could be causing the problem.

Another suggestion is to check if there are any CORS issues that might be preventing your connection to the WordPress GraphQL API. You can try adding CORS headers to your serverless function to see if that resolves the issue.

Finally, you may consider reaching out to Netlify support and provide them with the information you have so far. They should be able to assist you further with troubleshooting the issue.

Good lucky.
:sparkling_heart:

Thanks for your response. I actually don’t have any environment variables set up for this.
I do have console logs set up but it doesn’t seem to fire when refreshing the blog page, almost as if the feth in getServerSideProps is not working, but when I do the request on POSTMAN, it works perfectly.

These are the CORS headers I tried adding on the serverless function to no avail:

    headers: {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "https://www.example.com",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
      "Access-Control-Allow-Headers": "Content-Type, Authorization",
    },

Hey all, the solution has been found.
Because no request was happening from the getServerSideProps, I decided to try getStaticProps (and I honestly should be using that anyway… so I made the necessary changes for that). It didn’t help, so I tried one more thing which was to use the exact url of the serverless function entirely, such as

https://www.example.com/.netlify/functions/graphql-api” instead of “/.netlify/functions/graphql-api”.

This is exactly what was missing. Now I don’t know if I remove the headers for CORS I added it would end up messing it up again, so I just left it there, but it seems that I needed to enter the entire origin URL with the endpoint.

hey thanks for coming back and sharing your solution!

1 Like