Cloudflare Pages and Gatsby Functions

Hello!

I have one question as I have started to tackle the backend for my GatsbyJS-based application.

The problem arises here because currently using Gatsby Functions I have already created some functions that work great in the developer version on localhost. The problem arises here because I noticed that when building my application my Gatsby Functions are not being built and when I try to call them it always gets a 404 code.

Because of this problem, I concluded that I will have to create Cloudflare Functions, so I took a look at your documentation. Unfortunately, I have been trying for several days to change my Gatsby Functions to be compatible with your Cloudflare Functions documentation and it doesn’t do anything.

In such a situation, is it possible to set the server in such a way that it does not block my Gatsby Functions when building the application?

Maybe someone can show me with the example below how I should work to move my Gatsby Functions to Cloudflare Functions?

import fetch from "node-fetch";


export default async function handler(req, res) {
  try {
    const { code } = req.body;

    if (!code) {
      return res.status(400).json({ error: "Code parameter is required." });
    }

    const apiUrl = process.env.ENDPOINT;
    const token = process.env.CLIENT;

    if (!apiUrl) {
      return res.status(500).json({ error: "API endpoint not defined." });
    }

    const headers = {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
    };

    const graphqlQuery = `
      query GetCoupon($code: String!) {
        coupons(where: { code: $code }) {
          id
          code
          limit
          use
          time
          value
          currency {
            code
          }
          products {
            id
          }
        }
      }
    `;

    const response = await fetch(apiUrl, {
      method: "POST",
      headers: headers,
      body: JSON.stringify({
        query: graphqlQuery,
        variables: { code },
      }),
    });

    if (response.ok) {
      const data = await response.json();

      if (data.data && data.data.coupons && data.data.coupons.length > 0) {
        const coupon = data.data.coupons[0];

        if (coupon.use >= coupon.limit) {
          return res.status(403).json({ error: "Kupon został już wykorzystany." });
        }

        const currentTime = new Date().toISOString().split("T")[0];
        const couponDate = coupon.time.split("T")[0];

        if (couponDate < currentTime) {
          return res.status(406).json({ error: "Kupon przeterminowany." });
        }

        const currentUse = coupon.use;
        const updatedUse = currentUse + 1;

        const mutation = `
          mutation MyMutation($id: ID!, $use: Float!) {
            updateCoupon(data: { use: $use }, where: { id: $id }) {
              limit
            }
          }
        `;

        const mutationResponse = await fetch(apiUrl, {
          method: "POST",
          headers: headers,
          body: JSON.stringify({
            query: mutation,
            variables: {
              id: coupon.id,
              use: updatedUse,
            },
          }),
        });

        if (mutationResponse.ok) {
          const mutationData = await mutationResponse.json();

          if (
            mutationData.data &&
            mutationData.data.updateCoupon &&
            mutationData.data.updateCoupon.limit
          ) {
            return res.status(200).json(data.data);
          } else {
            return res.status(404).json({ error: "Mutation failed." });
          }
        } else {
          return res
            .status(mutationResponse.status)
            .json({ error: "Error in mutation request." });
        }
      } else {
        return res.status(404).json({ error: "Nie ma takiego kuponu." });
      }
    } else {
      return res.status(response.status).json({ error: "Error in API request." });
    }
  } catch (error) {
    console.error("Error:", error);
    return res.status(500).json({ error: "Internal Server Error." });
  }
}