Ad

Why Is The Fetch Function Saying, That I Have To Use Absolute Urls, Even If I Have Set A Proxy?

- 1 answer

At the moment I am coding a Shopify application. I want to fetch all the products from my store in server.js but every time it outputs a message, that says that only absolute urls are supported. A registered Webhook should get all the products inside my shop.

Error: only absolute urls are supported

Here is my javascript (server.js)

const { default: proxy } = require('@shopify/koa-shopify-graphql-proxy');
const { ApiVersion } = require('@shopify/koa-shopify-graphql-proxy');

app.prepare().then(() => {
    const server = new Koa();
    const router = new Router();
    server.use(session(server));
    server.keys = [/** Shopify Keys */];

    server.use(
        createShopifyAuth({
        /**
         * Webhook
         */
        }),
    );

    const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });
    router.post('/webhooks/products/create', webhook, async (ctx) => {
        await fetch('/graphql', {
            credentials: 'include',
            body: allProducts
        })
        .then((data) => {
            console.log(data)
        })
        .catch((err) => {
            console.log(err)
        })

        console.log('received Webhook: ', ctx.state.webhook);
    })

    server.use(router.allowedMethods());
    server.use(router.routes());
    console.log(proxy({ version: ApiVersion.Unstable }))
    server.use(proxy({ version: ApiVersion.Unstable }))

    server.listen(port, () => {
    console.log(`> Ready on localhost:${port}`)
    })
})

I was using the example from the npm package shopify koa proxy link here

How can I send http request with the proxy I am using?

Ad

Answer

The issue is exactly what the error says, fetch requires absolute urls.

Whether you have a proxy or not is really irrelevant to the fetch api, it doesn't know about that.

Just give it an absolute URL

Ad
source: stackoverflow.com
Ad